readline

package module
v0.0.0-...-4afb088 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2021 License: GPL-3.0 Imports: 5 Imported by: 0

README

Readline Bindings

This is a set of bindings to the GNU Readline Library.

The existing readline bindings for Go are more limited than this library, if you can believe it.

Note that the return type of String() has changed.

It was

func String(prompt string) string

and it's now

func String(prompt string) (string, error)

Installing the library

To install the library in order to use it, type:

go get github.com/bobappleyard/readline

You may need to be root.

For Mac OS X users, you may see errors like rl_catch_sigwinch undeclared. If so, you need to install GNU Readline via Homebrew:

brew install readline

On Mac OS X, if you encounter errors like these:

> go get github.com/bobappleyard/readline
# github.com/bobappleyard/readline
readline.go:38:19: error: stdio.h: No such file or directory
readline.go:39:20: error: stdlib.h: No such file or directory
In file included from /usr/local/opt/readline/include/readline/keymaps.h:35,
    	         from /usr/local/opt/readline/include/readline/readline.h:37,
            	 from readline.go:40:
/usr/local/opt/readline/include/readline/chardefs.h:25:19: error: ctype.h: No such file or directory
/usr/local/opt/readline/include/readline/chardefs.h:38:22: error: string.h: No such file or directory
In file included from readline.go:41:
/usr/local/opt/readline/include/readline/history.h:29:58: error: time.h: No such file or directory

You need to install "Command Line Tools" which can be done in XCode > Preferences > Downloads.

To install the library in order to hack on it, type

git clone git://github.com/bobappleyard/readline.git

Using the library

import "github.com/bobappleyard/readline"

These bindings provide access to three basic features of Readline:

  • Getting text from a prompt (via the String() and Reader() functions).
  • Managing the prompt's history (via the AddHistory(), GetHistory(), ClearHistory() and HistorySize() functions).
  • Controlling tab completion (via the Completer variable).

An example of the library's use:

package main

import (
	"io"
	"fmt"
	"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)
	}
}

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

Constants

This section is empty.

Variables

View Source
var Completer = func(query, ctx string) []string {
	return nil
}

This function provides entries for the tab completer.

View Source
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.

View Source
var Continue = ".."

The continue prompt used by Reader(). The prompt can contain ANSI escape sequences, they will be escaped as necessary.

View Source
var Prompt = "> "

The prompt used by Reader(). The prompt can contain ANSI escape sequences, they will be escaped as necessary.

Functions

func AddHistory

func AddHistory(s string)

Add an item to the history.

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.

func ClearHistory

func ClearHistory()

Deletes all the items in the history.

func ClearScreen

func ClearScreen()

Clear the screen

func FilenameCompleter

func FilenameCompleter(query, ctx string) []string

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 ForceUpdateDisplay

func ForceUpdateDisplay()

rl_forced_update_display / redraw

func GetHistory

func GetHistory(i int) string

Retrieve a line from the history.

func HistorySize

func HistorySize() int

Returns the number of items in the history.

func Init

func Init()

func LoadHistory

func LoadHistory(path string) error

Load the history from a file.

func NewReader

func NewReader() io.Reader

Begin reading lines. If more than one line is required, the continue prompt is used for subsequent lines.

func RefreshLine

func RefreshLine()

Redraw current line

func ReplaceLine

func ReplaceLine(text string, clearUndo int)

Replace current line

func Resize

func Resize()

func SaveHistory

func SaveHistory(path string) error

Save the history to a file.

func SetWordBreaks

func SetWordBreaks(cs string)

func String

func String(prompt string) (string, error)

Read a line with the given prompt. The prompt can contain ANSI escape sequences, they will be escaped as necessary.

Types

This section is empty.

Jump to

Keyboard shortcuts

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