ctxshell

package module
v0.0.0-...-3dbd443 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
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) AddHook

func (t *Cshell) AddHook(hook Hook)

func (*Cshell) AddKeyBinding

func (t *Cshell) AddKeyBinding(key rune, fn func() bool) *Cshell

add a key binding painic if the key is already in the list

func (*Cshell) AddNativeCmd

func (t *Cshell) AddNativeCmd(cmd *NativeCmd) *Cshell

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

func (t *Cshell) Error(messages ...string)

Error handler function to register time based messages for the prompt, and executing the Error function if defined

func (*Cshell) GetCurrentMessage

func (t *Cshell) GetCurrentMessage() (bool, Msg)

Gettig the Message from the message buffer that is currently displayed

func (*Cshell) GetHooksByPattern

func (t *Cshell) GetHooksByPattern(pattern string) []Hook

func (*Cshell) GetLastInput

func (t *Cshell) GetLastInput() string

returns the last input

func (*Cshell) GetReadline

func (t *Cshell) GetReadline() *readline.Instance

get the readline instance

func (*Cshell) Message

func (t *Cshell) Message(messages ...string)

Message handler function to register time based messages for the prompt

func (*Cshell) NotifyToPrompt

func (t *Cshell) NotifyToPrompt(message Msg) *Cshell

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

func (t *Cshell) OnErrorFunc(fn func(error)) *Cshell

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

func (t *Cshell) OnShutDownFunc(fn func()) *Cshell

set a function that is called if the shell is shutdown. so here some cleanup can be done.

func (*Cshell) OnUnknownCmdFunc

func (t *Cshell) OnUnknownCmdFunc(fn func(string) error) *Cshell

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

func (t *Cshell) ResizeMessageProvider(size int) *Cshell

this resizes the message buffer

func (*Cshell) Run

func (t *Cshell) Run() error

Run starts the shell

func (*Cshell) RunOnce

func (t *Cshell) RunOnce(cmds []string) error

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 (t *Cshell) RunOnceWithCmd(cmd func()) error

func (*Cshell) SetAsyncCobraExec

func (t *Cshell) SetAsyncCobraExec(b bool) *Cshell

enable or disable the async execution of cobra commands

func (*Cshell) SetAsyncNativeCmd

func (t *Cshell) SetAsyncNativeCmd(b bool) *Cshell

enable or disable the async execution of native commands

func (*Cshell) SetCaptureExitSignal

func (t *Cshell) SetCaptureExitSignal(b bool) *Cshell

enable or disable the capture of the exit signal

func (*Cshell) SetCobraRootCommand

func (t *Cshell) SetCobraRootCommand(cmd *cobra.Command) *Cshell

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

func (t *Cshell) SetExitCmdStr(s string) *Cshell

set the exit command string. this is the command string that exits the shell

func (*Cshell) SetIgnoreCobraCmd

func (t *Cshell) SetIgnoreCobraCmd(cmd ...string) *Cshell

defines commands they are ignored by the cobra command tree

func (*Cshell) SetIgnoreCobraCmds

func (t *Cshell) SetIgnoreCobraCmds(cmds []string) *Cshell

defines commands that are ignored by the cobra command tree

func (*Cshell) SetMessageDisplayTime

func (t *Cshell) SetMessageDisplayTime(d time.Duration) *Cshell

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

func (t *Cshell) SetNeverAsyncCmd(cmd ...string) *Cshell

defines a command that are never executed in a separate goroutine

func (*Cshell) SetNeverAsyncCmds

func (t *Cshell) SetNeverAsyncCmds(cmds []string) *Cshell

defines commands that are never executed in a separate goroutine

func (*Cshell) SetNoMessageDuplication

func (t *Cshell) SetNoMessageDuplication(b bool) *Cshell

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

func (t *Cshell) SetPromptFunc(f func(int) string) *Cshell

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

func (t *Cshell) SetStopOutput(b bool) *Cshell

enable or disable the output to stdout

func (*Cshell) SetTickTimerDuration

func (t *Cshell) SetTickTimerDuration(d time.Duration) *Cshell

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 (t *Cshell) StartMessageProvider() error

func (*Cshell) Stderr

func (t *Cshell) Stderr(messg string)

add a stderr message to the message buffer

func (*Cshell) Stderrln

func (t *Cshell) Stderrln(messg string)

add a stderr message to the message buffer the message is terminated with a newline

func (*Cshell) Stdout

func (t *Cshell) Stdout(messg string)

add message to the message buffer

func (*Cshell) Stdoutln

func (t *Cshell) Stdoutln(messg string)

add a stdout message to the message buffer the message is terminated with a newline

func (*Cshell) Stream

func (t *Cshell) Stream(msg ...interface{})

func (*Cshell) StreamLn

func (t *Cshell) StreamLn(msg ...interface{})

func (*Cshell) Streamf

func (t *Cshell) Streamf(format string, msg ...interface{})

func (*Cshell) TimedMessage

func (t *Cshell) TimedMessage(displayTime time.Duration, messages ...string)

Message handler function to register time based messages for the prompt

func (*Cshell) UpdatePromptEnabled

func (t *Cshell) UpdatePromptEnabled(onoff bool) *Cshell

enable or disable the periodic update of the prompt

func (*Cshell) UpdatePromptPeriod

func (t *Cshell) UpdatePromptPeriod(d time.Duration) *Cshell

set the duration for updating 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 Hook

type Hook struct {
	Before  func() error
	After   func() error
	Pattern string
}

func NewHook

func NewHook(pattern string, before, after func() error) Hook

func (Hook) Match

func (h Hook) Match(pattern string) bool

Match returns true if the pattern matches the hook's pattern by regexp.

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 DefaultPromptMessage(msg string, topic Topic, timeToDisplay time.Duration) Msg

func NewPromptMessage

func NewPromptMessage(msg string, topic Topic, timeToDisplay time.Duration, formatFunc func(string) string) Msg

func SimpleMsgOneSecond

func SimpleMsgOneSecond(msg string, topic Topic) *Msg

func (*Msg) GetMsg

func (m *Msg) GetMsg() string

func (*Msg) GetTimeToDisplay

func (m *Msg) GetTimeToDisplay() time.Duration

func (*Msg) GetTopic

func (m *Msg) GetTopic() Topic

func (*Msg) SetFormatFunc

func (m *Msg) SetFormatFunc() *Msg

type NativeCmd

type NativeCmd struct {
	Name          string
	Help          string
	ExecFunc      func(args []string) error
	CompleterFunc readline.DynamicCompleteFunc
}

func NewNativeCmd

func NewNativeCmd(name, help string, execFunc func(args []string) error) *NativeCmd

func (*NativeCmd) Exec

func (t *NativeCmd) Exec(args []string) error

func (*NativeCmd) GetHelp

func (t *NativeCmd) GetHelp() string

func (*NativeCmd) GetName

func (t *NativeCmd) GetName() string

func (*NativeCmd) SetCompleterFunc

func (t *NativeCmd) SetCompleterFunc(f readline.DynamicCompleteFunc) *NativeCmd

type Topic

type Topic string
const (
	TopicDefault Topic = "default"
	TopicInfo    Topic = "info"
	TopicError   Topic = "error"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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