Documentation ¶
Overview ¶
Package ishell implements an interactive shell.
Index ¶
- type Actions
- type Cmd
- type Context
- type ProgressBar
- type ProgressDisplay
- type ProgressDisplayCharSet
- type ProgressDisplayFunc
- type Shell
- func (s *Shell) Active() bool
- func (s *Shell) AddCmd(cmd *Cmd)
- func (s *Shell) AutoHelp(enable bool)
- func (s *Shell) Close()
- func (s *Shell) CustomCompleter(completer readline.AutoCompleter)
- func (c Shell) Del(key string)
- func (s *Shell) DeleteCmd(name string)
- func (s *Shell) EOF(f func(c *Context))
- func (c Shell) Get(key string) interface{}
- func (s *Shell) IgnoreCase(ignore bool)
- func (s *Shell) Interrupt(f func(c *Context, count int, input string))
- func (c Shell) Keys() (keys []string)
- func (s *Shell) MultiChoiceActive() bool
- func (s *Shell) NotFound(f func(*Context))
- func (s *Shell) Process(args ...string) error
- func (s *Shell) ProgressBar() ProgressBar
- func (s *Shell) RootCmd() *Cmd
- func (s *Shell) Run()
- func (c *Shell) Set(key string, value interface{})
- func (s *Shell) SetHistoryPath(path string)
- func (s *Shell) SetHomeHistoryPath(path string)
- func (s *Shell) SetOut(writer io.Writer)
- func (s *Shell) SetPager(pager string, args []string)
- func (s *Shell) SetRootCmd(cmd *Cmd)
- func (s *Shell) Start()
- func (s *Shell) Wait()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Actions ¶
type Actions interface { // ReadLine reads a line from standard input. ReadLine() string // ReadLineErr is ReadLine but returns error as well ReadLineErr() (string, error) // ReadLineWithDefault reads a line from standard input with default value. ReadLineWithDefault(string) string // ReadPassword reads password from standard input without echoing the characters. // Note that this only works as expected when the standard input is a terminal. ReadPassword() string // ReadPasswordErr is ReadPassword but returns error as well ReadPasswordErr() (string, error) // ReadMultiLinesFunc reads multiple lines from standard input. It passes each read line to // f and stops reading when f returns false. ReadMultiLinesFunc(f func(string) bool) string // ReadMultiLines reads multiple lines from standard input. It stops reading when terminator // is encountered at the end of the line. It returns the lines read including terminator. // For more control, use ReadMultiLinesFunc. ReadMultiLines(terminator string) string // Println prints to output and ends with newline character. Println(val ...interface{}) // Print prints to output. Print(val ...interface{}) // Printf prints to output using string format. Printf(format string, val ...interface{}) // ShowPaged shows a paged text that is scrollable. // This leverages on "less" for unix and "more" for windows. ShowPaged(text string) error // ShowPagedReader shows a paged text that is scrollable, from a reader source. // This leverages on "less" for unix and "more" for windows. ShowPagedReader(r io.Reader) error // MultiChoice presents options to the user. // returns the index of the selection or -1 if nothing is // selected. // text is displayed before the options. MultiChoice(options []string, text string) int // Checklist is similar to MultiChoice but user can choose multiple variants using Space. // init is initially selected options. Checklist(options []string, text string, init []int) []int // SetPrompt sets the prompt string. The string to be displayed before the cursor. SetPrompt(prompt string) // SetMultiPrompt sets the prompt string used for multiple lines. The string to be displayed before // the cursor; starting from the second line of input. SetMultiPrompt(prompt string) // SetMultiChoicePrompt sets the prompt strings used for MultiChoice(). SetMultiChoicePrompt(prompt, spacer string) // SetChecklistOptions sets the strings representing the options of Checklist(). // The generated string depends on SetMultiChoicePrompt() also. SetChecklistOptions(open, selected string) // ShowPrompt sets whether prompt should show when requesting input for ReadLine and ReadPassword. // Defaults to true. ShowPrompt(show bool) // Cmds returns all the commands added to the shell. Cmds() []*Cmd // HelpText returns the computed help of top level commands. HelpText() string // ClearScreen clears the screen. Same behaviour as running 'clear' in unix terminal or 'cls' in windows cmd. ClearScreen() error // Stop stops the shell. This will stop the shell from auto reading inputs and calling // registered functions. A stopped shell is only inactive but totally functional. // Its functions can still be called and can be restarted. Stop() }
Actions are actions that can be performed by a shell.
type Cmd ¶
type Cmd struct { // Command name. Name string // Command name aliases. Aliases []string // Function to execute for the command. Func func(c *Context) // One liner help message for the command. Help string // More descriptive help message for the command. LongHelp string // Completer is custom autocomplete for command. // It takes in command arguments and returns // autocomplete options. // By default all commands get autocomplete of // subcommands. // A non-nil Completer overrides the default behaviour. Completer func(args []string) []string // CompleterWithPrefix is custom autocomplete like // for Completer, but also provides the prefix // already so far to the completion function // If both Completer and CompleterWithPrefix are given, // CompleterWithPrefix takes precedence CompleterWithPrefix func(prefix string, args []string) []string // contains filtered or unexported fields }
Cmd is a shell command handler.
type Context ¶
type Context struct { // Args is command arguments. Args []string // RawArgs is unprocessed command arguments. RawArgs []string // Cmd is the currently executing command. This is empty for NotFound and Interrupt. Cmd Cmd Actions // contains filtered or unexported fields }
Context is an ishell context. It embeds ishell.Actions.
func (Context) Del ¶
func (c Context) Del(key string)
Del deletes key and its value in this context.
func (Context) Get ¶
func (c Context) Get(key string) interface{}
Get returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Get with the same key returns the same result.
func (*Context) ProgressBar ¶
func (c *Context) ProgressBar() ProgressBar
ProgressBar returns the progress bar for the current shell context.
type ProgressBar ¶
type ProgressBar interface { // Display sets the display of the progress bar. Display(ProgressDisplay) // Indeterminate sets the progress bar type // to indeterminate if true or determinate otherwise. Indeterminate(bool) // Interval sets the time between transitions for indeterminate // progress bar. Interval(time.Duration) // SetProgress sets the progress stage of the progress bar. // percent is from between 1 and 100. Progress(percent int) // Prefix sets the prefix for the output. The text to place before // the display. Prefix(string) // Suffix sets the suffix for the output. The text to place after // the display. Suffix(string) // Final sets the string to show after the progress bar is done. Final(string) // Start starts the progress bar. Start() // Stop stops the progress bar. Stop() }
ProgressBar is an ishell progress bar.
type ProgressDisplay ¶
type ProgressDisplay interface { // Determinate returns the strings to display // for percents 0 to 100. Determinate() [101]string // Indeterminate returns the strings to display // at interval. Indeterminate() []string }
ProgressDisplay handles the display string for a progress bar.
type ProgressDisplayCharSet ¶
type ProgressDisplayCharSet []string
ProgressDisplayCharSet is the character set for a progress bar.
func (ProgressDisplayCharSet) Determinate ¶
func (p ProgressDisplayCharSet) Determinate() [101]string
Determinate satisfies ProgressDisplay interface.
func (ProgressDisplayCharSet) Indeterminate ¶
func (p ProgressDisplayCharSet) Indeterminate() []string
Indeterminate satisfies ProgressDisplay interface.
type ProgressDisplayFunc ¶
ProgressDisplayFunc is a convenience function to create a ProgressDisplay. percent is -1 for indeterminate and 0-100 for determinate.
func (ProgressDisplayFunc) Determinate ¶
func (p ProgressDisplayFunc) Determinate() [101]string
Determinate satisfies ProgressDisplay interface.
func (ProgressDisplayFunc) Indeterminate ¶
func (p ProgressDisplayFunc) Indeterminate() []string
Indeterminate satisfies ProgressDisplay interface.
type Shell ¶
type Shell struct { Actions // contains filtered or unexported fields }
Shell is an interactive cli shell.
func New ¶
func New() *Shell
New creates a new shell with default settings. Uses standard output and default prompt ">> ".
func NewWithConfig ¶
NewWithConfig creates a new shell with custom readline config.
func NewWithReadline ¶
NewWithReadline creates a new shell with a custom readline instance.
func (*Shell) AutoHelp ¶
AutoHelp sets if ishell should trigger help message if a command's arg is "help". Defaults to true.
This can be set to false for more control on how help is displayed.
func (*Shell) Close ¶
func (s *Shell) Close()
Close stops the shell (if required) and closes the shell's input. This should be called when done with reading inputs. Unlike `Stop`, a closed shell cannot be restarted.
func (*Shell) CustomCompleter ¶
func (s *Shell) CustomCompleter(completer readline.AutoCompleter)
CustomCompleter allows use of custom implementation of readline.Autocompleter.
func (*Shell) EOF ¶
EOF adds a function to handle End of File input (Ctrl-d). This overrides the default behaviour which terminates the shell.
func (Shell) Get ¶
func (c Shell) Get(key string) interface{}
Get returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Get with the same key returns the same result.
func (*Shell) IgnoreCase ¶
IgnoreCase specifies whether commands should not be case sensitive. Defaults to false i.e. commands are case sensitive. If true, commands must be registered in lower cases.
func (*Shell) Interrupt ¶
Interrupt adds a function to handle keyboard interrupt (Ctrl-c). count is the number of consecutive times that Ctrl-c has been pressed. i.e. any input apart from Ctrl-c resets count to 0.
func (*Shell) MultiChoiceActive ¶
MultiChoiceActive returns true if the shell is in the multi choice selection mode
func (*Shell) NotFound ¶
NotFound adds a generic function for all inputs. It is called if the shell input could not be handled by any of the added commands.
func (*Shell) ProgressBar ¶
func (s *Shell) ProgressBar() ProgressBar
ProgressBar returns the progress bar for the shell.
func (*Shell) Set ¶
func (c *Shell) Set(key string, value interface{})
Set sets the key in this context to value.
func (*Shell) SetHistoryPath ¶
SetHistoryPath sets where readlines history file location. Use an empty string to disable history file. It is empty by default.
func (*Shell) SetHomeHistoryPath ¶
SetHomeHistoryPath is a convenience method that sets the history path in user's home directory.
func (*Shell) SetRootCmd ¶
SetRootCmd sets the shell's root command. Use with caution, this may affect the behaviour of the default completer.