shell

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

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

Go to latest
Published: Jun 26, 2020 License: MIT Imports: 4 Imported by: 0

README

go-shell

Go Report Card Software License

A Library that provides a simple framework for writing line-oriented command interpreters. Inspired primarily by the python cmd module.

This is primarily a wrapper around the go-prompt module that simply makes it easier to use. Providing users with its auto-completion features, history, customization features, and choice of emacs or standard key bindings.

package main

import (
	"fmt"
	shell "gitlab.com/k-more/go-shell"
)

func test(args []string) bool {
	fmt.Println(args)
	return false
}

func test2(args []string) bool {
	fmt.Println(args)
	return true
}

func main() {
	var lol2Args = []shell.Suggestion{
		{Text: "first arg", Description: "first desc"},
		{Text: "second arg", Description: "second desc"},
	}

	var test2OptArgs = map[string]shell.OptArg{
		"-t":     {Description: "an option", Arguments: nil},
		"-l":     {Description: "one more", Arguments: nil},
		"--nextarg":  {Description: "still", Arguments: nil},
		"--anotherone": {Description: "more", Arguments: lol2Args},
	}
	var commands = shell.Command{
		Function:    test2,
		Description: "my other function",
		Options:     test2OptArgs,
	}

	var cmdMap = shell.CommandMap{
		"test":  shell.Command{test, "my function", nil},
		"test2": commands,
	}

	cmdShell := shell.NewShell(cmdMap,
		shell.OptionInputTextColor(shell.Yellow),
		shell.OptionPrefixTextColor(shell.Fuchsia),
		shell.OptionInputBGColor(shell.Blue),
	)

	cmdShell.MainLoop("starting up...\n")

}

Author

Kristopher Bickmore

License

This software is licensed under the MIT license, see LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OptionAddASCIICodeBind = prompt.OptionAddASCIICodeBind

OptionAddASCIICodeBind to set a custom key bind. OptionAddASCIICodeBind(b ...ASCIICodeBind)

View Source
var OptionAddKeyBind = prompt.OptionAddKeyBind

OptionAddKeyBind to set a custom key bind. OptionAddKeyBind(b ...KeyBind)

View Source
var OptionBreakLineCallback = prompt.OptionBreakLineCallback

OptionBreakLineCallback to run a callback at every break line OptionBreakLineCallback(fn func(*Document))

View Source
var OptionCompletionOnDown = prompt.OptionCompletionOnDown

OptionCompletionOnDown allows for Down arrow key to trigger completion. Example: OptionCompletionOnDown()

View Source
var OptionCompletionWordSeparator = prompt.OptionCompletionWordSeparator

OptionCompletionWordSeparator to set word separators. Enable only ' ' if empty.

View Source
var OptionDescriptionBGColor = prompt.OptionDescriptionBGColor

OptionDescriptionBGColor to change a background color of description text in drop down suggestions.

View Source
var OptionDescriptionTextColor = prompt.OptionDescriptionTextColor

OptionDescriptionTextColor to change a background color of description text in drop down suggestions.

View Source
var OptionHistory = prompt.OptionHistory

OptionHistory to set history expressed by string array. Example: OptionHistory([]string{"SELECT * FROM users;"})

View Source
var OptionInitialBufferText = prompt.OptionInitialBufferText

OptionInitialBufferText to set the initial buffer text

View Source
var OptionInputBGColor = prompt.OptionInputBGColor

OptionInputBGColor to change a color of background which is input by user

View Source
var OptionInputTextColor = prompt.OptionInputTextColor

OptionInputTextColor to change a color of text which is input by user

View Source
var OptionLivePrefix = prompt.OptionLivePrefix

OptionLivePrefix to change the prefix dynamically by callback function

View Source
var OptionMaxSuggestion = prompt.OptionMaxSuggestion

OptionMaxSuggestion specify the max number of displayed suggestions. Example: OptionMaxSuggestion(5)

View Source
var OptionParser = prompt.OptionParser

OptionParser to set a custom ConsoleParser object. An argument should implement ConsoleParser interface.

View Source
var OptionPrefix = prompt.OptionPrefix

OptionPrefix to set prefix string.

View Source
var OptionPrefixBackgroundColor = prompt.OptionPrefixBackgroundColor

OptionPrefixBackgroundColor to change a background color of prefix string

