inputrc

package module
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 14 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

inputrc

Package inputrc provides a Go implementation of a readline inputrc parser.

Unit Tests Go Reference Tags Discord Discussion

Using

Please see the package level examples in the Go package documentation.

Questions

Please ask questions and share ideas in GitHub Discussions. This repository does not take issues or pull requests.

About

inputrc was built primarily to support these projects:

  • usql - a universal command-line interface for SQL databases

Documentation

Overview

Package inputrc parses readline inputrc files.

Example
package main

import (
	"os/user"

	"github.com/xo/inputrc"
)

func main() {
	u, err := user.Current()
	if err != nil {
		panic(err)
	}
	cfg := inputrc.NewDefaultConfig()
	if err := inputrc.UserDefault(u, cfg, inputrc.WithApp("bash")); err != nil {
		panic(err)
	}
}

Index

Examples

Constants

View Source
const (
	Control   rune = 0x1f
	Meta      rune = 0x80
	Esc       rune = 0x1b
	Delete    rune = 0x7f
	Alert     rune = '\a'
	Backspace rune = '\b'
	Formfeed  rune = '\f'
	Newline   rune = '\n'
	Return    rune = '\r'
	Tab       rune = '\t'
	Vertical  rune = '\v'
	Space     rune = ' '
)

Keys.

View Source
const DefaultVersion = "8.3"

DefaultVersion is the readline version that an $if version test compares against, when the caller does not set one with WithVersion.

Variables

This section is empty.

Functions

func Commands added in v0.6.0

func Commands() []string

Commands returns the names of every command in readline 8.3, bound or not.

A consumer can use it to tell an unknown command in a user's file from a real one.

func Decontrol

func Decontrol(c rune) rune

Decontrol decodes a Control-c code.

func DefaultBinds

func DefaultBinds() map[string]map[string]Bind

DefaultBinds are the default readline 8.3 bind keymaps.

see: gen/readline-dump.c

func DefaultVars

func DefaultVars() map[string]any

DefaultVars are the default readline 8.3 vars.

The values are those of a UTF-8 locale on an xterm terminal. The four meta vars (convert-meta, input-meta, output-meta and meta-flag) follow the locale in readline, and enable-bracketed-paste and enable-active-region follow the terminal.

see: gen/readline-dump.c

func Demeta

func Demeta(c rune) rune

Demeta decodes a Meta-c code.

func Encontrol

func Encontrol(c rune) rune

Encontrol encodes a Control-c code.

func Enmeta

func Enmeta(c rune) rune

Enmeta encodes a Meta-c code.

func Escape

func Escape(s string) string

Escape escapes a inputrc string.

func EscapeMacro added in v0.1.2

func EscapeMacro(s string) string

EscapeMacro escapes a inputrc macro.

func IsControl

func IsControl(c rune) bool

IsControl returns true when c is a Control-c code.

func IsMeta

func IsMeta(c rune) bool

IsMeta returns true when c is a Meta-c code.

func Parse

func Parse(r io.Reader, h Handler, opts ...Option) error

Parse parses inputrc data from r.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/xo/inputrc"
)

func main() {
	const example = `
set editing-mode vi
$if Usql
  set keymap vi-insert
  "\r": a-usql-action
  "\d": 'echo test\n'
$endif

`
	cfg := inputrc.NewDefaultConfig()
	if err := inputrc.Parse(strings.NewReader(example), cfg, inputrc.WithApp("usql")); err != nil {
		panic(err)
	}
	fmt.Println("editing mode:", cfg.GetString("editing-mode"))
	fmt.Println("vi-insert:")
	fmt.Printf("  %s: %s\n", inputrc.Escape(string(inputrc.Return)), cfg.Binds["vi-insert"][string(inputrc.Return)].Action)
	fmt.Printf("  %s: '%s'\n", inputrc.Escape(string(inputrc.Delete)), inputrc.EscapeMacro(cfg.Binds["vi-insert"][string(inputrc.Delete)].Action))
}
Output:
editing mode: vi
vi-insert:
  \C-M: a-usql-action
  \C-?: 'echo test\n'

func ParseBytes

func ParseBytes(buf []byte, h Handler, opts ...Option) error

ParseBytes parses inputrc data from buf.

As readline does, the parser reports a line with a fault and goes on with the next line, so every good line reaches the handler. If any line had a fault, ParseBytes returns the faults as one error, and each fault in it is a *ParseError. To read them one by one, assert that the error has an Unwrap() []error method, as with the error that errors.Join returns. To look for one fault, use errors.Is or errors.As. If the WithHaltOnErr option is set, the parse stops at the first fault. The same holds for ParseFile and UserDefault.

func ParseFile

func ParseFile(name string, h Handler, opts ...Option) error

ParseFile parses inputrc data from a file name.

func Unescape

func Unescape(s string) string

Unescape unescapes a inputrc string.

func UserDefault

func UserDefault(u *user.User, cfg *Config, opts ...Option) error

UserDefault loads default inputrc settings for the user, as readline does.

If the INPUTRC environment variable is not empty, UserDefault reads that file and no other. If the file does not exist, UserDefault loads nothing and returns no error, and it does not fall back to another file. If INPUTRC is empty or unset, UserDefault reads the first file that exists of ~/.inputrc (_inputrc on Windows) and /etc/inputrc.

Types

type Bind added in v0.1.2

type Bind struct {
	Action string
	Macro  bool
}

Bind represents a key binding.

type Config

type Config struct {
	ReadFileFunc func(string) ([]byte, error)
	Vars         map[string]any
	Binds        map[string]map[string]Bind
	Funcs        map[string]func(string, string) error
}

Config is a inputrc config handler.

