Documentation ¶
Index ¶
- Constants
- type Commands
- type Console
- func (c *Console) ActiveMenu() *Menu
- func (c *Console) HideCommands(filters ...string)
- func (c *Console) Menu(name string) *Menu
- func (c *Console) NewMenu(name string) *Menu
- func (c *Console) Printf(msg string, args ...any) (n int, err error)
- func (c *Console) SetPrintLogo(f func(c *Console))
- func (c *Console) Shell() *readline.Shell
- func (c *Console) ShowCommands(filters ...string)
- func (c *Console) Start() error
- func (c *Console) SwitchMenu(menu string)
- func (c *Console) SystemEditor(buffer []byte, filetype string) ([]byte, error)
- func (c *Console) TransientPrintf(msg string, args ...any) (n int, err error)
- type Menu
- func (m *Menu) ActiveFiltersFor(cmd *cobra.Command) []string
- func (m *Menu) AddHistorySource(name string, source readline.History)
- func (m *Menu) AddHistorySourceFile(name string, filepath string)
- func (m *Menu) AddInterrupt(err error, handler func(c *Console))
- func (m *Menu) CheckIsAvailable(cmd *cobra.Command) error
- func (m *Menu) DelInterrupt(errs ...error)
- func (m *Menu) DeleteHistorySource(name string)
- func (m *Menu) Name() string
- func (m *Menu) Printf(msg string, args ...any) (n int, err error)
- func (m *Menu) Prompt() *Prompt
- func (m *Menu) RunCommandArgs(args []string) (err error)
- func (m *Menu) RunCommandLine(line string) (err error)
- func (m *Menu) SetCommands(cmds Commands)
- func (m *Menu) SetErrFilteredCommandTemplate(s string)
- func (m *Menu) TransientPrintf(msg string, args ...any) (n int, err error)
- type Prompt
Constants ¶
const ( // CommandFilterKey should be used as a key to in a cobra.Annotation map. // The value will be used as a filter to disable commands when the console // calls the Filter("name") method on the console. // The string value will be comma-splitted, with each split being a filter. CommandFilterKey = "console-hidden" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Commands ¶
Commands is a simple function a root cobra command containing an arbitrary tree of subcommands, along with any behavior parameters normally found in cobra. This function is used by each menu to produce a new, blank command tree after each execution run, as well as each command completion invocation.
type Console ¶
type Console struct { // Leave an empty line before executing the command. NewlineBefore bool // Leave an empty line after executing the command. // Note that if you also want this newline to be used when logging messages // with TransientPrintf(), Printf() calls, you should leave this to false, // and add a leading newline to your prompt instead: the readline shell will // know how to handle it in all situations. NewlineAfter bool // PreReadlineHooks - All the functions in this list will be executed, // in their respective orders, before the console starts reading // any user input (ie, before redrawing the prompt). PreReadlineHooks []func() error // PreCmdRunLineHooks - Same as PreCmdRunHooks, but will have an effect on the // input line being ultimately provided to the command parser. This might // be used by people who want to apply supplemental, specific processing // on the command input line. PreCmdRunLineHooks []func(args []string) ([]string, error) // PreCmdRunHooks - Once the user has entered a command, but before executing // the target command, the console will execute every function in this list. // These hooks are distinct from the cobra.PreRun() or OnInitialize hooks, // and might be used in combination with them. PreCmdRunHooks []func() error // PostCmdRunHooks are run after the target cobra command has been executed. // These hooks are distinct from the cobra.PreRun() or OnFinalize hooks, // and might be used in combination with them. PostCmdRunHooks []func() error // contains filtered or unexported fields }
Console is an integrated console application instance.
func New ¶
New - Instantiates a new console application, with sane but powerful defaults. This instance can then be passed around and used to bind commands, setup additional things, print asynchronous messages, or modify various operating parameters on the fly. The app parameter is an optional name of the application using this console.
func (*Console) ActiveMenu ¶
ActiveMenu - Return the currently used console menu.
func (*Console) HideCommands ¶
HideCommands - Commands, in addition to their menus, can be shown/hidden based on a filter string. For example, some commands applying to a Windows host might be scattered around different groups, but, having all the filter "windows". If "windows" is used as the argument here, all windows commands for the current menu are subsequently hidden, until ShowCommands("windows") is called.
func (*Console) NewMenu ¶
NewMenu - Create a new command menu, to which the user can attach any number of commands (with any nesting), as well as some specific items like history sources, prompt configurations, sets of expanded variables, and others.
func (*Console) Printf ¶
Printf prints a string message (a log, or more broadly, an asynchronous event) below the current prompt. The message is printed regardless of the current menu.
If this function is called while a command is running, the console will simply print the log below the line, and will not print the prompt. In any other case this function works normally.
func (*Console) SetPrintLogo ¶
SetPrintLogo - Sets the function that will be called to print the logo.
func (*Console) Shell ¶
Shell returns the console readline shell instance, so that the user can further configure it or use some of its API for lower-level stuff.
func (*Console) ShowCommands ¶
ShowCommands - Commands, in addition to their menus, can be shown/hidden based on a filter string. For example, some commands applying to a Windows host might be scattered around different groups, but, having all the filter "windows". Use this function if you have previously called HideCommands("filter") and want these commands to be available back under their respective menu.
func (*Console) Start ¶
Start - Start the console application (readline loop). Blocking. The error returned will always be an error that the console application does not understand or cannot handle.
func (*Console) SwitchMenu ¶
SwitchMenu - Given a name, the console switches its command menu: The next time the console rebinds all of its commands, it will only bind those that belong to this new menu. If the menu is invalid, i.e that no commands are bound to this menu name, the current menu is kept.
func (*Console) SystemEditor ¶
SystemEditor - This function is a renamed-reexport of the underlying readline.StartEditorWithBuffer function, which enables you to conveniently edit files/buffers from within the console application. Naturally, the function will block until the editor is exited, and the updated buffer is returned. The filename parameter can be used to pass a specific filename.ext pattern, which might be useful if the editor has builtin filetype plugin functionality.
func (*Console) TransientPrintf ¶
TransientPrintf prints a string message (a log, or more broadly, an asynchronous event) without bothering the user, displaying the message and "pushing" the prompt below it. The message is printed regardless of the current menu.
If this function is called while a command is running, the console will simply print the log below the line, and will not print the prompt. In any other case this function works normally.
type Menu ¶
type Menu struct { // The root cobra command/parser is the one returned by the handler provided // through the `menu.SetCommands()` function. This command is thus renewed after // each command invocation/execution. // You can still use it as you want, for instance to introspect the current command // state of your menu. *cobra.Command // contains filtered or unexported fields }
Menu - A menu is a simple way to seggregate commands based on the environment to which they belong. For instance, when using a menu specific to some host/user, or domain of activity, commands will vary.
func (*Menu) ActiveFiltersFor ¶ added in v0.1.7
ActiveFiltersFor returns all the active menu filters that a given command does not declare as compliant with (added with console.Hide/ShowCommand()).
func (*Menu) AddHistorySource ¶
AddHistorySource adds a source of history commands that will be accessible to the shell when the menu is active.
func (*Menu) AddHistorySourceFile ¶
AddHistorySourceFile adds a new source of history populated from and writing to the specified "filepath" parameter. On the first call to this function, the default in-memory history source is removed.
func (*Menu) AddInterrupt ¶
AddInterrupt registers a handler to run when the console receives a given interrupt error from the underlying readline shell. Mainly two interrupt signals are concerned: io.EOF (returned when pressing CtrlD), and console.ErrCtrlC. Many will want to use this to switch menus. Note that these interrupt errors only work when the console is NOT currently executing a command, only when reading input.
func (*Menu) CheckIsAvailable ¶ added in v0.1.7
CheckIsAvailable checks if a target command is marked as filtered by the console application registered/and or active filters (added with console.Hide/ShowCommand()). If filtered, returns a template-formatted error message showing the list of incompatible filters. If not filtered, no error is returned.
func (*Menu) DelInterrupt ¶
DelInterrupt removes one or more interrupt handlers from the menu registered ones. If no error is passed as argument, all handlers are removed.
func (*Menu) DeleteHistorySource ¶
DeleteHistorySource removes a history source from the menu. This normally should only be used in two cases: - You want to replace the default in-memory history with another one. - You want to replace one of your history sources for some reason.
func (*Menu) Printf ¶
Printf prints a message to the console, but only if the current menu is active. If the menu is not active, the message is buffered and will be printed the next time the menu is active.
Unlike TransientPrintf, this function will not print the message above the current prompt, but will instead print it below it.
If this function is called while a command is running, the console will simply print the log below the current line, and will not print the prompt. In any other case this function will work normally.
func (*Menu) RunCommandArgs ¶ added in v0.1.7
RunCommandArgs is a convenience function to run a command line in a given menu. After running, the menu's commands are reset, and the prompts reloaded, therefore mostly mimicking the behavior that is the one of the normal readline/run/readline workflow. Although state segregation is a priority for this library to be ensured as much as possible, you should be cautious when using this function to run commands.
func (*Menu) RunCommandLine ¶ added in v0.1.7
RunCommandLine is the equivalent of menu.RunCommandArgs(), but accepts an unsplit command line to execute. This line is split and processed in *sh-compliant form, identically to how lines are in normal console usage.
func (*Menu) SetCommands ¶
SetCommands requires a function returning a tree of cobra commands to be used.
func (*Menu) SetErrFilteredCommandTemplate ¶ added in v0.1.7
SetErrFilteredCommandTemplate sets the error template to be used when a called command can't be executed because it's mark filtered.
func (*Menu) TransientPrintf ¶
TransientPrintf prints a message to the console, but only if the current menu is active. If the menu is not active, the message is buffered and will be printed the next time the menu is active.
The message is printed as a transient message, meaning that it will be printed above the current prompt, effectively "pushing" the prompt down.
If this function is called while a command is running, the console will simply print the log below the current line, and will not print the prompt. In any other case this function will work normally.
type Prompt ¶
type Prompt struct { Primary func() string // Primary is the main prompt. Secondary func() string // Secondary is the prompt used when the user is typing a multi-line command. Transient func() string // Transient is used if the console shell is configured to be transient. Right func() string // Right is the prompt printed on the right side of the screen. Tooltip func(word string) string // Tooltip is used to hint on the root command, replacing right prompts if not empty. // contains filtered or unexported fields }
Prompt - A prompt is a set of functions that return the strings to print for each prompt type. The console will call these functions to retrieve the prompt strings to print. Each menu has its own prompt.