inputrc

package
v0.0.0-...-8aadb99 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0, MIT Imports: 13 Imported by: 2

Documentation

Overview

Package inputrc parses readline inputrc files.

Example
package main

import (
	"os/user"

	"github.com/alexj212/readline/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

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.

Variables

This section is empty.

Functions

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 bind keymaps.

see: INPUTRC=/dev/null bash -c 'bind -pm <keymap>'

func DefaultVars

func DefaultVars() map[string]interface{}

DefaultVars are the default readline vars.

see: INPUTRC=/dev/null bash -c 'bind -v'

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

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/alexj212/readline/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.

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.

Types

type Bind

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]interface{}
	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) interface{}

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 interface{}) 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"
	// 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 interface{}) error
	// Get gets the value.
	Get(name string) interface{}
	// 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 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 set 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

func WithStrict(strict bool) Option

WithStrict is a parser option to set strict keymap parsing.

func WithTerm

func WithTerm(term string) Option

WithTerm is a parser option to set the term name.

type ParseError

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

ParseError is a parse error.

func (*ParseError) Error

func (err *ParseError) Error() string

Error satisfies the error interface.

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) Errs

func (p *Parser) Errs() []error

Errs returns the parse errors encountered.

func (*Parser) Parse

func (p *Parser) Parse(stream io.Reader, handler 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