ui

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package ui provides user interface formatting and interaction utilities.

Index

Constants

View Source
const (
	Reset  = "\033[0m"
	Bold   = "\033[1m"
	Dim    = "\033[2m"
	Italic = "\033[3m"
	Under  = "\033[4m"

	// Foreground colors.
	Black   = "\033[30m"
	Red     = "\033[31m"
	Green   = "\033[32m"
	Yellow  = "\033[33m"
	Blue    = "\033[34m"
	Magenta = "\033[35m"
	Cyan    = "\033[36m"
	White   = "\033[37m"

	// Bright foreground colors.
	BrightBlack   = "\033[90m"
	BrightRed     = "\033[91m"
	BrightGreen   = "\033[92m"
	BrightYellow  = "\033[93m"
	BrightBlue    = "\033[94m"
	BrightMagenta = "\033[95m"
	BrightCyan    = "\033[96m"
	BrightWhite   = "\033[97m"

	// Background colors.
	BgBlack   = "\033[40m"
	BgRed     = "\033[41m"
	BgGreen   = "\033[42m"
	BgYellow  = "\033[43m"
	BgBlue    = "\033[44m"
	BgMagenta = "\033[45m"
	BgCyan    = "\033[46m"
	BgWhite   = "\033[47m"
)

Color codes.

View Source
const (
	// ColorReset resets all colors and styles.
	ColorReset = "\033[0m"

	// ColorBlack is black color.
	ColorBlack = "\033[30m"

	// ColorRed is red color.
	ColorRed = "\033[31m"

	// ColorGreen is green color.
	ColorGreen = "\033[32m"

	// ColorYellow is yellow color.
	ColorYellow = "\033[33m"

	// ColorBlue is blue color.
	ColorBlue = "\033[34m"

	// ColorMagenta is magenta color.
	ColorMagenta = "\033[35m"

	// ColorCyan is cyan color.
	ColorCyan = "\033[36m"

	// ColorWhite is white color.
	ColorWhite = "\033[37m"
)

Color constants for terminal output.

Variables

This section is empty.

Functions

func Alert

func Alert(message string, alertType PromptType)

Alert displays an alert message.

func AlertError

func AlertError(message string)

AlertError displays an error alert.

func AlertInfo

func AlertInfo(message string)

AlertInfo displays an info alert.

func AlertSuccess

func AlertSuccess(message string)

AlertSuccess displays a success alert.

func AlertWarning

func AlertWarning(message string)

AlertWarning displays a warning alert.

func BoldText

func BoldText(text string) string

BoldText returns bold text.

func ClearLine

func ClearLine()

ClearLine clears the current line.

func Colorize

func Colorize(color, text string) string

Colorize applies color to text if colors are enabled.

func DimText

func DimText(text string) string

DimText returns dimmed text.

func Error

func Error(text string) string

Error returns text in red.

func ForceColor

func ForceColor(force bool)

ForceColor forces color output even if not in a terminal.

func FormatDuration

func FormatDuration(seconds int) string

FormatDuration formats duration with colors.

func FormatSize

func FormatSize(size int64) string

FormatSize formats a file size with appropriate units and colors.

func FormatSpeed

func FormatSpeed(bytesPerSecond int64) string

FormatSpeed formats download speed with colors.

func HideCursor

func HideCursor()

HideCursor hides the cursor.

func Highlight

func Highlight(text string) string

Highlight returns text in cyan.

func Info

func Info(text string) string

Info returns text in blue.

func IsColorSupported

func IsColorSupported() bool

IsColorSupported checks if the terminal supports color output.

func IsTerminalInteractive

func IsTerminalInteractive() bool

IsTerminalInteractive checks if the current session is interactive.

func MoveCursorDown

func MoveCursorDown(n int)

MoveCursorDown moves cursor down n lines.

func MoveCursorUp

func MoveCursorUp(n int)

MoveCursorUp moves cursor up n lines.

func PrintHeader

func PrintHeader(text string)

PrintHeader prints a formatted header.

func PrintKeyValue

