prompt

package module
v0.0.0-...-b542729 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2017 License: MIT Imports: 15 Imported by: 0

README

Prompt

Build Status GoDoc Coverage Status

Prompt is a library for adding shell like interfaces to command line applications. It's intended to be similar to the interface used by some networking equipment.

asciicast

Features

Prompt supports:

  • built-in command completion
  • history
  • context sensitive completion
  • command sets
  • command output to a file
  • command output filtering (e.g. 'grep')

License

MIT

Warning

This library should be considered unstable for now. Interfaces may change, and your code may break as I add features and re-think existing features.

Documentation

Overview

Package prompt provides an interactive command line interface with customizable completion of commands using github.com/peterh/liner as the line editor.

It's meant to be similar to the command line interface on some networking equipment.

Index

Constants

This section is empty.

Variables

View Source
var CompleteDirectory = completeFileDir(dirsOnly)

CompleteDirectory is a pre-defined completer that is used to complete a directory name.

View Source
var CompleteFile = completeFileDir(filesOnly)

CompleteFile is a pre-defined completer that is used to complete a file name.

View Source
var CompleteFileOrDirectory = completeFileDir(filesAndDirs)

CompleteFileOrDirectory is a pre-defined completer that is used to complete a file or directory name.

Functions

func Grep

func Grep(r io.Reader, w io.Writer, args []string)

Grep is a very simple grep.

Types

type Command

type Command func(w io.Writer, args []string)

Command is a function representing a command to be executed. Arguments are passed as args, and all output should be written to w to allow for filtering.

func PopCommandSet

func PopCommandSet(p *Prompt) Command

PopCommandSet returns a command that enters a leaves the current command set.

func PushCommandSet

func PushCommandSet(p *Prompt, name string) Command

PushCommandSet returns a command that enters a named command set.

func PushCommandSetArg

func PushCommandSetArg(p *Prompt) Command

PushCommandSetArg returns a command that enters the command set passed in as an argument. It should be registered with a description like "enter $1" to specify that it takes a single argument.

type CommandSet

type CommandSet struct {
	// contains filtered or unexported fields
}

CommandSet is a set of commands, usually related. Command sets can be switched between by registering commands that call PushCommandSet/PopCommandSet on the Prompt.

func (*CommandSet) RegisterCommandFunc

func (cs *CommandSet) RegisterCommandFunc(desc string, fn Command) error

RegisterCommandFunc registers a command. The description syntax is:

cmd [arg1 arg2...] [$n:[completionType] ...] [$*:[completionType]]

$n - n is a digit, and it matches a single argument. This can be used to reorder arguments (e.g. "foo $2 $1", when called with "foo a b", will have []args{"b","a"}) when the command is executed.)

$* - wildcard, matches all arguments to the end of the line

type Completer

type Completer func(arg string) []string

Completer is used to provide context-sensitive completions for placeholders. Completers are registered, and then are called retrieve completions for the registered placeholder types.

type Filter

type Filter func(r io.Reader, w io.Writer, args []string)

Filter is the type of function used for filtering command output. They should synchronously read from r, apply any filtering logic and write the result to the w.

type Prompt

type Prompt struct {
	LineState *liner.State  // the liner used for input, visibile to allow direct manipulation/changes
	Prompter  func() string // Prompt is the function called to return the prompt
	// contains filtered or unexported fields
}

Prompt is the user prompt.

func NewPrompt

func NewPrompt() *Prompt

NewPrompt returns a newly initialized prompt.

func (*Prompt) Close

func (p *Prompt) Close() error

Close closes and cleans up the prompt.

func (*Prompt) CurrentCommandSet

func (p *Prompt) CurrentCommandSet() *CommandSet

CurrentCommandSet returns the current command set in use.

func (*Prompt) NewCommandSet

func (p *Prompt) NewCommandSet(name string) *CommandSet

NewCommandSet constructs and registers a new command set.

func (*Prompt) PopCommandSet

func (p *Prompt) PopCommandSet() error

PopCommandSet removes the latest command set pushed.

func (*Prompt) Prompt

func (p *Prompt) Prompt() bool

Prompt prompts the user and returns input.

func (*Prompt) PushCommandSet

func (p *Prompt) PushCommandSet(name string) error

PushCommandSet enters a new command set and pushes it to the stack.

func (*Prompt) RegisterCompleter

func (p *Prompt) RegisterCompleter(name string, fn Completer) error

RegisterCompleter registers a function to be used for context sensitive completion of command placeholders.

func (*Prompt) RegisterFilter

func (p *Prompt) RegisterFilter(name string, fn Filter) error

RegisterFilter registers a filter for use by the user. In the case:

cmd | foo arg1 arg2

'foo' is the filter name, and []string{"arg1","arg2"} would be passed to the filter.

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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