View Source
var OptionPrefixTextColor = prompt.OptionPrefixTextColor

OptionPrefixTextColor change a text color of prefix string

View Source
var OptionPreviewSuggestionBGColor = prompt.OptionPreviewSuggestionBGColor

OptionPreviewSuggestionBGColor to change a background color which is completed

View Source
var OptionPreviewSuggestionTextColor = prompt.OptionPreviewSuggestionTextColor

OptionPreviewSuggestionTextColor to change a text color which is completed

View Source
var OptionScrollbarBGColor = prompt.OptionScrollbarBGColor

OptionScrollbarBGColor to change a background color of scrollbar.

View Source
var OptionScrollbarThumbColor = prompt.OptionScrollbarThumbColor

OptionScrollbarThumbColor to change a thumb color on scrollbar.

View Source
var OptionSelectedDescriptionBGColor = prompt.OptionSelectedDescriptionBGColor

OptionSelectedDescriptionBGColor to change a background color of description which is selected inside suggestions drop down box.

View Source
var OptionSelectedDescriptionTextColor = prompt.OptionSelectedDescriptionTextColor

OptionSelectedDescriptionTextColor to change a text color of description which is selected inside suggestions drop down box.

View Source
var OptionSelectedSuggestionBGColor = prompt.OptionSelectedSuggestionBGColor

OptionSelectedSuggestionBGColor to change a background color for completed text which is selected inside suggestions drop down box.

View Source
var OptionSelectedSuggestionTextColor = prompt.OptionSelectedSuggestionTextColor

OptionSelectedSuggestionTextColor to change a text color for completed text which is selected inside suggestions drop down box.

View Source
var OptionShowCompletionAtStart = prompt.OptionShowCompletionAtStart

OptionShowCompletionAtStart to set completion window is open at start.

View Source
var OptionSuggestionBGColor = prompt.OptionSuggestionBGColor

OptionSuggestionBGColor change a background color in drop down suggestions.

View Source
var OptionSuggestionTextColor = prompt.OptionSuggestionTextColor

OptionSuggestionTextColor to change a text color in drop down suggestions.

View Source
var OptionSwitchKeyBindMode = prompt.OptionSwitchKeyBindMode

OptionSwitchKeyBindMode set a key bind mode. select key bindings either "emacs" or "common" Example: OptionSwitchKeyBindMode("common")

View Source
var OptionTitle = prompt.OptionTitle

OptionTitle to set title displayed at the header bar of terminal.

View Source
var OptionWriter = prompt.OptionWriter

OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interface.

View Source
var StringOption func(x string) Option

StringOption is the function type targeting ConsoleParser parameters

Functions

This section is empty.

Types

type ASCIICodeBind

type ASCIICodeBind = prompt.ASCIICodeBind

ASCIICodeBind represents which []byte should do what operation struct{ASCIICode []byte

Fn        KeyBindFunc}

type Color

type Color = prompt.Color

Color represents color on terminal.

const (
	// DefaultColor represents a default color.
	DefaultColor Color = iota

	// Black represents a black.
	Black
	// DarkRed represents a dark red.
	DarkRed
	// DarkGreen represents a dark green.
	DarkGreen
	// Brown represents a brown.
	Brown
	// DarkBlue represents a dark blue.
	DarkBlue
	// Purple represents a purple.
	Purple
	// Cyan represents a cyan.
	Cyan
	// LightGray represents a light gray.
	LightGray

	// DarkGray represents a dark gray.
	DarkGray
	// Red represents a red.
	Red
	// Green represents a green.
	Green
	// Yellow represents a yellow.
	Yellow
	// Blue represents a blue.
	Blue
	// Fuchsia represents a fuchsia.
	Fuchsia
	// Turquoise represents a turquoise.
	Turquoise
	// White represents a white.
	White
)

type ColorOption

type ColorOption func(x Color) Option

ColorOption is the function type to specifically target colors parameters

type Command

type Command struct {
	Function    func(args []string) (quit bool)
	Description string
	Options     map[string]OptArg
}

Command ...

type CommandMap

type CommandMap map[string]Command

CommandMap ...

type ConsoleParser

type ConsoleParser = prompt.ConsoleParser

ConsoleParser ...

type ConsoleWriter

type ConsoleWriter = prompt.ConsoleWriter