func PrintKeyValue(key, value string)

PrintKeyValue prints a key-value pair with formatting.

func PrintSection

func PrintSection(text string)

PrintSection prints a section header.

func Prompt

func Prompt(message string, promptType PromptType) string

Prompt displays a prompt and waits for user input.

func PromptChoice

func PromptChoice(message string, choices []string, defaultChoice int) int

PromptChoice presents multiple choices to the user.

func PromptPassword

func PromptPassword(message string) string

PromptPassword prompts for a password (input is hidden).

func PromptYesNo

func PromptYesNo(message string, defaultYes bool) bool

PromptYesNo asks a yes/no question.

func SetColorEnabled

func SetColorEnabled(enabled bool)

SetColorEnabled enables or disables color output.

func ShowCursor

func ShowCursor()

ShowCursor shows the cursor.

func ShowProgress

func ShowProgress(current, total int64, message string)

ShowProgress displays a simple text progress indicator.

func Success

func Success(text string) string

Success returns text in green.

func Warning

func Warning(text string) string

Warning returns text in yellow.

Types

type ColorConfig

type ColorConfig struct {
	Enabled bool
	Force   bool
}

ColorConfig manages color output settings.

type ErrorFormatOptions

type ErrorFormatOptions struct {
	ShowErrorCode   bool // Show error codes
	ShowSuggestions bool // Show recovery suggestions
	ShowTimestamp   bool // Show timestamp
	Compact         bool // Use compact format
	MultiLine       bool // Use multi-line format for complex errors
}

ErrorFormatOptions configures error message formatting.

type Formatter

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

Formatter provides comprehensive formatting capabilities for various output types.

func NewFormatter

func NewFormatter() *Formatter

NewFormatter creates a new formatter with default settings.

func (*Formatter) ClearLine

func (f *Formatter) ClearLine()

ClearLine clears the current line in the terminal.

func (*Formatter) ConfirmPrompt

func (f *Formatter) ConfirmPrompt(message string, defaultValue bool) (bool, error)

ConfirmPrompt displays a yes/no confirmation prompt.

func (*Formatter) CreateLoadingAnimation

func (f *Formatter) CreateLoadingAnimation(message string) func()

CreateLoadingAnimation creates a simple loading animation.

func (*Formatter) FormatError

func (f *Formatter) FormatError(err error, options *ErrorFormatOptions) string

FormatError formats an error message with optional error details.

func (*Formatter) FormatMessage

func (f *Formatter) FormatMessage(msgType MessageType, format string, args ...interface{}) string

FormatMessage formats a message with appropriate styling based on type.

func (*Formatter) FormatProgressBar

func (f *Formatter) FormatProgressBar(current, total uint64, options *ProgressBarOptions) string

FormatProgressBar creates a formatted progress bar.

func (*Formatter) FormatStatusIndicator

func (f *Formatter) FormatStatusIndicator(status StatusIndicator, message string) string

FormatStatusIndicator formats a status indicator with appropriate styling.

func (*Formatter) NewTableFormatter

func (f *Formatter) NewTableFormatter(headers []string) *TableFormatter

NewTableFormatter creates a new table formatter.

func (*Formatter) PrintMessage

func (f *Formatter) PrintMessage(msgType MessageType, format string, args ...interface{})

PrintMessage prints a formatted message to the output writer.

func (*Formatter) Prompt

func (f *Formatter) Prompt(message string) (string, error)

Prompt displays an interactive prompt and returns the user's response.

func (*Formatter) SelectPrompt

func (f *Formatter) SelectPrompt(message string, options []string, defaultIndex int) (int, error)

SelectPrompt displays a selection prompt with multiple options.

func (*Formatter) WithColor

func (f *Formatter) WithColor(enabled bool) *Formatter

WithColor enables or disables color output.

func (*Formatter) WithInteractive

func (f *Formatter) WithInteractive(interactive bool) *Formatter

WithInteractive enables or disables interactive mode.

func (*Formatter) WithLanguage

func (f *Formatter) WithLanguage(lang Language) *Formatter

