prompt

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	KeyEnter     = Key{'\n', '\n'}
	KeyEscape    = Key{0, 0x1b}
	KeyUp        = Key{0, 0xF1} // Unique code for up arrow
	KeyDown      = Key{0, 0xF2} // Unique code for down arrow
	KeyLeft      = Key{0, 0xF3} // Unique code for left arrow
	KeyRight     = Key{0, 0xF4} // Unique code for right arrow
	KeyHome      = Key{0, 0xF5} // Unique code for home
	KeyEnd       = Key{0, 0xF6} // Unique code for end
	KeyF1        = Key{0, 0xF7} // Unique code for F1
	KeyF2        = Key{0, 0xF8} // Unique code for F2
	KeyF3        = Key{0, 0xF9} // Unique code for F3
	KeyF4        = Key{0, 0xFA} // Unique code for F4
	KeyF5        = Key{0, 0xFB} // Unique code for F5
	KeyF6        = Key{0, 0xFC} // Unique code for F6
	KeyF7        = Key{0, 0xFD} // Unique code for F7
	KeyF8        = Key{0, 0xFE} // Unique code for F8
	KeyF9        = Key{0, 0xFF} // Unique code for F9
	KeyF10       = Key{0, 0xE1} // Unique code for F10
	KeyF11       = Key{0, 0xE2} // Unique code for F11
	KeyF12       = Key{0, 0xE3} // Unique code for F12
	KeyTab       = Key{'\t', '\t'}
	KeyBackspace = Key{0x7f, 0x7f}
	KeyCtrlC     = Key{0, 0x03}
	KeySpace     = Key{' ', ' '}
)

Special keys - using unique Code values so == comparison works

View Source
var KeyUnknown = Key{0, 0}

KeyUnknown represents an unknown key.

Functions

func ClearLine

func ClearLine(out io.Writer)

ClearLine clears the current line and moves cursor to the beginning.

func ClearToEndOfScreen

func ClearToEndOfScreen(out io.Writer)

ClearToEndOfScreen clears from cursor to end of screen.

func Confirm

func Confirm() clix.PromptOption

Confirm creates a confirmation prompt option. Note: This is now also available in core as clix.WithConfirm(), but kept here for backward compatibility and as a convenience alias. Usage:

prompter.Prompt(ctx, clix.WithLabel("Continue?"), Confirm())
// or use clix.WithConfirm() directly

func HideCursor

func HideCursor(out io.Writer)

HideCursor hides the terminal cursor.

func MoveCursorDown

func MoveCursorDown(out io.Writer, n int)

MoveCursorDown moves the cursor down n lines.

func MoveCursorUp

func MoveCursorUp(out io.Writer, n int)

MoveCursorUp moves the cursor up n lines.

func MultiSelect

func MultiSelect(options []clix.SelectOption) clix.PromptOption

MultiSelect creates a multi-select prompt option. This option is only valid when using the prompt extension (TerminalPrompter). Usage:

prompter.Prompt(ctx, clix.WithLabel("Select"), MultiSelect([]clix.SelectOption{...}))

func RestoreCursorPosition

func RestoreCursorPosition(out io.Writer)

RestoreCursorPosition restores the cursor to a previously saved position.

func SaveCursorPosition

func SaveCursorPosition(out io.Writer)

SaveCursorPosition saves the current cursor position.

func Select

func Select(options []clix.SelectOption) clix.PromptOption

Select creates a select prompt option. This option is only valid when using the prompt extension (TerminalPrompter). Usage:

prompter.Prompt(ctx, clix.WithLabel("Choose"), Select([]clix.SelectOption{...}))

func ShowCursor

func ShowCursor(out io.Writer)

ShowCursor shows the terminal cursor.

func WithContinueText

func WithContinueText(text string) clix.PromptOption

WithContinueText sets the text shown for the continue action in multi-select prompts. Usage:

prompter.Prompt(ctx, clix.WithLabel("Select"), MultiSelect([]clix.SelectOption{...}), WithContinueText("Finish"))

Types

type Extension

type Extension struct {
}

Extension replaces the default TextPrompter with TerminalPrompter, enabling advanced prompt features: select, multi-select, confirm, and raw terminal mode.

Without this extension, advanced prompt types (select, multi-select) return errors directing users to add the extension. With this extension, all prompt types are supported.

Example:

import (
	"github.com/SCKelemen/clix"
	"github.com/SCKelemen/clix/ext/prompt"
)

app := clix.NewApp("myapp")
app.AddExtension(prompt.Extension{})
// Now your app supports select, multi-select, and confirm prompts

// Use advanced prompts via the standard PromptRequest API:
result, err := app.Prompter.Prompt(ctx, clix.PromptRequest{
	Label: "Choose an option",
	Options: []clix.SelectOption{
		{Label: "Option A", Value: "a"},
		{Label: "Option B", Value: "b"},
	},
})

// Or use the helper function for type-safe access:
if tp := prompt.AsTerminalPrompter(app.Prompter); tp != nil {
	// Access TerminalPrompter-specific methods if needed
}

func (Extension) Extend

func (Extension) Extend(app *clix.App) error

Extend implements clix.Extension.

type Key

type Key struct {
	Rune rune
	Code byte
}

Key represents a pressed key.

func ReadKey

func ReadKey(in io.Reader) (key Key, err error)

ReadKey reads a single keypress from the terminal, returning the key code and any special keys (arrows, enter, etc.)

func (Key) IsPrintable

func (k Key) IsPrintable() bool

IsPrintable returns true if the key represents a printable character.

func (Key) IsSpecial

func (k Key) IsSpecial() bool

IsSpecial returns true if this is a special key (arrow, enter, etc.)

func (Key) String

func (k Key) String() string

String returns a string representation of the key.

type TerminalPrompter

type TerminalPrompter struct {
	In  io.Reader
	Out io.Writer
}

TerminalPrompter implements Prompter with full support for text, select, multi-select, and confirm prompts, including raw terminal mode for interactive navigation.

func AsTerminalPrompter

func AsTerminalPrompter(p clix.Prompter) *TerminalPrompter

AsTerminalPrompter safely casts a Prompter to TerminalPrompter. Returns nil if the prompter is not a TerminalPrompter (e.g., it's a TextPrompter). This allows you to access advanced prompt methods with compile-time safety.

Example:

if tp := prompt.AsTerminalPrompter(app.Prompter); tp != nil {
	result, err := tp.PromptWithOptions(ctx, clix.PromptRequest{
		TextPromptRequest: clix.TextPromptRequest{Label: "Choose"},
		Options: []clix.SelectOption{{Label: "A", Value: "a"}},
	})
}

func (TerminalPrompter) Prompt

func (p TerminalPrompter) Prompt(ctx context.Context, opts ...clix.PromptOption) (string, error)

Prompt displays a prompt and reads the user's response. Supports all prompt types: text, select, multi-select, and confirm.

type TerminalState

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

TerminalState manages raw terminal mode for interactive prompts.

func EnableRawMode

func EnableRawMode(in *os.File) (*TerminalState, error)

EnableRawMode enables raw terminal mode for reading individual keystrokes.

func (*TerminalState) Restore

func (ts *TerminalState) Restore() error

Restore restores the terminal to its previous state.

Jump to

Keyboard shortcuts

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