led

package module
v0.0.0-...-00ec37e Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2018 License: MIT Imports: 5 Imported by: 0

README

Led

Build Status Go Report Card GoDoc

A line editor in Go. Inspired by linenoise, but written with extensibility and separation of concerns in mind.

The motivation behind this library is to provide line editor functionality that can be controlled, extended, and modified by clients with regards to how to handle key events. In this regard the name of the library also is a pun on letting go (of control).

Usage

import (
	"github.com/svenfuchs/led"
)

func main() {
	e := NewEd("$ ").Run()
	e.Handle(led.Chars, func(e *led.Ed, k led.Key) { ... })
	e.Run()
}

See example/led.go for a usage example that makes use of custom key handlers, suggestions, completion, and history, and reimplements (most of?) the functionality in linenoise.

Also see https://github.com/svenfuchs/travis-go for an example that takes over more control.

Todo

  • Handle multiple lines if line length exceeds terminal width
  • History search with ctrl-r

Documentation

Index

Constants

View Source
const (
	Clear int = iota
	ClearLine
	Cursor
	ShowCursor
	HideCursor
	Del
	Cr
	Newline
	Red
	Green
	Reset
)

Known ansi codes

View Source
const (
	Rgt int = iota
	Lft
)

Directions

View Source
const (
	Chars int = iota
	CtrlA
	CtrlB
	CtrlC
	CtrlD
	CtrlE
	CtrlF
	CtrlH
	Tab
	CtrlK
	CtrlL
	Enter
	CtrlN
	CtrlP
	CtrlT
	CtrlU
	CtrlW
	Esc
	Backspace
	Delete
	ShiftTab
	Up
	Down
	Right
	Left
)

Known keys

View Source
const (
	Forw int = iota
	Back
	Hist
	Comp
	Sugg
)

Forw/Back - directions Hist/Comp/Sugg - modes for cycling through sets

Variables

View Source
var Keys = map[int]Key{
	CtrlA:     {CtrlA, []byte{0x1}, "Ctrl-A"},
	CtrlB:     {CtrlB, []byte{0x2}, "Ctrl-B"},
	CtrlC:     {CtrlC, []byte{0x3}, "Ctrl-C"},
	CtrlD:     {CtrlD, []byte{0x4}, "Ctrl-D"},
	CtrlE:     {CtrlE, []byte{0x5}, "Ctrl-E"},
	CtrlF:     {CtrlF, []byte{0x6}, "Ctrl-F"},
	CtrlH:     {CtrlH, []byte{0x8}, "Ctrl-H"},
	Tab:       {Tab, []byte{0x9}, "Tab"},
	CtrlK:     {CtrlK, []byte{0x0b}, "Ctrl-K"},
	CtrlL:     {CtrlL, []byte{0x0c}, "Ctrl-L"},
	Enter:     {Enter, []byte{0x0d}, "Enter"},
	CtrlN:     {CtrlN, []byte{0x0e}, "Ctrl-N"},
	CtrlP:     {CtrlP, []byte{0x10}, "Ctrl-P"},
	CtrlT:     {CtrlT, []byte{0x14}, "Ctrl-T"},
	CtrlU:     {CtrlU, []byte{0x15}, "Ctrl-U"},
	CtrlW:     {CtrlW, []byte{0x17}, "Ctrl-W"},
	Esc:       {Esc, []byte{0x1b}, "Esc"},
	Backspace: {Backspace, []byte{0x7f}, "Backspace"},
	Delete:    {Delete, []byte{0x1b, 0x5b, 0x33, 0x7E}, "Delete"},
	ShiftTab:  {ShiftTab, []byte{0x1b, 0x5b, 0x5a}, "Shift-Tab"},
	Up:        {Up, []byte{0x1b, 0x5b, 0x41}, "Up"},
	Down:      {Down, []byte{0x1b, 0x5b, 0x42}, "Down"},
	Right:     {Right, []byte{0x1b, 0x5b, 0x43}, "Right"},
	Left:      {Left, []byte{0x1b, 0x5b, 0x44}, "Left"},
}

Keys defines known keys

Functions

func Ansi

func Ansi(c int) []byte

Ansi returns the chars for a given ansi code

