Documentation
¶
Index ¶
- Constants
- Variables
- func Black(msg string) string
- func Blue(msg string) string
- func Cyan(msg string) string
- func Green(msg string) string
- func Magenta(msg string) string
- func Prompt()
- func Red(msg string) string
- func SetColor(msg string, conf, bg, text int) string
- func White(msg string) string
- func Yellow(msg string) string
- type ApiHandleFunc
- type Client
- type Config
- type Console
- func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, string)
- func (c *Console) Evaluate(statement string)
- func (c *Console) Interactive()
- func (c *Console) Print(msg string, opt ...interface{})
- func (c *Console) RegisterHandler(api, name string, handler ApiHandleFunc)
- func (c *Console) Stop(graceful bool) error
- func (c *Console) StopInteractive()
- func (c *Console) Welcome()
- type UserPrompter
- type WordCompleter
Constants ¶
const ( TextBlack = iota + 30 TextRed TextGreen TextYellow TextBlue TextMagenta TextCyan TextWhite )
const DefaultPrompt = "> "
DefaultPrompt is the default prompt line prefix to use for user input querying.
const (
ErrorNoHandlerCode = global.ErrorCodeConsoleBase + 1 + iota
)
const HistoryFile = "history"
HistoryFile is the file within the data directory to store input scrollback.
Variables ¶
var ( Newline = []byte{'\n'} Space = []byte{' '} CmdSplit = " " )
var ErrorNoHandler = func(msg string) *model.ErrorModel { return model.NewErrModelByStr(ErrorNoHandlerCode, "no handler: [%v]", msg) }
var Stdin = newTerminalPrompter()
Stdin holds the stdin line reader (also using stdout for printing prompts). Only this reader may be used for input because it keeps an internal buffer.
Functions ¶
Types ¶
type ApiHandleFunc ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) RegisterApiHandler ¶
func (c *Client) RegisterApiHandler(api, name string, f ApiHandleFunc)
type Config ¶
type Config struct {
DataDir string // Data directory to store the console history at
DocRoot string // Filesystem path from where to load JavaScript files from
Prompt string // Input prompt prefix string (defaults to DefaultPrompt)
Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter)
Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout)
Preload []string // Absolute paths to JavaScript files to preload
DefaultCategory string // the default cmd category
}
func NewConsoleConfig ¶
func NewConsoleConfig() Config
type Console ¶
type Console struct {
// contains filtered or unexported fields
}
func (*Console) AutoCompleteInput ¶
AutoCompleteInput is a pre-assembled word completer to be used by the user input prompter to provide hints to the user about the methods available.
func (*Console) Evaluate ¶
Evaluate executes code and pretty prints the result to the specified output stream.
func (*Console) Interactive ¶
func (c *Console) Interactive()
Interactive starts an interactive user session, where input is prompted from the configured user prompter.
func (*Console) RegisterHandler ¶
func (c *Console) RegisterHandler(api, name string, handler ApiHandleFunc)
func (*Console) StopInteractive ¶
func (c *Console) StopInteractive()
StopInteractive causes Interactive to return as soon as possible.
type UserPrompter ¶
type UserPrompter interface {
// PromptInput displays the given prompt to the user and requests some textual
// data to be entered, returning the input of the user.
PromptInput(prompt string) (string, error)
// PromptPassword displays the given prompt to the user and requests some textual
// data to be entered, but one which must not be echoed out into the terminal.
// The method returns the input provided by the user.
PromptPassword(prompt string) (string, error)
// PromptConfirm displays the given prompt to the user and requests a boolean
// choice to be made, returning that choice.
PromptConfirm(prompt string) (bool, error)
// SetHistory sets the input scrollback history that the prompter will allow
// the user to scroll back to.
SetHistory(history []string)
// AppendHistory appends an entry to the scrollback history. It should be called
// if and only if the prompt to append was a valid command.
AppendHistory(command string)
// ClearHistory clears the entire history
ClearHistory()
// SetWordCompleter sets the completion function that the prompter will call to
// fetch completion candidates when the user presses tab.
SetWordCompleter(completer WordCompleter)
}
UserPrompter defines the methods needed by the console to prompt the user for various types of inputs.
type WordCompleter ¶
WordCompleter takes the currently edited line with the cursor position and returns the completion candidates for the partial word to be completed. If the line is "Hello, wo!!!" and the cursor is before the first '!', ("Hello, wo!!!", 9) is passed to the completer which may returns ("Hello, ", {"world", "Word"}, "!!!") to have "Hello, world!!!".