cli

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package cli provides CLI application utilities wrapping Cobra and Lipgloss.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bold

func Bold(format string, args ...interface{})

Bold prints bold text.

func CaptureInput

func CaptureInput(initial string, extension string) (string, error)

CaptureInput opens $EDITOR with initial content and returns the edited text.

This is useful for capturing long-form input like commit messages, SQL queries, or configuration in CLI applications.

The extension parameter determines the temp file suffix (e.g., "md", "sql", "json"), which helps editors apply syntax highlighting.

Falls back to "vim" if $EDITOR is unset.

Example:

text, err := cli.CaptureInput("# Enter your notes\n", "md")
if err != nil {
    return err
}
fmt.Println("You entered:", text)

func CaptureInputWithEditor

func CaptureInputWithEditor(editor, initial, extension string) (string, error)

CaptureInputWithEditor opens a specific editor with initial content.

This variant is useful for testing or when you want to override the default editor.

Example:

// Force nano regardless of $EDITOR
text, err := cli.CaptureInputWithEditor("nano", "", "txt")

func Command

func Command(use, short string, run func(cmd *cobra.Command, args []string) error) *cobra.Command

Command creates a new cobra command with common setup.

Example:

cmd := cli.Command("serve", "Start the server", func(cmd *cobra.Command, args []string) error {
    return server.Run()
})

func CommandWithArgs

func CommandWithArgs(use, short string, nArgs int, run func(cmd *cobra.Command, args []string) error) *cobra.Command

CommandWithArgs creates a command that requires positional arguments.

func Dim

func Dim(format string, args ...interface{})

Dim prints dimmed text.

func Error

func Error(format string, args ...interface{})

Error prints an error message.

func Fatal

func Fatal(format string, args ...interface{})

Fatal prints an error and exits with code 1.

func FatalErr

func FatalErr(msg string, err error)

FatalErr prints an error message with the error and exits.

func Group

func Group(use, short string) *cobra.Command

Group creates a command group (no run function, just subcommands).

func Info

func Info(format string, args ...interface{})

Info prints an info message.

func KeyValue

func KeyValue(data map[string]string)

KeyValue prints a simple key-value list.

Example:

cli.KeyValue(map[string]string{
    "Host": "localhost",
    "Port": "8080",
})

func List

func List(items ...string)

List prints a bulleted list.

Example:

cli.List("Item 1", "Item 2", "Item 3")

func Must

func Must(err error)

Must exits if err is not nil.

func NumberedList

func NumberedList(items ...string)

NumberedList prints a numbered list.

Example:

cli.NumberedList("First", "Second", "Third")

func SetStyledHelp added in v0.2.0

func SetStyledHelp(cmd *cobra.Command)

SetStyledHelp configures beautiful help output for a command.

func SimpleTable

func SimpleTable(headers []string, rows [][]string)

SimpleTable prints a quick table without building.

Example:

cli.SimpleTable(
    []string{"Name", "Value"},
    [][]string{
        {"Host", "localhost"},
        {"Port", "8080"},
    },
)

func Success

func Success(format string, args ...interface{})

Success prints a success message.

func Warning

func Warning(format string, args ...interface{})

Warning prints a warning message.

func WithSpinner

func WithSpinner(message string, fn func() error) error

WithSpinner runs a function with a spinner, handling success/error.

Example:

err := cli.WithSpinner("Processing...", func() error {
    return doSomething()
})

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App builds CLI applications with sensible defaults.

func NewApp

func NewApp(name, version string) *App

NewApp creates a new CLI application builder.

Example:

app := cli.NewApp("myapp", "1.0.0").
    WithDescription("My application").
    WithConfig("config.yaml").
    WithEnvPrefix("MYAPP")

app.AddCommand(serveCmd)
app.AddCommand(migrateCmd)

if err := app.Run(); err != nil {
    os.Exit(1)
}

func (*App) AddCommand

func (a *App) AddCommand(cmd *cobra.Command) *App

AddCommand adds a subcommand.

func (*App) Root

func (a *App) Root() *cobra.Command

Root returns the root cobra command for advanced customization.

func (*App) Run

func (a *App) Run() error