func Colored

func Colored(c int, b []byte) []byte

Colored returns the given chars wrapped in color codes

func Deansi

func Deansi(b []byte) []byte

Deansi replaces ansi codes in the given byte array with hints. This is useful for testing.

func MoveCursor

func MoveCursor(pos int, d int) []byte

MoveCursor returns ansi codes for moving the cursor by the given number of chars in the given direction.

func Read

func Read(tty reader) chan Key

Read returns a channel for reading keys. Terminates on ctrl-d.

func SetCursor

func SetCursor(pos int) []byte

SetCursor returns ansi codes for setting the cursor to the given horizontal position.

Types

type Code

type Code struct {
	Code  int
	Chars []byte
	Hint  string
}

Code represents an ansi code

func (Code) Str

func (c Code) Str() string

Str returns the ansi code's chars as a string

type Ed

type Ed struct {
	Prompt    []byte
	Pos       int
	Chars     []byte
	Suggested []byte
	// contains filtered or unexported fields
}

Ed represents the line editor

func NewEd

func NewEd(led string, t ...Iterm) *Ed

NewEd creates a line editor (Ed) without any handlers attached. See NewReadline for a line editor that resembles (most of) Linenoise's functionality.

func NewReadline

func NewReadline(led string, t ...Iterm) *Ed

NewReadline creates a line editor that resembles most of Linenoise's functionality

func (*Ed) Append

func (e *Ed) Append(b []byte)

Append appends the given chars at the end of the line.

func (*Ed) Back

func (e *Ed) Back()

Back removes one char before the current cursor position.

func (*Ed) BackWord

func (e *Ed) BackWord()

BackWord removes one word before the current cursor position.

func (*Ed) Complete

func (e *Ed) Complete(strs [][]byte, dir int)

Complete displays the previous or next completion from the given slice depending on the given direction.

func (*Ed) CompleteNext

func (e *Ed) CompleteNext(strs [][]byte)

CompleteNext displays the next completion from the given slice.

func (*Ed) CompletePrev

func (e *Ed) CompletePrev(strs [][]byte)

CompletePrev displays the previous completion from the given slice.

func (*Ed) Del

func (e *Ed) Del(i ...int)

Del writes a delete char (`\x7F`) to the terminae.

func (*Ed) Delete

func (e *Ed) Delete()

Delete removes one char after the current cursor position.

func (*Ed) DeleteFromCursor

func (e *Ed) DeleteFromCursor()

DeleteFromCursor deletes all chars from the current cursor position to the end of the line.

func (*Ed) Discard

func (e *Ed) Discard()

Discard discards the given input by moving to the next line and starting over with an empty prompt. This resembles the behaviour Ctrl-C in Bash or Zshele.

func (*Ed) End

func (e *Ed) End()

End moves the cursor to the end of the line.

func (*Ed) Handle

func (e *Ed) Handle(key int, handler func(*Ed, Key))

Handle attaches a handler for a key

func (*Ed) History

func (e *Ed) History(strs [][]byte, dir int)

History displays the previous or next line from the given slice depending on the given direction.

func (*Ed) HistoryNext

func (e *Ed) HistoryNext(strs [][]byte)

HistoryNext displays the next line from the given slice.

func (*Ed) HistoryPrev

func (e *Ed) HistoryPrev(strs [][]byte)

HistoryPrev displays the previous line from the given slice.

func (*Ed) Insert

func (e *Ed) Insert(b []byte)

Insert inserts the given chars at the current cursor position.

func (*Ed) Left

func (e *Ed) Left()

Left moves the cursor one char to the left.

func (*Ed) MoveCursor

func (e *Ed) MoveCursor(i int, dir int)

MoveCursor moves the cursor by the given number of chars in the given direction.

func (*Ed) Newline

func (e *Ed) Newline()

Newline writes a newline char to the terminae.

func (*Ed) Pause

func (e *Ed) Pause()

Pause pauses the editor, should be used before outputting text to the terminal, e.g. in an Enter handler.

func (*Ed) Refresh

func (e *Ed) Refresh()

func (*Ed) Reject

func (e *Ed) Reject(chars []byte)

