Documentation
¶
Overview ¶
This package provides access to basic GNU Readline functions. Currently supported are:
- getting text from a prompt (via the String() and NewReader() functions).
- managing the prompt's history (via the AddHistory(), GetHistory(), ClearHistory() and HistorySize() functions).
- controlling tab completion (via the Completer variable).
Here is a simple example:
package main
import (
"fmt"
"io"
"github.com/bobappleyard/readline"
)
func main() {
for {
l, err := readline.String("> ")
if err == io.EOF {
break
}
if err != nil {
fmt.Println("error: ", err)
break
}
fmt.Println(l)
readline.AddHistory(l)
}
}
Index ¶
- Variables
- func AddHistory(s string)
- func Cleanup()
- func ClearHistory()
- func ClearScreen()
- func FilenameCompleter(query, ctx string) []string
- func ForceUpdateDisplay()
- func GetHistory(i int) string
- func HistorySize() int
- func LoadHistory(path string) error
- func NewReader() io.Reader
- func RefreshLine()
- func ReplaceLine(text string, clearUndo int)
- func SaveHistory(path string) error
- func SetWordBreaks(cs string)
- func String(prompt string) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Completer = func(query, ctx string) []string { return nil }
This function provides entries for the tab completer.
var CompletionAppendChar = 0
If CompletionAppendChar is non-zero, readline will append the corresponding character to the prompt after each completion. A typical value would be a space.
var Continue = ".."
The continue prompt used by Reader(). The prompt can contain ANSI escape sequences, they will be escaped as necessary.
var Prompt = "> "
The prompt used by Reader(). The prompt can contain ANSI escape sequences, they will be escaped as necessary.
Functions ¶
func Cleanup ¶
func Cleanup()
Cleanup() frees internal memory and restores terminal attributes. This function should be called when program execution stops before the return of a String() call, so as not to leave the terminal in a corrupted state.
Example ¶
package main
import (
"fmt"
"github.com/bobappleyard/readline"
"os"
"os/signal"
"syscall"
)
func main() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT)
readline.CatchSigint = false
var line string
var err error
done := make(chan struct{})
go func() {
line, err = readline.String("> ")
close(done)
}()
select {
case <-sigint:
fmt.Println("\nInterrupted")
// Restore terminal attributes
readline.Cleanup()
// Note that we still have a goroutine reading from Stdin that
// will terminate when we exit.
os.Exit(1)
case <-done:
fmt.Printf("Read line %s, error %v\n", line, err)
}
}
Output:
func FilenameCompleter ¶
This function can be assigned to the Completer variable to use readline's default filename completion, or it can be called by a custom completer function to get a list of files and filter it.
func NewReader ¶
Begin reading lines. If more than one line is required, the continue prompt is used for subsequent lines.
func SetWordBreaks ¶
func SetWordBreaks(cs string)
Types ¶
This section is empty.