Documentation ¶
Overview ¶
Library to interact with ansi/v100 style terminals.
Index ¶
- Constants
- Variables
- func SleepWithContext(ctx context.Context, duration time.Duration) error
- func TimeoutReader(fd int, tv *unix.Timeval, buf []byte) (int, error)
- func TimeoutToTimeval(timeout time.Duration) *unix.Timeval
- type AutoCompleteCallback
- type InterruptReader
- type InterruptedError
- type Terminal
- func (t *Terminal) AddToHistory(commands ...string)
- func (t *Terminal) AutoHistory() bool
- func (t *Terminal) Close() error
- func (t *Terminal) History() []string
- func (t *Terminal) IsTerminal() bool
- func (t *Terminal) NewHistory(capacity int)
- func (t *Terminal) ReadLine() (string, error)
- func (t *Terminal) ReplaceLatest(command string) string
- func (t *Terminal) ResetInterrupts(ctx context.Context) (context.Context, context.CancelFunc)
- func (t *Terminal) Resume(ctx context.Context) (context.Context, context.CancelFunc)
- func (t *Terminal) SetAutoCompleteCallback(f AutoCompleteCallback)
- func (t *Terminal) SetAutoHistory(enabled bool)
- func (t *Terminal) SetHistoryFile(f string) error
- func (t *Terminal) SetPrompt(s string)
- func (t *Terminal) Suspend()
Constants ¶
const CtrlC = 3 // Control-C is ascii 3 (C is 3rd letter of the alphabet)
const DefaultHistoryCapacity = term.DefaultHistoryEntries
DefaultHistoryCapacity is the default number of entries in the history (99).
const IsUnix = true
Variables ¶
var ( ErrUserInterrupt = NewErrInterrupted("terminal interrupted by user") ErrStopped = NewErrInterrupted("interrupt reader stopped") // not really an error more of a marker. )
Functions ¶
func SleepWithContext ¶ added in v0.8.0
func TimeoutReader ¶ added in v0.8.1
Types ¶
type AutoCompleteCallback ¶
type AutoCompleteCallback func(t *Terminal, line string, pos int, key rune) (newLine string, newPos int, ok bool)
AutoCompleteCallback is called with "this" terminal as first argument so AutoCompleteCallback can use t.Out etc. (compared to the original x/term callback).
type InterruptReader ¶ added in v0.8.0
type InterruptReader struct {
// contains filtered or unexported fields
}
func NewInterruptReader ¶ added in v0.8.0
func NewInterruptReader(reader *os.File, bufSize int) *InterruptReader
NewInterruptReader creates a new interrupt reader. it needs to be Start()ed to start reading from the underlying reader and intercept Ctrl-C and listen for interrupt signals.
func (*InterruptReader) Read ¶ added in v0.8.0
func (ir *InterruptReader) Read(p []byte) (int, error)
Implement io.Reader interface.
func (*InterruptReader) Start ¶ added in v0.8.0
func (ir *InterruptReader) Start(ctx context.Context) (context.Context, context.CancelFunc)
Start or restart (after a cancel/interrupt) the interrupt reader.
func (*InterruptReader) Stop ¶ added in v0.9.0
func (ir *InterruptReader) Stop()
type InterruptedError ¶ added in v0.8.1
func NewErrInterrupted ¶ added in v0.8.1
func NewErrInterrupted(reason string) InterruptedError
func NewErrInterruptedWithErr ¶ added in v0.8.1
func NewErrInterruptedWithErr(reason string, err error) InterruptedError
func (InterruptedError) Error ¶ added in v0.8.1
func (e InterruptedError) Error() string
func (InterruptedError) Unwrap ¶ added in v0.8.1
func (e InterruptedError) Unwrap() error
type Terminal ¶
type Terminal struct { // Use this for any output to the screen/console so the required \r are added in raw mode // the prompt and command edit is refresh as needed when input comes in. Out io.Writer // Cancellable context after Open(). Use it to cancel the terminal reading or check for done. Context context.Context //nolint:containedctx // To avoid Open() returning 4 values. Cancel context.CancelFunc // contains filtered or unexported fields }
func Open ¶
Open opens stdin as a terminal, do `defer terminal.Close()` to restore the terminal to its original state upon exit. fortio.org/log (and thus stdlib "log") will be redirected to the terminal in a manner that preserves the prompt. New cancellable context is returned, use it to cancel the terminal reading or check for done for control-c or signal.
func (*Terminal) AddToHistory ¶ added in v0.4.0
AddToHistory add commands to the history.
func (*Terminal) AutoHistory ¶ added in v0.5.0
AutoHistory returns the current auto history setting.
func (*Terminal) Close ¶
Close restores the terminal to its original state. Must be called at exit to avoid leaving the terminal in raw mode. Safe to call multiple times. Will save the history to the history file if one was set using [SetHistoryFile] and the capacity is > 0.
func (*Terminal) IsTerminal ¶
func (*Terminal) NewHistory ¶ added in v0.4.0
NewHistory creates/resets the history to a new one with the given capacity. Using 0 as capacity will disable history reading and writing but not change the underlying history state from it's DefaultHistoryCapacity.
func (*Terminal) ReadLine ¶
ReadLine reads a line from the terminal using the setup prompt and history and edit capabilities. Returns the line and an error if any. io.EOF is returned when the user presses Control-D. ErrInterrupted is returned when the user presses Control-C or a signal is received.
func (*Terminal) ReplaceLatest ¶ added in v0.5.0
ReplaceLatest replaces the current history with the given commands, returns the previous value.
func (*Terminal) ResetInterrupts ¶ added in v0.8.0
If you want to reset and restart after an interrupt, call this.
func (*Terminal) SetAutoCompleteCallback ¶
func (t *Terminal) SetAutoCompleteCallback(f AutoCompleteCallback)
SetAutoCompleteCallback sets the callback called for each key press. Can be used to implement auto completion. See example/main.go for an example.
func (*Terminal) SetAutoHistory ¶ added in v0.5.0
SetAutoHistory enables/disables auto history (default is enabled).
func (*Terminal) SetHistoryFile ¶
Sets up a file to load and save history from/to. File is being read when this is called. If no error is returned, the file will also be automatically updated on Close().
Directories ¶
Path | Synopsis |
---|---|
ansipixel provides terminal drawing and key reading abilities.
|
ansipixel provides terminal drawing and key reading abilities. |
* A more interesting/real example is https://github.com/grol-io/grol * but this demonstrates most of the features of the terminal package.
|
* A more interesting/real example is https://github.com/grol-io/grol * but this demonstrates most of the features of the terminal package. |