Documentation
¶
Overview ¶
Package cli provides CLI application utilities wrapping Cobra and Lipgloss.
Index ¶
- func Bold(format string, args ...interface{})
- func CaptureInput(initial string, extension string) (string, error)
- func CaptureInputWithEditor(editor, initial, extension string) (string, error)
- func Command(use, short string, run func(cmd *cobra.Command, args []string) error) *cobra.Command
- func CommandWithArgs(use, short string, nArgs int, ...) *cobra.Command
- func Dim(format string, args ...interface{})
- func Error(format string, args ...interface{})
- func Fatal(format string, args ...interface{})
- func FatalErr(msg string, err error)
- func Group(use, short string) *cobra.Command
- func Info(format string, args ...interface{})
- func KeyValue(data map[string]string)
- func List(items ...string)
- func Must(err error)
- func NumberedList(items ...string)
- func SetStyledHelp(cmd *cobra.Command)
- func SimpleTable(headers []string, rows [][]string)
- func Success(format string, args ...interface{})
- func Warning(format string, args ...interface{})
- func WithSpinner(message string, fn func() error) error
- type App
- func (a *App) AddCommand(cmd *cobra.Command) *App
- func (a *App) Root() *cobra.Command
- func (a *App) Run() error
- func (a *App) RunWithArgs(args []string) error
- func (a *App) Viper() *viper.Viper
- func (a *App) WithConfig(path string) *App
- func (a *App) WithConfigName(name string) *App
- func (a *App) WithDescription(desc string) *App
- func (a *App) WithEnvPrefix(prefix string) *App
- func (a *App) WithLongDescription(long string) *App
- func (a *App) WithStandardFlags() *App
- type Progress
- type Spinner
- func (s *Spinner) Start()
- func (s *Spinner) StartWithContext(ctx context.Context)
- func (s *Spinner) Stop()
- func (s *Spinner) StopError(message string)
- func (s *Spinner) StopSuccess(message string)
- func (s *Spinner) StopWithMessage(message string)
- func (s *Spinner) Update(message string)
- func (s *Spinner) WithDelay(d time.Duration) *Spinner
- func (s *Spinner) WithFrames(frames []string) *Spinner
- func (s *Spinner) WithWriter(w io.Writer) *Spinner
- type Table
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureInput ¶
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 ¶
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 ¶
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 Fatal ¶
func Fatal(format string, args ...interface{})
Fatal prints an error and exits with code 1.
func KeyValue ¶
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 NumberedList ¶
func NumberedList(items ...string)
NumberedList prints a numbered list.
Example:
cli.NumberedList("First", "Second", "Third")
func SetStyledHelp ¶ added in v0.2.0
SetStyledHelp configures beautiful help output for a command.
func SimpleTable ¶
SimpleTable prints a quick table without building.
Example:
cli.SimpleTable(
[]string{"Name", "Value"},
[][]string{
{"Host", "localhost"},
{"Port", "8080"},
},
)
func WithSpinner ¶
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 ¶
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 ¶
AddCommand adds a subcommand.
func (*App) RunWithArgs ¶
RunWithArgs executes with specific arguments (useful for testing).
func (*App) WithConfig ¶
WithConfig sets the config file path.
func (*App) WithConfigName ¶
WithConfigName sets the config file name (without extension) to search for.
func (*App) WithDescription ¶
WithDescription sets the app description.
func (*App) WithEnvPrefix ¶
WithEnvPrefix sets the environment variable prefix.
func (*App) WithLongDescription ¶
WithLongDescription sets detailed description.
func (*App) WithStandardFlags ¶
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 ¶
NewProgress creates a progress indicator.
Example:
p := cli.NewProgress("Processing files", 100)
for i := 0; i < 100; i++ {
p.Increment()
// do work
}
p.Done()
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner shows an animated spinner with a message.
func NewSpinner ¶
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
StartWithContext begins the spinner animation with context cancellation. The spinner stops automatically when the context is cancelled.
func (*Spinner) StopSuccess ¶
StopSuccess stops and prints a success message.
func (*Spinner) StopWithMessage ¶
StopWithMessage stops and prints a final message.
func (*Spinner) WithFrames ¶
WithFrames sets custom animation frames.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table builds styled terminal tables.
func NewTable ¶
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()