ui

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ExitOK    = 0
	ExitError = 1 // generic error, bad input, validation
	ExitAuth  = 2 // missing/invalid token
	ExitNet   = 3 // network errors, timeouts, 5xx
)

Exit codes used by the CLI. Returned via os.Exit at the entrypoint.

Variables

View Source
var StatusShort = map[string]int{
	"want":    1,
	"reading": 2,
	"read":    3,
	"paused":  4,
	"dnf":     5,
	"ignored": 6,
}

StatusShort maps short keys to status ids.

Functions

func Exit

func Exit(err error)

Exit prints the error to stderr (if any) and exits with the appropriate code.

func ExitCodeFor

func ExitCodeFor(err error) int

ExitCodeFor returns the appropriate exit code for the given error. It unwraps the error chain looking for known sentinels and falls back to ExitError for anything else.

IMPORTANT: every call site that wants to surface a sentinel must wrap with %w (fmt.Errorf("...: %w", errs.ErrNoToken)), never %v. errors.Is unwraps the %w chain; %v produces a fresh string error that won't match.

func HasColor

func HasColor(disableColor bool) bool

HasColor reports whether colored output is appropriate. True when stdout is a terminal, NO_COLOR is not set, and --no-color was not passed (caller passes disableColor).

func IsInteractive

func IsInteractive() bool

IsInteractive reports whether stdin and stdout are both terminals. Use for commands that prompt the user interactively.

func JoinStrings

func JoinStrings(parts []string, sep string) string

JoinStrings joins a slice with a separator, ignoring empty entries.

func PadRight

func PadRight(s string, width int) string

PadRight pads s with spaces to width, ignoring ANSI.

func SelectBook

func SelectBook(ctx context.Context, books []api.UserBook, styles *Styles) (*api.UserBook, error)

SelectBook runs an interactive picker and returns the chosen user_book. The context is forwarded to the bubbletea program so Ctrl-C cancels cleanly. Returns (nil, nil) if the user cancelled.

In a non-TTY environment (e.g. piped stdin), SelectBook falls back to returning the first book without launching a picker.

func SelectEdition

func SelectEdition(ctx context.Context, editions []api.EditionResult, styles *Styles) (*api.EditionResult, error)

SelectEdition runs an interactive picker for book editions. If there is only one edition it is returned directly. Returns (nil, nil) if the user cancelled.

func SelectSearchResult

func SelectSearchResult(ctx context.Context, results []api.SearchResult, styles *Styles) (*api.SearchResult, error)

SelectSearchResult runs an interactive picker for catalog search results. Returns (nil, nil) if the user cancelled.

func SelectStatus

func SelectStatus(ctx context.Context, styles *Styles) (int, error)

SelectStatus runs an interactive picker for reading statuses. Returns the chosen status ID, or (0, nil) if the user cancelled. In non-interactive mode, returns 1 ("Want to Read") without prompting.

func ShouldSpinner

func ShouldSpinner(jsonMode bool) bool

ShouldSpinner reports whether a yacspin spinner should be shown. True when stderr is a terminal and --json was not passed.

func StatusName

func StatusName(statusID int) string

StatusName returns a human-readable status name.

func StripANSI

func StripANSI(s string) string

StripANSI removes all ANSI escape sequences from s.

func TerminalWidth

func TerminalWidth() int

TerminalWidth returns the width of the terminal in columns. Returns 0 if the width cannot be determined.

func Truncate

func Truncate(s string, n int) string

Truncate returns s truncated to n columns, with an ellipsis if truncated.

func VisibleWidth

func VisibleWidth(s string) int

VisibleWidth returns the display width of s, stripping ANSI escapes and counting CJK/wide characters as 2 columns.

func WithSpinner

func WithSpinner(ctx context.Context, jsonMode bool, fn func(context.Context) error) error

WithSpinner displays a terminal spinner while fn runs. The spinner is suppressed when stdout is not a TTY or when jsonMode is true.

func WithSpinnerMsg

func WithSpinnerMsg(ctx context.Context, jsonMode bool, msg string, fn func(context.Context) error) error

WithSpinnerMsg displays a terminal spinner with an optional message while fn runs. The spinner is suppressed when stdout is not a TTY or when jsonMode is true.

Types

type Styles

type Styles struct {
	Color bool

	Title       lipgloss.Style
	Bold        lipgloss.Style
	Dim         lipgloss.Style
	Green       lipgloss.Style
	Red         lipgloss.Style
	Yellow      lipgloss.Style
	Cyan        lipgloss.Style
	Magenta     lipgloss.Style
	BGreen      lipgloss.Style
	BYellow     lipgloss.Style
	BCyan       lipgloss.Style
	SuccessBold lipgloss.Style
}

ColorOn returns lipgloss with colors if hasColor is true, plain otherwise. Stash a package-level "Styles" struct as well as per-call adaptive renderers.

func NewStyles

func NewStyles(color bool) *Styles

NewStyles returns styles with colors enabled/disabled.

func (*Styles) Apply

func (s *Styles) Apply(style lipgloss.Style, str string) string

Apply wraps a string with a color style if colors are enabled.

func (*Styles) Bullet

func (s *Styles) Bullet() string

Bullet returns a cyan bullet point.

func (*Styles) ProgressBar

func (s *Styles) ProgressBar(pct float64, width int) string

ProgressBar renders a percentage progress bar with a color gradient. pct is 0-100, width is the number of characters in the bar.

func (*Styles) Separator

func (s *Styles) Separator(ch string, maxWidth int) string

Separator returns a horizontal line of the given character, sized to the terminal width (capped at maxWidth).

func (*Styles) StatusColor

func (s *Styles) StatusColor(statusID int) lipgloss.Style

StatusColor returns the appropriate color style for a Hardcover status id. 1=Want, 2=Reading, 3=Read, 4=Paused, 5=DNF, 6=Ignored.

func (*Styles) Success

func (s *Styles) Success(msg string) string

Success returns a green-styled message with no prefix glyph.

Jump to

Keyboard shortcuts

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