Documentation
¶
Overview ¶
Package repl is the interactive shell of hail: the loop that shows a prompt, reads a command, runs it and shows the next prompt.
The package ports what brush-interactive does around its line editors. rline replaces those editors, and transit, with its bash grammar, colors the line and finds what the cursor completes (D5). The shell itself, in package interp, expands the prompts, keeps the history, runs the commands and gives the completions. This package joins the two.
Two readers ¶
When standard input and standard error are both terminals, rline reads each command, with editing, the history, completion and colors. Enter ends the command only when the parser says that it is complete, after it expands the aliases, so a command of several lines comes back at once, and rline draws PS2 at the start of each line after the first.
Otherwise, such as when a pipe feeds hail -i, the shell reads one byte at a time from its descriptor 0, so that a command that reads standard input finds the rest of it, as in bash. The prompts and an echo of each line go to standard error, as readline writes them when it cannot edit.
Key bindings ¶
rline reads the defaults of readline and the inputrc file of the user. The terminal reader is also the interp.KeyBinder of the shell, so that bind, set -o vi and the key bindings of config.yaml change the keys of rline, through its methods Bind, Unbind, AddCommand and SetVariable (rline's D76). It adds, as commands of rline, the functions of bash that readline does not have and that hail can run (keys.go): the five that complete a file, a command, a variable, a user or a host, and the five that list them, through a completer of their own (rline's D80), the three of patterns, dynamic-complete-history, dabbrev-expand, the five of shell words, which kill onto the kill ring (rline's D83, words.go), display-shell-version, which writes below the line (rline's D85), and edit-and-execute-command and vi-edit-and-execute-command, which run the editor and the command after rline accepts the line and leaves raw mode (rline's D85). A key that completes lists the matches on the next press after a press that completed nothing, as in bash (rline's D84). A shell command of bind -x has the terminal while it runs (rline's D81), and sees the mark and the numeric argument (rline's D82).
Signals ¶
The shell receives its signals itself, through interp.Shell.HandleSignals, which main calls: an interactive shell ignores SIGTERM and SIGQUIT, and Ctrl-C abandons the command list that runs. With job control, which interp.Shell.SetTerminal turns on, Ctrl-Z stops the job in the foreground. Before each prompt, interp.Shell.BeforePrompt prints the jobs that ended or stopped.
While rline waits for a key, the shell runs what bash runs while readline waits: the trap of a signal that readline catches, such as SIGWINCH, and with set -b the notice of a job that ended. The shell asks for it through interp.Shell.SetPromptWaker, and rline runs interp.Shell.AtPrompt on the goroutine of ReadLine and draws the line again below (rline's D87). A trap of any other signal, such as SIGUSR1, runs before the next command.
Index ¶
- Constants
- Variables
- func BundledStyle(name string) (*styles.Style, bool)
- func IsStylePath(value string) bool
- func LoadStyle(value string, read func(path string) ([]byte, error)) (*styles.Style, error)
- func Preview(w io.Writer, style *styles.Style, overrides map[string]string, color bool) error
- func Run(sh *interp.Shell, cfg Config) int
- func StyleNames() []string
- type Config
Constants ¶
const DefaultStyle = "onedark"
DefaultStyle is the name of the style that colors the line when config.yaml names none.
Variables ¶
var ErrNoStyle = errors.New("no style has this name")
ErrNoStyle is the error of a name that no bundled style has.
Functions ¶
func BundledStyle ¶
BundledStyle returns the bundled style of the name, such as "dracula". It returns false when hail has no style of that name.
The style holds the entries of the capture names of the bash highlight query only. bundled.go holds those entries for each style of the module styles of transit, so that hail does not link the whole module, which holds 600 KB of JSON.
func IsStylePath ¶
IsStylePath reports whether the value of a style names a file and not a bundled style: it holds a slash or ends in ".json". The name of a bundled style has neither.
func LoadStyle ¶
LoadStyle returns the style that value names: the style file at a path, when IsStylePath says so, or else the bundled style of a name. read reads the file at a path.
func Preview ¶
Preview writes the name of the style and a sample of bash in its colors to w, as the line editor draws the line. The overrides apply as they apply to the line. With color false, or when w is not a terminal, the sample has no colors. rline draws the colors, so a terminal of 256 or 16 colors and NO_COLOR give what they give on the line.
func Run ¶
Run runs the interactive shell sh until it ends, and returns the status that hail exits with. The caller sets sh up first: SetInteractive, the startup files and LoadHistory.
As in bash, the shell reads each line with the line editor while set -o emacs or set -o vi is on, and reads plain lines while both are off, as after --noediting or set +o emacs. It starts the editor at the first line that it edits.
func StyleNames ¶
func StyleNames() []string
StyleNames returns the names of the bundled styles, sorted.
Types ¶
type Config ¶
type Config struct {
// Stdin is the terminal that the user types on, and Stderr the
// terminal that the prompts go to, as bash writes them to standard
// error. When either is not a terminal, or set -o emacs and set -o vi
// are off, as after --noediting, the shell reads plain lines from its
// descriptor 0.
Stdin, Stderr *os.File
// Terminal is a terminal that the host gives, such as xterm.js in a
// web page, where there is no file to read keys from. When it is set,
// the line editor reads keys from it and draws on it, and Stdin and
// Stderr are not used (rline's D79).
Terminal rline.Terminal
// Color colors the line as the user types it.
Color bool
// Vi starts the editor in vi mode, as set -o vi does.
Vi bool
// Style is the style of transit that colors the line. nil colors it in
// DefaultStyle.
Style *styles.Style
// Styles maps the name of a capture of the highlight query of transit,
// such as "keyword" or "string", to the style that draws it: a style
// string of transit, such as "bold #c678dd", or the spec of a style of
// rline, such as "bold ansi-fuchsia". It wins over Style. A name also
// applies to each capture name that it starts, before a dot.
Styles map[string]string
// InputrcFile is the inputrc file to read in place of the user's, as
// $INPUTRC names it. Empty reads the file of the user, as readline
// does.
InputrcFile string
}
Config is how the interactive shell reads its commands.