func NewConfig

func NewConfig() *Config

NewConfig creates a new inputrc config.

func NewDefaultConfig

func NewDefaultConfig(opts ...ConfigOption) *Config

NewDefaultConfig creates a new inputrc config with default values.

func (*Config) Bind

func (cfg *Config) Bind(keymap, sequence, action string, macro bool) error

Bind satisfies the Handler interface.

func (*Config) Do

func (cfg *Config) Do(name, value string) error

Do satisfies the Handler interface.

func (*Config) Get

func (cfg *Config) Get(name string) any

Get satisfies the Handler interface.

func (*Config) GetBool

func (cfg *Config) GetBool(name string) bool

GetBool returns the var name as a bool.

func (*Config) GetInt

func (cfg *Config) GetInt(name string) int

GetInt returns the var name as a int.

func (*Config) GetString

func (cfg *Config) GetString(name string) string

GetString returns the var name as a string.

func (*Config) ReadFile

func (cfg *Config) ReadFile(name string) ([]byte, error)

ReadFile satisfies the Handler interface.

func (*Config) Set

func (cfg *Config) Set(name string, value any) error

Set satisfies the Handler interface.

type ConfigOption

type ConfigOption func(*Config)

ConfigOption is a inputrc config handler option.

func WithConfigReadFileFunc

func WithConfigReadFileFunc(readFileFunc func(string) ([]byte, error)) ConfigOption

WithConfigReadFileFunc is a inputrc config option to set the func used for ReadFile operations.

type Error

type Error string

Error is a error.

const (
	// ErrBindMissingClosingQuote is the bind missing closing quote error.
	ErrBindMissingClosingQuote Error = `bind missing closing quote`
	// ErrMissingColon is the missing : error.
	ErrMissingColon Error = "missing :"
	// ErrMacroMissingClosingQuote is the macro missing closing quote error.
	ErrMacroMissingClosingQuote Error = `macro missing closing quote`
	// ErrInvalidKeymap is the invalid keymap error.
	ErrInvalidKeymap Error = "invalid keymap"
	// ErrInvalidEditingMode is the invalid editing mode error.
	ErrInvalidEditingMode Error = "invalid editing mode"
	// ErrElseWithoutMatchingIf is the $else without matching $if error.
	ErrElseWithoutMatchingIf Error = "$else without matching $if"
	// ErrEndifWithoutMatchingIf is the $endif without matching $if error.
	ErrEndifWithoutMatchingIf Error = "$endif without matching $if"
	// ErrInvalidValue is the invalid value error.
	ErrInvalidValue Error = "invalid value"
	// ErrUnknownVariable is the unknown variable error.
	ErrUnknownVariable Error = "unknown variable"
	// ErrUnknownDirective is the unknown directive error.
	ErrUnknownDirective Error = "unknown directive"
	// ErrIncludeTooDeep is the $include nesting too deep error.
	ErrIncludeTooDeep Error = "$include nesting too deep"
	// ErrUnknownModifier is the unknown modifier error.
	ErrUnknownModifier Error = "unknown modifier"
)

Errors.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Handler

type Handler interface {
	// ReadFile reads a file.
	ReadFile(name string) ([]byte, error)
	// Do handles $constructs.
	Do(typ string, param string) error
	// Set sets the value.
	Set(name string, value any) error
	// Get gets the value.
	Get(name string) any
	// Bind binds a key sequence to an action for the current keymap.
	Bind(keymap, sequence, action string, macro bool) error
}

Handler is the handler interface.

type Locator added in v0.6.0

type Locator interface {
	// Locate records the position of the directive that follows.
	Locate(name string, line int)
}

Locator is an optional interface of a Handler. If a Handler implements it, the parser calls Locate with the file name and line number before each Bind, Set and Do call, so that the Handler can report where a directive came from. The name is empty when the input has no name.

type Option

type Option func(*Parser)

Option is a parser option.

func WithApp

func WithApp(app string) Option

WithApp is a parser option to set the app name.

func WithHaltOnErr

func WithHaltOnErr(haltOnErr bool) Option

WithHaltOnErr is a parser option to halt on every encountered error.

func WithMode

func WithMode(mode string) Option

WithMode is a parser option to set the mode name.

func WithName

func WithName(name string) Option

WithName is a parser option to set the file name.

func WithStrict added in v0.2.0

func WithStrict(strict bool) Option

WithStrict is a parser option that is kept for compatibility. The parser always rejects a keymap that readline does not have.

func WithTerm

func WithTerm(term string) Option

WithTerm is a parser option to set the term name.

func WithVersion added in v0.6.0

func WithVersion(version string) Option

WithVersion is a parser option to set the readline version that $if version tests compare against. The default is DefaultVersion.

type ParseError

type ParseError struct {
	Name string
	Line int
	Text string
	Err  error

	// Msg is the wording that readline uses for the fault, such as "foo:
	// unknown parser directive". It is empty when readline has no wording
	// for the fault.
	Msg string
}

ParseError is a parse error.

func (*ParseError) Error

func (err *ParseError) Error() string

Error satisfies the error interface.

func (*ParseError) Message added in v0.10.0

func (err *ParseError) Message() string

Message returns Msg, or the text of Err when Msg is empty.

func (*ParseError) Unwrap

func (err *ParseError) Unwrap() error

Unwrap satisfies the errors.Unwrap call.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is a inputrc parser.

func New

func New(opts ...Option) *Parser

New creates a new inputrc parser.

func (*Parser) Parse

func (p *Parser) Parse(r io.Reader, h Handler) error

Parse parses inputrc data from the reader, passing sets and binding keys to h based on the configured options.

Jump to

Keyboard shortcuts

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