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)
}
}
Output:
Index ¶
- Constants
- func Commands() []string
- func Decontrol(c rune) rune
- func DefaultBinds() map[string]map[string]Bind
- func DefaultVars() map[string]any
- func Demeta(c rune) rune
- func Encontrol(c rune) rune
- func Enmeta(c rune) rune
- func Escape(s string) string
- func EscapeMacro(s string) string
- func IsControl(c rune) bool
- func IsMeta(c rune) bool
- func Parse(r io.Reader, h Handler, opts ...Option) error
- func ParseBytes(buf []byte, h Handler, opts ...Option) error
- func ParseFile(name string, h Handler, opts ...Option) error
- func Unescape(s string) string
- func UserDefault(u *user.User, cfg *Config, opts ...Option) error
- type Bind
- type Config
- func (cfg *Config) Bind(keymap, sequence, action string, macro bool) error
- func (cfg *Config) Do(name, value string) error
- func (cfg *Config) Get(name string) any
- func (cfg *Config) GetBool(name string) bool
- func (cfg *Config) GetInt(name string) int
- func (cfg *Config) GetString(name string) string
- func (cfg *Config) ReadFile(name string) ([]byte, error)
- func (cfg *Config) Set(name string, value any) error
- type ConfigOption
- type Error
- type Handler
- type Locator
- type Option
- type ParseError
- type Parser
Examples ¶
Constants ¶
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.
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 DefaultBinds ¶
DefaultBinds are the default readline 8.3 bind keymaps.
see: gen/readline-dump.c
func DefaultVars ¶
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 EscapeMacro ¶ added in v0.1.2
EscapeMacro escapes a inputrc macro.
func Parse ¶
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 ¶
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 UserDefault ¶
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 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 NewDefaultConfig ¶
func NewDefaultConfig(opts ...ConfigOption) *Config
NewDefaultConfig creates a new inputrc config with default values.
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.
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 WithHaltOnErr ¶
WithHaltOnErr is a parser option to halt on every encountered error.
func WithStrict ¶ added in v0.2.0
WithStrict is a parser option that is kept for compatibility. The parser always rejects a keymap that readline does not have.
func WithVersion ¶ added in v0.6.0
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.