console

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package console provides a high-level output and interaction API for CLI commands. It wraps a cobra.Command and renderer, providing structured output (tables, records, lists), interactive prompts (ask, confirm, choose, secret), feedback (spinner, progress), and markup-aware printing.

Usage:

c := console.New(cmd)
c.Render(vm)                           // template + markup → output
c.Print("<bold>hello</bold> world")    // markup-aware print
name, _ := c.Ask("What is your name?")
ok, _ := c.Confirm("Continue?")

Index

Constants

This section is empty.

Variables

View Source
var ErrNonInteractive = errors.New("prompt requires interactive mode (remove --non-interactive)")

ErrNonInteractive is returned when a prompt is called in non-interactive mode.

Functions

This section is empty.

Types

type Console

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

Console wraps a cobra.Command with structured output and interaction methods.

func New

func New(cmd *cobra.Command) *Console

New creates a Console from a cobra command. The renderer format is determined by the command's --format flag.

func (*Console) Alert

func (c *Console) Alert(message string)

Alert prints a yellow warning mark followed by the message.

func (*Console) Ask

func (c *Console) Ask(title string) (string, error)

Ask shows a text input prompt and returns the user's input. Returns ErrNonInteractive if --non-interactive is set.

func (*Console) AskWithPlaceholder

func (c *Console) AskWithPlaceholder(title, placeholder string) (string, error)

AskWithPlaceholder shows a text input prompt with a placeholder hint. Returns ErrNonInteractive if --non-interactive is set.

func (*Console) Choose

func (c *Console) Choose(title string, options []Option) (string, error)

Choose shows a selection prompt and returns the chosen value. Returns ErrNonInteractive if --non-interactive is set.

func (*Console) ChooseStrings

func (c *Console) ChooseStrings(title string, choices []string) (string, error)

ChooseStrings is a convenience wrapper for Choose when labels and values are the same.

func (*Console) Confirm

func (c *Console) Confirm(title string) (bool, error)

Confirm shows a yes/no confirmation prompt and returns the user's choice. Returns ErrNonInteractive if --non-interactive is set.

func (*Console) Debug

func (c *Console) Debug(msg string, keyvals ...any)

Debug writes a debug-level log message.

func (*Console) Dump

func (c *Console) Dump(v ...any)

Dump pretty-prints values to stderr for debugging. Output is always sent to stderr to avoid contaminating structured stdout.

func (*Console) Error

func (c *Console) Error(msg string, keyvals ...any)

Error writes an error-level log message.

func (*Console) Fail

func (c *Console) Fail(message string)

Fail prints a red x mark followed by the message.

func (*Console) Format

func (c *Console) Format() renderer.Format

Format returns the active output format.

func (*Console) Info

func (c *Console) Info(message string)

Info prints a dim informational message.

func (*Console) Log

func (c *Console) Log(msg string, keyvals ...any)

Log writes an info-level log message.

func (*Console) Print

func (c *Console) Print(text string)

Print writes markup-processed text to the appropriate output stream.

func (*Console) Printf

func (c *Console) Printf(format string, args ...any)

Printf formats and writes markup-processed text.

func (*Console) Println

func (c *Console) Println(text string)

Println writes markup-processed text followed by a newline.

func (*Console) Progress

func (c *Console) Progress(title string, total int) *progress.Bar

Progress returns a configured progress bar ready to run.

c.Progress("deploying", len(services)).Run(func(s *progress.Sender) error {
    for i, svc := range services {
        s.Step(i, svc.Name)
        // ...
        s.ItemDone(svc.Name)
    }
    s.Step(len(services), "")
    return nil
})

func (*Console) Render

func (c *Console) Render(data any) error

Render delegates to the underlying renderer. For Templated viewmodels, this executes the template and applies markup post-processing. For JSON format, it marshals the struct.

func (*Console) Secret

func (c *Console) Secret(title string) (string, error)

Secret shows a password input prompt (masked characters). Returns ErrNonInteractive if --non-interactive is set.

func (*Console) Select

func (c *Console) Select(title string, options []Option, preselected []string) ([]string, error)

Select shows a multi-select prompt and returns the chosen values. preselected values are checked by default. Returns ErrNonInteractive if --non-interactive is set.

func (*Console) SelectStrings

func (c *Console) SelectStrings(title string, choices []string, preselected []string) ([]string, error)

SelectStrings is a convenience wrapper for Select when labels and values are the same.

func (*Console) Spinner

func (c *Console) Spinner(title string, action func()) error

Spinner shows a spinner with a title while the action runs.

func (*Console) Success

func (c *Console) Success(message string)

Success prints a green check mark followed by the message.

func (*Console) Warn

func (c *Console) Warn(msg string, keyvals ...any)

Warn writes a warn-level log message.

type Option

type Option struct {
	Label string
	Value string
}

Option represents a selectable choice for Choose.

Jump to

Keyboard shortcuts

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