ConsoleWriter ...

type Document

type Document = prompt.Document

Document has text displayed in terminal and cursor position.

struct {
	Text string
	// This represents a index in a rune array of Document.Text.
	// So if Document is "日本(cursor)語", cursorPosition is 2.
	// But DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.
	cursorPosition int
	lastKey        Key
}

type ExitChecker

type ExitChecker = prompt.ExitChecker

ExitChecker is called after user input to check if prompt must stop and exit go-prompt Run loop. User input means: selecting/typing an entry, then, if said entry content matches the ExitChecker function criteria: - immediate exit (if breakline is false) without executor called - exit after typing <return> (meaning breakline is true), and the executor is called first, before exit. Exit means exit go-prompt (not the overall Go program) func(in string, breakline bool) bool

type Key

type Key = prompt.Key

Key is the type express the key inserted from user.

const (
	// Escape Key ...
	Escape Key = iota

	ControlA
	ControlB
	ControlC
	ControlD
	ControlE
	ControlF
	ControlG
	ControlH
	ControlI
	ControlJ
	ControlK
	ControlL
	ControlM
	ControlN
	ControlO
	ControlP
	ControlQ
	ControlR
	ControlS
	ControlT
	ControlU
	ControlV
	ControlW
	ControlX
	ControlY
	ControlZ

	ControlSpace
	ControlBackslash
	ControlSquareClose
	ControlCircumflex
	ControlUnderscore
	ControlLeft
	ControlRight
	ControlUp
	ControlDown

	Up
	Down
	Right
	Left

	ShiftLeft
	ShiftUp
	ShiftDown
	ShiftRight

	Home
	End
	Delete
	ShiftDelete
	ControlDelete
	PageUp
	PageDown
	BackTab
	Insert
	Backspace

	// Aliases.
	Tab
	Enter

	F1
	F2
	F3
	F4
	F5
	F6
	F7
	F8
	F9
	F10
	F11
	F12
	F13
	F14
	F15
	F16
	F17
	F18
	F19
	F20
	F21
	F22
	F23
	F24

	// Any Matches any key.
	Any

	// Special
	CPRResponse
	Vt100MouseEvent
	WindowsMouseEvent
	BracketedPaste

	// Key which is ignored. (The key binding for this key should not do anything.)
	Ignore

	// Key is not defined
	NotDefined
)

type KeyBind

type KeyBind = prompt.KeyBind

KeyBind represents which key should do what operation. struct { Key key

Fn KeyBindFunc }

type KeyBindFunc

type KeyBindFunc = prompt.KeyBindFunc

KeyBindFunc receives buffer and processed it. func(*buffer)

type KeyBindMode

type KeyBindMode = prompt.KeyBindMode

KeyBindMode to switch a key binding flexibly. string

const (
	// CommonKeyBind is a mode without any keyboard shortcut
	CommonKeyBind KeyBindMode = "common"
	// EmacsKeyBind is a mode to use emacs-like keyboard shortcut
	EmacsKeyBind KeyBindMode = "emacs"
)

type LivePrefixOption

type LivePrefixOption func(f func() (prefix string, useLivePrefix bool)) Option

LivePrefixOption is a function type for for modifying the shell prefix

type OptArg

type OptArg struct {
	Description string
	Arguments   []Suggestion
}

OptArg ...

type Option

type Option = prompt.Option

Option is the type to replace default parameters. shell.NewShell accepts any number of options (this is functional option pattern).

type ParserOption

type ParserOption func(x ConsoleParser) Option

ParserOption is the function type targeting ConsoleParser parameters

type Shell

type Shell struct {
	Precmd   func(line string)
	Postcmd  func(stop bool, line string)
	Preloop  func()
	Postloop func()
	// contains filtered or unexported fields
}

Shell ...

func NewShell

func NewShell(cmdMap CommandMap, opts ...Option) (shell Shell)

NewShell ...

func (*Shell) AddCommand

func (shell *Shell) AddCommand(name string, command Command)

AddCommand ...

func (*Shell) MainLoop

func (shell *Shell) MainLoop(intro string)

MainLoop ...

type Suggestion

type Suggestion = prompt.Suggest

Suggestion ...

type WriterOption

type WriterOption func(x ConsoleWriter) Option

WriterOption is the function type targeting ConsoleWriter parameters

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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