Documentation ¶
Index ¶
- Constants
- type Cshell
- func (t *Cshell) AddHook(hook Hook)
- func (t *Cshell) AddKeyBinding(key rune, fn func() bool) *Cshell
- func (t *Cshell) AddNativeCmd(cmd *NativeCmd) *Cshell
- func (t *Cshell) ApplyCobraCompletionOnce(newCmd *readline.PrefixCompleter, c *cobra.Command)
- func (t *Cshell) Error(messages ...string)
- func (t *Cshell) GetCurrentMessage() (bool, Msg)
- func (t *Cshell) GetHooksByPattern(pattern string) []Hook
- func (t *Cshell) GetLastInput() string
- func (t *Cshell) GetReadline() *readline.Instance
- func (t *Cshell) Message(messages ...string)
- func (t *Cshell) NotifyToPrompt(message Msg) *Cshell
- func (t *Cshell) OnErrorFunc(fn func(error)) *Cshell
- func (t *Cshell) OnShutDownFunc(fn func()) *Cshell
- func (t *Cshell) OnUnknownCmdFunc(fn func(string) error) *Cshell
- func (t *Cshell) ResizeMessageProvider(size int) *Cshell
- func (t *Cshell) Run() error
- func (t *Cshell) RunOnce(cmds []string) error
- func (t *Cshell) RunOnceWithCmd(cmd func()) error
- func (t *Cshell) SetAsyncCobraExec(b bool) *Cshell
- func (t *Cshell) SetAsyncNativeCmd(b bool) *Cshell
- func (t *Cshell) SetCaptureExitSignal(b bool) *Cshell
- func (t *Cshell) SetCobraRootCommand(cmd *cobra.Command) *Cshell
- func (t *Cshell) SetExitCmdStr(s string) *Cshell
- func (t *Cshell) SetIgnoreCobraCmd(cmd ...string) *Cshell
- func (t *Cshell) SetIgnoreCobraCmds(cmds []string) *Cshell
- func (t *Cshell) SetMessageDisplayTime(d time.Duration) *Cshell
- func (t *Cshell) SetNeverAsyncCmd(cmd ...string) *Cshell
- func (t *Cshell) SetNeverAsyncCmds(cmds []string) *Cshell
- func (t *Cshell) SetNoMessageDuplication(b bool) *Cshell
- func (t *Cshell) SetPromptFunc(f func(int) string) *Cshell
- func (t *Cshell) SetStopOutput(b bool) *Cshell
- func (t *Cshell) SetTickTimerDuration(d time.Duration) *Cshell
- func (t *Cshell) StartBackgroundPromptUpate()
- func (t *Cshell) StartMessageProvider() error
- func (t *Cshell) Stderr(messg string)
- func (t *Cshell) Stderrln(messg string)
- func (t *Cshell) Stdout(messg string)
- func (t *Cshell) Stdoutln(messg string)
- func (t *Cshell) Stream(msg ...interface{})
- func (t *Cshell) StreamLn(msg ...interface{})
- func (t *Cshell) Streamf(format string, msg ...interface{})
- func (t *Cshell) TimedMessage(displayTime time.Duration, messages ...string)
- func (t *Cshell) UpdatePromptEnabled(onoff bool) *Cshell
- func (t *Cshell) UpdatePromptPeriod(d time.Duration) *Cshell
- type CshellMessage
- type CshellMsgFifo
- type Hook
- type KeyFunc
- type Msg
- type NativeCmd
- type Topic
Constants ¶
const ( UpdateByInit = 1001 UpdateByPeriod = 1002 UpdateBySignal = 1003 UpdateByInput = 1004 UpdateByNotify = 1005 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cshell ¶
type Cshell struct { StopOutput bool // stop printing the output to stdout // contains filtered or unexported fields }
func NewCshell ¶
func NewCshell() *Cshell
NewCshell creates a new cshell instance with default values. use the setter functions to change the default values before calling Run()
func (*Cshell) AddKeyBinding ¶
add a key binding painic if the key is already in the list
func (*Cshell) AddNativeCmd ¶
add a native command to the shell. native commands are just a wrapper
func (*Cshell) ApplyCobraCompletionOnce ¶
func (t *Cshell) ApplyCobraCompletionOnce(newCmd *readline.PrefixCompleter, c *cobra.Command)
ApplyCobraCompletionOnce applies the completer function of the cobra command only once. this is necessary because the completer function is called every time the user hits the tab key. so just read the valid args once and add them to the completer item. that means, if somehow the valid args are changing, the user have to restart the shell.
func (*Cshell) Error ¶
Error handler function to register time based messages for the prompt, and executing the Error function if defined
func (*Cshell) GetCurrentMessage ¶
Gettig the Message from the message buffer that is currently displayed
func (*Cshell) GetHooksByPattern ¶
func (*Cshell) GetReadline ¶
get the readline instance
func (*Cshell) NotifyToPrompt ¶
notify the prompt to display a message. the message is displayed for the time defined by SetMessageDisplayTime() this is also used internally to display messages nd errors
func (*Cshell) OnErrorFunc ¶
set a function that is called if an error occurs. this means any error that is not handled by the shell itself.
func (*Cshell) OnShutDownFunc ¶
set a function that is called if the shell is shutdown. so here some cleanup can be done.
func (*Cshell) OnUnknownCmdFunc ¶
set a function that is called if the user enters an unknown command. this is useful if you want to implement a fallback for unknown commands.
func (*Cshell) ResizeMessageProvider ¶
this resizes the message buffer
func (*Cshell) RunOnce ¶
RunOnce executes the given commands and exits the shell afterwards. this is useful if you want to execute a command from a script.
func (*Cshell) RunOnceWithCmd ¶
func (*Cshell) SetAsyncCobraExec ¶
enable or disable the async execution of cobra commands
func (*Cshell) SetAsyncNativeCmd ¶
enable or disable the async execution of native commands
func (*Cshell) SetCaptureExitSignal ¶
enable or disable the capture of the exit signal
func (*Cshell) SetCobraRootCommand ¶
set the cobra root command. this is the root of the cobra command tree it is used to parse the command line and execute the commands, and to get the flags and arguments for the completer
func (*Cshell) SetExitCmdStr ¶
set the exit command string. this is the command string that exits the shell
func (*Cshell) SetIgnoreCobraCmd ¶
defines commands they are ignored by the cobra command tree
func (*Cshell) SetIgnoreCobraCmds ¶
defines commands that are ignored by the cobra command tree
func (*Cshell) SetMessageDisplayTime ¶
set the duration for displaying a message. this is ment for messages that are displayed inside the prompt. as long a message is displayed, the prompt will get updated with status UpdateByNotify. in this case the message could received by using GetCurrentMessage() where you get a bool if a message is currently displayed and the message itself. example:
shell := ctxshell.NewCshell() shell.SetMessageDisplayTime(5 * time.Second) shell.SetPromptFunc(func(reason int) string { if reason == ctxshell.UpdateByNotify { if haveAnMessage, msg := shell.GetCurrentMessage(); haveAnMessage { return "info:[" + msg.GetMsg() + "] > " } } return " prompt > " }) shell.Run()
this duration is setting the time a message is displayed. after this time the message is gone and the next message from the buffer is displayed. until any messages are handled. this is ment for having the messages displayed in a environment, that is constantly printing something to stdout, so it would be hard to see the messages in the stdout.
func (*Cshell) SetNeverAsyncCmd ¶
defines a command that are never executed in a separate goroutine
func (*Cshell) SetNeverAsyncCmds ¶
defines commands that are never executed in a separate goroutine
func (*Cshell) SetNoMessageDuplication ¶
SetNoMessageDuplication enables or disables the duplication of messages. here it is all about internal messages that are displayed in the prompt. this notification messages. that means, if a message is already in the message buffer, it is not added again. this is useful if you want to avoid that the same message is displayed multiple times what is not really useful at some point. but on default we do not set this behavior, so any message is populated to the message buffer. also keep in mind, that this will only affect the message buffer. so it is still possible to display the same message multiple times. it just reduces spamming the message buffer.
func (*Cshell) SetPromptFunc ¶
set the prompt function. this function is called every time the prompt is printed. so you can change the prompt string dynamically
func (*Cshell) SetStopOutput ¶
enable or disable the output to stdout
func (*Cshell) SetTickTimerDuration ¶
this sets the duration of the tick timer for print the buffered messages this is required because the readline instance is not thread safe. so we have to collect any output from any threads and print it in the main thread synchronously
func (*Cshell) StartBackgroundPromptUpate ¶
func (t *Cshell) StartBackgroundPromptUpate()
this is the promt update loop it updates the prompt every updatePromptDuration use UpdatePromptPeriod to set the updatePromptDuration use UpdatePromptEnabled to enable or disable the prompt update
func (*Cshell) StartMessageProvider ¶
func (*Cshell) Stderrln ¶
add a stderr message to the message buffer the message is terminated with a newline
func (*Cshell) Stdoutln ¶
add a stdout message to the message buffer the message is terminated with a newline
func (*Cshell) TimedMessage ¶
Message handler function to register time based messages for the prompt
func (*Cshell) UpdatePromptEnabled ¶
enable or disable the periodic update of the prompt
type CshellMessage ¶
type CshellMessage struct { MsgType string // "stdout", "stderr", "info", "error" Msg string Time time.Time }
func NewCshellMessage ¶
func NewCshellMessage(msgType, msg string) *CshellMessage
type CshellMsgFifo ¶
type CshellMsgFifo struct {
// contains filtered or unexported fields
}
func NewCshellMsgScope ¶
func NewCshellMsgScope(size int) *CshellMsgFifo
func (*CshellMsgFifo) Close ¶
func (t *CshellMsgFifo) Close()
func (*CshellMsgFifo) Flush ¶
func (t *CshellMsgFifo) Flush()
func (*CshellMsgFifo) FlushAndClose ¶
func (t *CshellMsgFifo) FlushAndClose()
func (*CshellMsgFifo) GetAllMessages ¶
func (t *CshellMsgFifo) GetAllMessages() []*CshellMessage
func (*CshellMsgFifo) Pop ¶
func (t *CshellMsgFifo) Pop() *CshellMessage
func (*CshellMsgFifo) Push ¶
func (t *CshellMsgFifo) Push(msgType, msg string)
func (*CshellMsgFifo) Size ¶
func (t *CshellMsgFifo) Size() int
type KeyFunc ¶
type KeyFunc struct { Key rune // what key to bind Fn func() bool // what function to call. returning false means do populate the key }
key binding struct
type Msg ¶
type Msg struct {
// contains filtered or unexported fields
}
func DefaultPromptMessage ¶
func NewPromptMessage ¶
func SimpleMsgOneSecond ¶
func (*Msg) GetTimeToDisplay ¶
func (*Msg) SetFormatFunc ¶
type NativeCmd ¶
type NativeCmd struct { Name string Help string ExecFunc func(args []string) error CompleterFunc readline.DynamicCompleteFunc }
func NewNativeCmd ¶
func (*NativeCmd) SetCompleterFunc ¶
func (t *NativeCmd) SetCompleterFunc(f readline.DynamicCompleteFunc) *NativeCmd