Categorygithub.com/peterh/liner
modulepackage
1.2.2
Repository: https://github.com/peterh/liner.git
Documentation: pkg.go.dev

# README

Liner

Liner is a command line editor with history. It was inspired by linenoise; everything Unix-like is a VT100 (or is trying very hard to be). If your terminal is not pretending to be a VT100, change it. Liner also support Windows.

Liner is intended for use by cross-platform applications. Therefore, the decision was made to write it in pure Go, avoiding cgo, for ease of cross compilation. Furthermore, features only supported on some platforms have been intentionally omitted. For example, Ctrl-Z is "suspend" on Unix, but "EOF" on Windows. In the interest of making an application behave the same way on every supported platform, Ctrl-Z is ignored by Liner.

Liner is released under the X11 license (which is similar to the new BSD license).

Line Editing

The following line editing commands are supported on platforms and terminals that Liner supports:

KeystrokeAction
Ctrl-A, HomeMove cursor to beginning of line
Ctrl-E, EndMove cursor to end of line
Ctrl-B, LeftMove cursor one character left
Ctrl-F, RightMove cursor one character right
Ctrl-Left, Alt-BMove cursor to previous word
Ctrl-Right, Alt-FMove cursor to next word
Ctrl-D, Del(if line is not empty) Delete character under cursor
Ctrl-D(if line is empty) End of File - usually quits application
Ctrl-CReset input (create new empty prompt)
Ctrl-LClear screen (line is unmodified)
Ctrl-TTranspose previous character with current character
Ctrl-H, BackSpaceDelete character before cursor
Ctrl-W, Alt-BackSpaceDelete word leading up to cursor
Alt-DDelete word following cursor
Ctrl-KDelete from cursor to end of line
Ctrl-UDelete from start of line to cursor
Ctrl-P, UpPrevious match from history
Ctrl-N, DownNext match from history
Ctrl-RReverse Search history (Ctrl-S forward, Ctrl-G cancel)
Ctrl-YPaste from Yank buffer (Alt-Y to paste next yank instead)
TabNext completion
Shift-Tab(after Tab) Previous completion

Note that "Previous" and "Next match from history" will retain the part of the line that the user has already typed, similar to zsh's "up-line-or-beginning-search" (which is the default on some systems) or bash's "history-search-backward" (which is my preferred behaviour, but does not appear to be the default Up keybinding on any system).

Getting started

package main

import (
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/peterh/liner"
)

var (
	history_fn = filepath.Join(os.TempDir(), ".liner_example_history")
	names      = []string{"john", "james", "mary", "nancy"}
)

func main() {
	line := liner.NewLiner()
	defer line.Close()

	line.SetCtrlCAborts(true)

	line.SetCompleter(func(line string) (c []string) {
		for _, n := range names {
			if strings.HasPrefix(n, strings.ToLower(line)) {
				c = append(c, n)
			}
		}
		return
	})

	if f, err := os.Open(history_fn); err == nil {
		line.ReadHistory(f)
		f.Close()
	}

	if name, err := line.Prompt("What is your name? "); err == nil {
		log.Print("Got: ", name)
		line.AppendHistory(name)
	} else if err == liner.ErrPromptAborted {
		log.Print("Aborted")
	} else {
		log.Print("Error reading line: ", err)
	}

	if f, err := os.Create(history_fn); err != nil {
		log.Print("Error writing history file: ", err)
	} else {
		line.WriteHistory(f)
		f.Close()
	}
}

For documentation, see http://godoc.org/github.com/peterh/liner

# Functions

NewLiner initializes a new *State, and sets the terminal into raw mode.
TerminalMode returns the current terminal input mode as an InputModeSetter.
TerminalSupported returns true if the current terminal supports line editing features, and false if liner will use the 'dumb' fallback for input.

# Constants

HistoryLimit is the maximum number of entries saved in the scrollback history.
KillRingMax is the max number of elements to save on the killring.
Two tab styles are currently available: TabCircular cycles through each completion item and displays it directly on the prompt TabPrints prints the list of completion items to the screen after a second tab key is pressed.
Two tab styles are currently available: TabCircular cycles through each completion item and displays it directly on the prompt TabPrints prints the list of completion items to the screen after a second tab key is pressed.

# Variables

ErrInternal is returned when liner experiences an error that it cannot handle.
ErrInvalidPrompt is returned from Prompt or PasswordPrompt if the prompt contains any unprintable runes (including substrings that could be colour codes on some platforms).
ErrNotTerminalOutput is returned from Prompt or PasswordPrompt if the platform is normally supported, but stdout has been redirected.
ErrPromptAborted is returned from Prompt or PasswordPrompt when the user presses Ctrl-C if SetCtrlCAborts(true) has been called on the State.

# Structs

State represents an open terminal.

# Interfaces

ModeApplier is the interface that wraps a representation of the terminal mode.

# Type aliases

Completer takes the currently edited line content at the left of the cursor and returns a list of completion candidates.
ShouldRestart is passed the error generated by readNext and returns true if the the read should be restarted or false if the error should be returned.
TabStyle is used to select how tab completions are displayed.
WordCompleter takes the currently edited line with the cursor position and returns the completion candidates for the partial word to be completed.