Run executes the CLI application.

func (*App) RunWithArgs

func (a *App) RunWithArgs(args []string) error

RunWithArgs executes with specific arguments (useful for testing).

func (*App) Viper

func (a *App) Viper() *viper.Viper

Viper returns the viper instance for config access.

func (*App) WithConfig

func (a *App) WithConfig(path string) *App

WithConfig sets the config file path.

func (*App) WithConfigName

func (a *App) WithConfigName(name string) *App

WithConfigName sets the config file name (without extension) to search for.

func (*App) WithDescription

func (a *App) WithDescription(desc string) *App

WithDescription sets the app description.

func (*App) WithEnvPrefix

func (a *App) WithEnvPrefix(prefix string) *App

WithEnvPrefix sets the environment variable prefix.

func (*App) WithLongDescription

func (a *App) WithLongDescription(long string) *App

WithLongDescription sets detailed description.

func (*App) WithStandardFlags

func (a *App) WithStandardFlags() *App

WithStandardFlags adds common flags (config, verbose, quiet).

type Progress

type Progress struct {
	// contains filtered or unexported fields
}

Progress shows a simple progress indicator.

func NewProgress

func NewProgress(message string, total int) *Progress

NewProgress creates a progress indicator.

Example:

p := cli.NewProgress("Processing files", 100)
for i := 0; i < 100; i++ {
    p.Increment()
    // do work
}
p.Done()

func (*Progress) Done

func (p *Progress) Done()

Done completes the progress and moves to a new line.

func (*Progress) Increment

func (p *Progress) Increment()

Increment advances the progress by 1.

func (*Progress) Set

func (p *Progress) Set(current int)

Set sets the current progress value.

func (*Progress) SetWriter

func (p *Progress) SetWriter(w io.Writer) *Progress

SetWriter sets the output writer.

type Spinner

type Spinner struct {
	// contains filtered or unexported fields
}

Spinner shows an animated spinner with a message.

func NewSpinner

func NewSpinner(message string) *Spinner

NewSpinner creates a new spinner with a message.

Example:

s := cli.NewSpinner("Loading...")
s.Start()
// do work
s.Stop()

func (*Spinner) Start

func (s *Spinner) Start()

Start begins the spinner animation. For long-running operations, prefer StartWithContext to ensure cleanup.

func (*Spinner) StartWithContext added in v0.2.0

func (s *Spinner) StartWithContext(ctx context.Context)

StartWithContext begins the spinner animation with context cancellation. The spinner stops automatically when the context is cancelled.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop stops the spinner and clears the line.

func (*Spinner) StopError

func (s *Spinner) StopError(message string)

StopError stops and prints an error message.

func (*Spinner) StopSuccess

func (s *Spinner) StopSuccess(message string)

StopSuccess stops and prints a success message.

func (*Spinner) StopWithMessage

func (s *Spinner) StopWithMessage(message string)

StopWithMessage stops and prints a final message.

func (*Spinner) Update

func (s *Spinner) Update(message string)

Update changes the spinner message.

func (*Spinner) WithDelay

func (s *Spinner) WithDelay(d time.Duration) *Spinner

WithDelay sets the animation speed.

func (*Spinner) WithFrames

func (s *Spinner) WithFrames(frames []string) *Spinner

WithFrames sets custom animation frames.

func (*Spinner) WithWriter

func (s *Spinner) WithWriter(w io.Writer) *Spinner

WithWriter sets the output writer.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table builds styled terminal tables.

func NewTable

func NewTable(headers ...string) *Table

NewTable creates a new table with headers.

Example:

t := cli.NewTable("ID", "Name", "Status")
t.AddRow("1", "Alice", "Active")
t.AddRow("2", "Bob", "Inactive")
t.Print()

func (*Table) AddRow

func (t *Table) AddRow(values ...string) *Table

AddRow adds a row to the table.

func (*Table) Print

func (t *Table) Print()

Print renders the table to the configured writer.

func (*Table) SetWriter

func (t *Table) SetWriter(w io.Writer) *Table

SetWriter sets the output writer.

func (*Table) String

func (t *Table) String() string

String returns the table as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL