ux

package
v0.0.0-...-5f05eaf Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BoldString = color.New(color.Bold).SprintfFunc()
View Source
var ErrCancelled = errors.New("cancelled by user")

Functions

func Hyperlink(url string, text ...string) string

Hyperlink returns a hyperlink formatted string.

func NewVisualElement

func NewVisualElement(renderFn RenderFn) *visualElement

func Ptr

func Ptr[T any](value T) *T

Types

type Canvas

type Canvas interface {
	Run() error
	Update() error
	WithWriter(writer io.Writer) Canvas
}

func NewCanvas

func NewCanvas(visuals ...Visual) Canvas

NewCanvas creates a new Canvas instance.

type CanvasSize

type CanvasSize struct {
	Rows int
	Cols int
}

CanvasSize represents the size of the canvas.

type Confirm

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

Confirm is a component for prompting the user to confirm a message.

func NewConfirm

func NewConfirm(options *ConfirmOptions) *Confirm

NewConfirm creates a new Confirm instance.

func (*Confirm) Ask

func (p *Confirm) Ask() (*bool, error)

Ask prompts the user to confirm a message.

func (*Confirm) Render

func (p *Confirm) Render(printer Printer) error

Render renders the Confirm component.

func (*Confirm) WithCanvas

func (p *Confirm) WithCanvas(canvas Canvas) Visual

WithCanvas sets the canvas for the Confirm component.

type ConfirmOptions

type ConfirmOptions struct {
	// The writer to use for output (default: os.Stdout)
	Writer io.Writer
	// The reader to use for input (default: os.Stdin)
	Reader io.Reader
	// The default value to use for the prompt (default: nil)
	DefaultValue *bool
	// The message to display before the prompt
	Message string
	// The optional message to display when the user types ? (default: "")
	HelpMessage string
	// The optional hint text that display after the message (default: "[Type ? for hint]")
	Hint string
	// The optional placeholder text to display when the value is empty (default: "")
	PlaceHolder string
}

ConfirmOptions represents the options for the Confirm component.

var DefaultConfirmOptions ConfirmOptions = ConfirmOptions{
	Writer: os.Stdout,
	Reader: os.Stdin,
}

type CursorPosition

type CursorPosition struct {
	Row int
	Col int
}

CursorPosition represents the position of the cursor on the canvas.

type Printer

type Printer interface {
	internal.Cursor

	Fprintf(format string, a ...any)
	Fprintln(a ...any)

	ClearCanvas()

	CursorPosition() CursorPosition
	SetCursorPosition(position CursorPosition)
	Size() CanvasSize
}

Printer is a base component for UX components that require a printer for rendering.

func NewPrinter

func NewPrinter(writer io.Writer) Printer

NewPrinter creates a new Printer instance.

type Prompt

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

Prompt is a component for prompting the user for input.

func NewPrompt

func NewPrompt(options *PromptOptions) *Prompt

NewPrompt creates a new Prompt instance.

func (*Prompt) Ask

func (p *Prompt) Ask() (string, error)

Ask prompts the user for input.

func (*Prompt) Render

func (p *Prompt) Render(printer Printer) error

Render renders the prompt.

func (*Prompt) WithCanvas

func (p *Prompt) WithCanvas(canvas Canvas) Visual

WithCanvas sets the canvas for the prompt.

type PromptOptions

type PromptOptions struct {
	// The writer to use for output (default: os.Stdout)
	Writer io.Writer
	// The reader to use for input (default: os.Stdin)
	Reader io.Reader
	// The default value to use for the prompt (default: "")
	DefaultValue string
	// The message to display before the prompt
	Message string
	// The optional message to display when the user types ? (default: "")
	HelpMessage string
	// The optional hint text that display after the message (default: "[Type ? for hint]")
	Hint string
	// The optional placeholder text to display when the value is empty (default: "")
	PlaceHolder string
	// The optional validation function to use
	ValidationFn func(string) (bool, string)
	// The optional validation message to display when validation fails (default: "Invalid input")
	ValidationMessage string
	// The optional validation message to display when the value is empty and required (default: "This field is required")
	RequiredMessage string
	// Whether or not the prompt is required (default: false)
	Required bool
	// Whether or not to clear the prompt after completion (default: false)
	ClearOnCompletion bool
	// Whether or not to capture hint keys (default: true)
	IgnoreHintKeys bool
}

PromptOptions represents the options for the Prompt component.

var DefaultPromptOptions PromptOptions = PromptOptions{
	Writer:            os.Stdout,
	Reader:            os.Stdin,
	Required:          false,
	ValidationMessage: "Invalid input",
	RequiredMessage:   "This field is required",
	Hint:              "[Type ? for hint]",
	ClearOnCompletion: false,
	IgnoreHintKeys:    false,
	ValidationFn: func(input string) (bool, string) {
		return true, ""
	},
}

type RenderFn

type RenderFn func(printer Printer) error

type Select

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

Select is a component for prompting the user to select an option from a list.

func NewSelect

func NewSelect(options *SelectOptions) *Select

NewSelect creates a new Select instance.

func (*Select) Ask

func (p *Select) Ask() (*int, error)

Ask prompts the user to select an option from a list.

func (*Select) Render

func (p *Select) Render(printer Printer) error

Render renders the Select component.

func (*Select) WithCanvas

func (p *Select) WithCanvas(canvas Canvas) Visual

WithCanvas sets the canvas for the select component.

type SelectOptions

type SelectOptions struct {
	// The writer to use for output (default: os.Stdout)
	Writer io.Writer
	// The reader to use for input (default: os.Stdin)
	Reader io.Reader
	// The default value to use for the prompt (default: nil)
	SelectedIndex *int
	// The message to display before the prompt
	Message string
	// The available options to display
	Allowed []string
	// The optional message to display when the user types ? (default: "")
	HelpMessage string
	// The optional hint text that display after the message (default: "[Type ? for hint]")
	Hint string
	// The maximum number of options to display at one time (default: 6)
	DisplayCount int
	// Whether or not to display the number prefix before each option (default: false)
	DisplayNumbers *bool
	// Whether or not to disable filtering (default: true)
	EnableFiltering *bool
}

SelectOptions represents the options for the Select component.

var DefaultSelectOptions SelectOptions = SelectOptions{
	Writer:          os.Stdout,
	Reader:          os.Stdin,
	SelectedIndex:   Ptr(0),
	DisplayCount:    6,
	EnableFiltering: Ptr(true),
	DisplayNumbers:  Ptr(false),
}

type SetProgressFunc

type SetProgressFunc func(string)

SetProgressFunc is a function that sets the progress of a task.

type Spinner

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

Spinner is a component for displaying a spinner.

func NewSpinner

func NewSpinner(options *SpinnerOptions) *Spinner

NewSpinner creates a new Spinner instance.

func (*Spinner) Render

func (s *Spinner) Render(printer Printer) error

Render renders the spinner.

func (*Spinner) Run

func (s *Spinner) Run(ctx context.Context, task func(context.Context) error) error

Run runs a task with the spinner.

func (*Spinner) Start

func (s *Spinner) Start(ctx context.Context) error

Start starts the spinner.

func (*Spinner) Stop

func (s *Spinner) Stop(ctx context.Context) error

Stop stops the spinner.

func (*Spinner) UpdateText

func (s *Spinner) UpdateText(text string)

UpdateText updates the text of the spinner.

func (*Spinner) WithCanvas

func (s *Spinner) WithCanvas(canvas Canvas) Visual

WithCanvas sets the canvas for the spinner.

type SpinnerOptions

type SpinnerOptions struct {
	Animation   []string
	Text        string
	Interval    time.Duration
	ClearOnStop bool
	Writer      io.Writer
}

SpinnerOptions represents the options for the Spinner component.

var DefaultSpinnerOptions SpinnerOptions = SpinnerOptions{
	Animation: []string{"|", "/", "-", "\\"},
	Text:      "Loading...",
	Interval:  250 * time.Millisecond,
	Writer:    os.Stdout,
}

type Task

type Task struct {
	Title  string
	Action func(SetProgressFunc) (TaskState, error)
	State  TaskState
	Error  error
	// contains filtered or unexported fields
}

Task represents a task in the task list.

type TaskList

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

TaskList is a component for managing a list of tasks.

func NewTaskList

func NewTaskList(options *TaskListOptions) *TaskList

NewTaskList creates a new TaskList instance.

func (*TaskList) AddTask

func (t *TaskList) AddTask(options TaskOptions) *TaskList

AddTask adds a task to the task list and manages async/sync execution.

func (*TaskList) Render

func (t *TaskList) Render(printer Printer) error

Render renders the task list.

func (*TaskList) Run

func (t *TaskList) Run() error

Run executes all async tasks first and then runs queued sync tasks sequentially.

func (*TaskList) WithCanvas

func (t *TaskList) WithCanvas(canvas Canvas) Visual

WithCanvas sets the canvas for the TaskList component.

type TaskListOptions

type TaskListOptions struct {
	// The writer to use for output (default: os.Stdout)
	Writer             io.Writer
	MaxConcurrentAsync int
	SuccessStyle       string
	ErrorStyle         string
	WarningStyle       string
	RunningStyle       string
	SkippedStyle       string
	PendingStyle       string
}

TaskListOptions represents the options for the TaskList component.

var DefaultTaskListOptions TaskListOptions = TaskListOptions{
	Writer:             os.Stdout,
	MaxConcurrentAsync: 5,

	SuccessStyle: color.GreenString("(✔) Done "),
	ErrorStyle:   color.RedString("(x) Error "),
	WarningStyle: color.YellowString("(!) Warning "),
	RunningStyle: color.CyanString("(-) Running "),
	SkippedStyle: color.HiBlackString("(-) Skipped "),
	PendingStyle: color.HiBlackString("(o) Pending "),
}

type TaskOptions

type TaskOptions struct {
	Title  string
	Action func(SetProgressFunc) (TaskState, error)
	Async  bool
}

TaskOptions represents the options for the Task component.

type TaskState

type TaskState int

TaskState represents the state of a task.

const (
	Pending TaskState = iota
	Running
	Skipped
	Warning
	Error
	Success
)

type Visual

type Visual interface {
	Render(printer Printer) error
	WithCanvas(canvas Canvas) Visual
}

func Render

func Render(renderFn RenderFn) Visual

type VisualContext

type VisualContext struct {
	// The size of the visual
	Size CanvasSize
	// The relative row position of the visual within the canvas
	Top int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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