WithLanguage sets the language for localized messages.

func (*Formatter) WithWriter

func (f *Formatter) WithWriter(w io.Writer) *Formatter

WithWriter sets the output writer.

type Language

type Language string

Language represents supported languages.

const (
	// English language.
	LanguageEnglish Language = "en"

	// Japanese language.
	LanguageJapanese Language = "ja"

	// Spanish language.
	LanguageSpanish Language = "es"

	// French language.
	LanguageFrench Language = "fr"
)

type MessageType

type MessageType int

MessageType represents different types of messages.

const (
	// MessageInfo for informational messages.
	MessageInfo MessageType = iota

	// MessageSuccess for success messages.
	MessageSuccess

	// MessageWarning for warning messages.
	MessageWarning

	// MessageError for error messages.
	MessageError

	// MessageDebug for debug messages.
	MessageDebug

	// MessagePrompt for interactive prompts.
	MessagePrompt
)

type ProgressBarOptions

type ProgressBarOptions struct {
	Width           int           // Width of the progress bar
	ShowPercentage  bool          // Show percentage
	ShowSpeed       bool          // Show download speed
	ShowETA         bool          // Show estimated time remaining
	ShowSize        bool          // Show downloaded/total size
	RefreshInterval time.Duration // How often to refresh
	Template        string        // Custom template for progress display
}

ProgressBarOptions configures progress bar appearance and behavior.

type PromptType

type PromptType int

PromptType represents the type of prompt.

const (
	PromptTypeInfo PromptType = iota
	PromptTypeWarning
	PromptTypeError
	PromptTypeSuccess
	PromptTypeQuestion
)

type Spinner

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

Spinner represents a console spinner.

func NewSpinner

func NewSpinner(style SpinnerStyle, message string) *Spinner

NewSpinner creates a new spinner with the specified style.

func (*Spinner) SetInterval

func (s *Spinner) SetInterval(interval time.Duration)

SetInterval sets the spinner interval.

func (*Spinner) SetMessage

func (s *Spinner) SetMessage(message string)

SetMessage updates the spinner message.

func (*Spinner) Start

func (s *Spinner) Start()

Start starts the spinner.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop stops the spinner.

func (*Spinner) StopWithError

func (s *Spinner) StopWithError(message string)

StopWithError stops the spinner with an error message.

func (*Spinner) StopWithMessage

func (s *Spinner) StopWithMessage(message string)

StopWithMessage stops the spinner and displays a final message.

func (*Spinner) StopWithSuccess

func (s *Spinner) StopWithSuccess(message string)

StopWithSuccess stops the spinner with a success message.

func (*Spinner) StopWithWarning

func (s *Spinner) StopWithWarning(message string)

StopWithWarning stops the spinner with a warning message.

type SpinnerStyle

type SpinnerStyle int

SpinnerStyle represents different spinner styles.

const (
	SpinnerStyleDots SpinnerStyle = iota
	SpinnerStyleLine
	SpinnerStyleCircle
	SpinnerStyleSquare
	SpinnerStyleArrow
	SpinnerStyleBounce
)

type StatusIndicator

type StatusIndicator int

StatusIndicator represents different status states.

const (
	// StatusPending for pending operations.
	StatusPending StatusIndicator = iota

	// StatusInProgress for ongoing operations.
	StatusInProgress

	// StatusCompleted for completed operations.
	StatusCompleted

	// StatusFailed for failed operations.
	StatusFailed

	// StatusPaused for paused operations.
	StatusPaused

	// StatusCancelled for cancelled operations.
	StatusCancelled
)

type TableFormatter

type TableFormatter struct {
	Headers []string
	Rows    [][]string
	// contains filtered or unexported fields
}

TableFormatter provides table formatting capabilities.

func (*TableFormatter) AddRow

func (tf *TableFormatter) AddRow(row []string)

AddRow adds a row to the table.

func (*TableFormatter) Format

func (tf *TableFormatter) Format() string

Format formats the table for display.

Jump to

Keyboard shortcuts

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