Reject rejects the given chars by printing them at the current cursor position in red, and removing them after 100 milliseconds.

func (*Ed) Reset

func (e *Ed) Reset()

Reset resets the editor and starts over with an empty line.

func (*Ed) Resume

func (e *Ed) Resume()

Resume resumes the editor, and places the terminal in raw mode.

func (*Ed) Return

func (e *Ed) Return()

Return moves the cursor to the beginning of the line.

func (*Ed) Right

func (e *Ed) Right()

Right moves the cursor one char to the right.

func (*Ed) Run

func (e *Ed) Run()

Run runs the editor

func (*Ed) Set

func (e *Ed) Set(b []byte)

Set sets the content of the editor to the given line.

func (*Ed) SetCursor

func (e *Ed) SetCursor(pos ...int)

SetCursor sets the cursor to the given position, defaults to the current position.

func (*Ed) Stop

func (e *Ed) Stop()

Stop stops the terminae.

func (*Ed) Str

func (e *Ed) Str() string

Str returns the current line.

func (*Ed) Suggest

func (e *Ed) Suggest(str []byte)

Suggest appends the first matching suggestion from the given slice in green after the current cursor position.

func (*Ed) Transpose

func (e *Ed) Transpose()

Transpose transposes the char before the cursor position with the one on the cursor position, and moves the cursor one char to the right, if possible. If the cursor is at the beginning of the line it transposes the current char with the next one, and moves the cursor two chars to the right. This resembles Zshell's behaviour.

func (*Ed) Write

func (e *Ed) Write(b []byte)

Write writes the given chars to the terminae.

type Iterm

type Iterm interface {
	Start()
	Read(b []byte) (int, error)
	Write(b []byte) (int, error)
	Restore() error
	RawMode() error
	Close() error
}

Iterm represents a subset of the tty implemented in github.com/pkg/term.

type Key

type Key struct {
	Code  int
	Chars []byte
	Name  string
}

Key represents a key

func (Key) Str

func (k Key) Str() string

Str returns the key's chars as a string

type List

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

List represents a list of strings that are used for completion, history, and suggestions.

func NewList

func NewList(strs [][]byte, str []byte, mode int) *List

NewList returns a list of strings that are used for completion, history, and suggestions.

func (*List) Next

func (c *List) Next() []byte

Next returns the next string from the list.

func (*List) Prev

func (c *List) Prev() []byte

Prev returns the previous string from the list.

type Term

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

Term represents a terminal

func StartTerm

func StartTerm(ts ...Iterm) *Term

StartTerm starts a terminal.

func (*Term) Clear

func (t *Term) Clear()

Clear clears from the current cursor position to the end of the line.

func (*Term) ClearLine

func (t *Term) ClearLine()

ClearLine clears the current line.

func (*Term) Del

func (t *Term) Del(i ...int)

Del writes the given number of delete chars (`\x7F`) to the terminal (defaults to 1), deleting the char after the cursor position.

func (*Term) HideCursor

func (t *Term) HideCursor()

HideCursor hides the cursor.

func (*Term) MoveCursor

func (t *Term) MoveCursor(i int, dir int)

MoveCursor moves the cursor by the given number of chars in the given direction.

func (*Term) Newline

func (t *Term) Newline()

Newline writes a newline char to the terminal.

func (*Term) Pause

func (t *Term) Pause()

Pause pauses the terminal, restoring the previous mode and settings.

func (*Term) Read

func (t *Term) Read() chan Key

Read returns a channel for reading keys from the terminal. See keys.Read.

func (*Term) Resume

func (t *Term) Resume()

Resume resumes the terminal, setting the terminal in raw mode.

func (*Term) Return

func (t *Term) Return()

Return writes a carriage return char to the terminal.

func (*Term) SetCursor

func (t *Term) SetCursor(pos int)

SetCursor moves the cursor to the given horizontal position.

func (*Term) ShowCursor

func (t *Term) ShowCursor()

ShowCursor shows the cursor.

func (*Term) Stop

func (t *Term) Stop()

Stop stops the terminal, restoring the previous mode and settings, and closing the tty.

func (*Term) Write

func (t *Term) Write(b []byte)

Write writes the given chars to the terminal.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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