rawterm

package module
v0.0.0-...-f84711c Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2017 License: MIT Imports: 17 Imported by: 2

README

rawterm

Stripped down version of readline for raw terminal reading and writing.

Documentation

Overview

Readline is a pure go implementation for GNU-Readline kind library.

example:

rl, err := readline.New("> ")
if err != nil {
	panic(err)
}
defer rl.Close()

for {
	line, err := rl.Readline()
	if err != nil { // io.EOF
		break
	}
	println(line)
}

Package terminal provides support functions for dealing with terminals, as commonly found on UNIX systems.

Putting a terminal into raw mode is the most common requirement:

oldState, err := terminal.MakeRaw(0)
if err != nil {
        panic(err)
}
defer terminal.Restore(0, oldState)

Index

Constants

View Source
const (
	CharLineStart = 1
	CharBackward  = 2
	CharInterrupt = 3
	CharDelete    = 4
	CharLineEnd   = 5
	CharForward   = 6
	CharBell      = 7
	CharCtrlH     = 8
	CharTab       = 9
	CharCtrlJ     = 10
	CharKill      = 11
	CharCtrlL     = 12
	CharEnter     = 13
	CharNext      = 14
	CharPrev      = 16
	CharBckSearch = 18
	CharFwdSearch = 19
	CharTranspose = 20
	CharCtrlU     = 21
	CharCtrlW     = 23
	CharCtrlZ     = 26
	CharEsc       = 27
	CharEscapeEx  = 91
	CharBackspace = 127
)
View Source
const (
	MetaBackward rune = -iota - 1
	MetaForward
	MetaDelete
	MetaBackspace
	MetaTranspose
)

Variables

View Source
var (
	Stdin  io.ReadCloser  = os.Stdin
	Stdout io.WriteCloser = os.Stdout
	Stderr io.WriteCloser = os.Stderr
)
View Source
var (
	ErrInterrupt = errors.New("Interrupt")
)
View Source
var TabWidth = 4

Functions

func ClearScreen

func ClearScreen(w io.Writer) (int, error)

ClearScreen clears the console screen

func Debug

func Debug(o ...interface{})

append log info to another file

func DefaultIsTerminal

func DefaultIsTerminal() bool

func DefaultOnWidthChanged

func DefaultOnWidthChanged(f func())

func GetInt

func GetInt(s []string, def int) int

func GetScreenWidth

func GetScreenWidth() int

func GetSize

func GetSize(fd int) (width, height int, err error)

GetSize returns the dimensions of the given terminal.

func GetStdin

func GetStdin() int

func IsPrintable

func IsPrintable(key rune) bool

func IsTerminal

func IsTerminal(fd int) bool

IsTerminal returns true if the given file descriptor is a terminal.

func IsWordBreak

func IsWordBreak(i rune) bool

func Line

func Line(prompt string) (string, error)

readline with global configs

func LineCount

func LineCount(screenWidth, w int) int

calculate how many lines for N character

func Password

func Password(prompt string) ([]byte, error)

func ReadPassword

func ReadPassword(fd int) ([]byte, error)

ReadPassword reads a line of input from a terminal without local echo. This is commonly used for inputting passwords and other sensitive data. The slice returned does not include the \n.

func Restore

func Restore(fd int, state *State) error

func SplitByLine

func SplitByLine(start, screenWidth int, rs []rune) []string

func SuspendMe

func SuspendMe()

SuspendMe use to send suspend signal to myself, when we in the raw mode. For OSX it need to send to parent's pid For Linux it need to send to myself

func WaitForResume

func WaitForResume() chan struct{}

WaitForResume need to call before current process got suspend. It will run a ticker until a long duration is occurs, which means this process is resumed.

Types

type CancelableStdin

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

func NewCancelableStdin

func NewCancelableStdin(r io.Reader) *CancelableStdin

func (*CancelableStdin) Close

func (c *CancelableStdin) Close() error

func (*CancelableStdin) Read

func (c *CancelableStdin) Read(b []byte) (n int, err error)

type Config

type Config struct {
	// prompt supports ANSI escape sequence, so we can color some characters even in windows
	Prompt string

	// Any key press will pass to Listener
	// NOTE: Listener will be triggered by (nil, 0, 0) immediately
	Listener Listener

	InterruptPrompt string
	EOFPrompt       string

	FuncGetWidth func() int

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	EnableMask bool
	MaskRune   rune

	// erase the editing line after user submited it
	// it use in IM usually.
	UniqueEditLine bool

	// filter input runes (may be used to disable CtrlZ or for translating some keys to different actions)
	// -> output = new (translated) rune and true/false if continue with processing this one
	FuncFilterInputRune func(rune) (rune, bool)

	// force use interactive even stdout is not a tty
	FuncIsTerminal      func() bool
	FuncMakeRaw         func() error
	FuncExitRaw         func() error
	FuncOnWidthChanged  func(func())
	ForceUseInteractive bool
	// contains filtered or unexported fields
}

func (*Config) Init

func (c *Config) Init() error

func (*Config) SetListener

func (c *Config) SetListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool))

type DumpListener

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

func (*DumpListener) OnChange

func (d *DumpListener) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)

type Instance

type Instance struct {
	Config    *Config
	Terminal  *Terminal
	Operation *Operation
}

func New

func New(prompt string) (*Instance, error)

func NewEx

func NewEx(cfg *Config) (*Instance, error)

func (*Instance) Clean

func (i *Instance) Clean()

func (*Instance) Close

func (i *Instance) Close() error

we must make sure that call Close() before process exit.

func (*Instance) GenPasswordConfig

func (i *Instance) GenPasswordConfig() *Config

func (*Instance) Line

func (i *Instance) Line() *Result

func (*Instance) ReadPassword

func (i *Instance) ReadPassword(prompt string) ([]byte, error)

func (*Instance) ReadPasswordEx

func (i *Instance) ReadPasswordEx(prompt string, l Listener) ([]byte, error)

func (*Instance) ReadPasswordWithConfig

func (i *Instance) ReadPasswordWithConfig(cfg *Config) ([]byte, error)

we can generate a config by `i.GenPasswordConfig()`

func (*Instance) ReadSlice

func (i *Instance) ReadSlice() ([]byte, error)

same as readline

func (*Instance) Readline

func (i *Instance) Readline() (string, error)

err is one of (nil, io.EOF, readline.ErrInterrupt)

func (*Instance) Refresh

func (i *Instance) Refresh()

func (*Instance) SetConfig

func (i *Instance) SetConfig(cfg *Config) *Config

func (*Instance) SetMaskRune

func (i *Instance) SetMaskRune(r rune)

func (*Instance) SetPrompt

func (i *Instance) SetPrompt(s string)

func (*Instance) Stderr

func (i *Instance) Stderr() io.Writer

readline will refresh automatic when write through Stdout()

func (*Instance) Stdout

func (i *Instance) Stdout() io.Writer

readline will refresh automatic when write through Stdout()

func (*Instance) Write

func (i *Instance) Write(b []byte) (int, error)

type InterruptError

type InterruptError struct {
	Line []rune
}

func (*InterruptError) Error

func (*InterruptError) Error() string

type Listener

type Listener interface {
	OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
}

func FuncListener

func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener

type Operation

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

func NewOperation

func NewOperation(t *Terminal, cfg *Config) *Operation

func (*Operation) Clean

func (o *Operation) Clean()

func (Operation) EnterPasswordMode

func (o Operation) EnterPasswordMode(cfg *Config) (err error)

func (Operation) ExitPasswordMode

func (o Operation) ExitPasswordMode()

func (*Operation) GenPasswordConfig

func (o *Operation) GenPasswordConfig() *Config

func (*Operation) Password

func (o *Operation) Password(prompt string) ([]byte, error)

func (Operation) PasswordConfig

func (o Operation) PasswordConfig() *Config

func (*Operation) PasswordEx

func (o *Operation) PasswordEx(prompt string, l Listener) ([]byte, error)

func (*Operation) PasswordWithConfig

func (o *Operation) PasswordWithConfig(cfg *Config) ([]byte, error)

func (*Operation) Refresh

func (o *Operation) Refresh()

func (*Operation) Runes

func (o *Operation) Runes() ([]rune, error)

func (*Operation) SetBuf

func (o *Operation) SetBuf(s string)

func (*Operation) SetConfig

func (op *Operation) SetConfig(cfg *Config) (*Config, error)

func (*Operation) SetMaskRune

func (o *Operation) SetMaskRune(r rune)

func (*Operation) SetPrompt

func (o *Operation) SetPrompt(s string)

func (*Operation) SetTitle

func (o *Operation) SetTitle(t string)

func (*Operation) Slice

func (o *Operation) Slice() ([]byte, error)

func (*Operation) Stderr

func (o *Operation) Stderr() io.Writer

func (*Operation) Stdout

func (o *Operation) Stdout() io.Writer

func (*Operation) String

func (o *Operation) String() (string, error)

type RawMode

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

func (*RawMode) Enter

func (r *RawMode) Enter() (err error)

func (*RawMode) Exit

func (r *RawMode) Exit() error

type Result

type Result struct {
	Line  string
	Error error
}

func (*Result) CanBreak

func (l *Result) CanBreak() bool

func (*Result) CanContinue

func (l *Result) CanContinue() bool

type RuneBuffer

type RuneBuffer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewRuneBuffer

func NewRuneBuffer(w io.Writer, prompt string, cfg *Config, width int) *RuneBuffer

func (*RuneBuffer) BackEscapeWord

func (r *RuneBuffer) BackEscapeWord()

func (*RuneBuffer) Backspace

func (r *RuneBuffer) Backspace()

func (*RuneBuffer) Backup

func (r *RuneBuffer) Backup()

func (*RuneBuffer) Clean

func (r *RuneBuffer) Clean()

func (*RuneBuffer) CurrentWidth

func (r *RuneBuffer) CurrentWidth(x int) int

func (*RuneBuffer) CursorLineCount

func (r *RuneBuffer) CursorLineCount() int

func (*RuneBuffer) Delete

func (r *RuneBuffer) Delete() (success bool)

func (*RuneBuffer) DeleteWord

func (r *RuneBuffer) DeleteWord()

func (*RuneBuffer) Erase

func (r *RuneBuffer) Erase()

func (*RuneBuffer) IdxLine

func (r *RuneBuffer) IdxLine(width int) int

func (*RuneBuffer) IsCursorInEnd

func (r *RuneBuffer) IsCursorInEnd() bool

func (*RuneBuffer) Kill

func (r *RuneBuffer) Kill()

func (*RuneBuffer) KillFront

func (r *RuneBuffer) KillFront()

func (*RuneBuffer) Len

func (r *RuneBuffer) Len() int

func (*RuneBuffer) LineCount

func (r *RuneBuffer) LineCount(width int) int

func (*RuneBuffer) MoveBackward

func (r *RuneBuffer) MoveBackward()

func (*RuneBuffer) MoveForward

func (r *RuneBuffer) MoveForward()

func (*RuneBuffer) MoveTo

func (r *RuneBuffer) MoveTo(ch rune, prevChar, reverse bool) (success bool)

func (*RuneBuffer) MoveToEndWord

func (r *RuneBuffer) MoveToEndWord()

func (*RuneBuffer) MoveToLineEnd

func (r *RuneBuffer) MoveToLineEnd()

func (*RuneBuffer) MoveToLineStart

func (r *RuneBuffer) MoveToLineStart()

func (*RuneBuffer) MoveToNextWord

func (r *RuneBuffer) MoveToNextWord()

func (*RuneBuffer) MoveToPrevWord

func (r *RuneBuffer) MoveToPrevWord() (success bool)

func (*RuneBuffer) OnWidthChange

func (r *RuneBuffer) OnWidthChange(newWidth int)

func (*RuneBuffer) Pos

func (r *RuneBuffer) Pos() int

func (*RuneBuffer) PromptLen

func (r *RuneBuffer) PromptLen() int

func (*RuneBuffer) Refresh

func (r *RuneBuffer) Refresh(f func())

func (*RuneBuffer) Replace

func (r *RuneBuffer) Replace(ch rune)

func (*RuneBuffer) Reset

func (r *RuneBuffer) Reset() []rune

func (*RuneBuffer) Restore

func (r *RuneBuffer) Restore()

func (*RuneBuffer) RuneSlice

func (r *RuneBuffer) RuneSlice(i int) []rune

func (*RuneBuffer) Runes

func (r *RuneBuffer) Runes() []rune

func (*RuneBuffer) Set

func (r *RuneBuffer) Set(buf []rune)

func (*RuneBuffer) SetConfig

func (r *RuneBuffer) SetConfig(cfg *Config)

func (*RuneBuffer) SetMask

func (r *RuneBuffer) SetMask(m rune)

func (*RuneBuffer) SetOffset

func (r *RuneBuffer) SetOffset(offset string)

func (*RuneBuffer) SetPrompt

func (r *RuneBuffer) SetPrompt(prompt string)

func (*RuneBuffer) SetStyle

func (r *RuneBuffer) SetStyle(start, end int, style string)

func (*RuneBuffer) SetWithIdx

func (r *RuneBuffer) SetWithIdx(idx int, buf []rune)

func (*RuneBuffer) Transpose

func (r *RuneBuffer) Transpose()

func (*RuneBuffer) WriteRune

func (r *RuneBuffer) WriteRune(s rune)

func (*RuneBuffer) WriteRunes

func (r *RuneBuffer) WriteRunes(s []rune)

func (*RuneBuffer) WriteString

func (r *RuneBuffer) WriteString(s string)

type Runes

type Runes struct{}

func (Runes) Aggregate

func (Runes) Aggregate(candicate [][]rune) (same []rune, size int)

func (Runes) Backspace

func (Runes) Backspace(r []rune) []byte

func (Runes) ColorFilter

func (Runes) ColorFilter(r []rune) []rune

func (Runes) Copy

func (Runes) Copy(r []rune) []rune

func (Runes) Equal

func (Runes) Equal(a, b []rune) bool

func (Runes) EqualFold

func (r Runes) EqualFold(a, b []rune) bool

func (Runes) EqualRune

func (Runes) EqualRune(a, b rune, fold bool) bool

func (Runes) EqualRuneFold

func (r Runes) EqualRuneFold(a, b rune) bool

func (Runes) HasPrefix

func (Runes) HasPrefix(r, prefix []rune) bool

func (Runes) HasPrefixFold

func (Runes) HasPrefixFold(r, prefix []rune) bool

func (Runes) Index

func (Runes) Index(r rune, rs []rune) int

func (Runes) IndexAll

func (rs Runes) IndexAll(r, sub []rune) int

Search in runes from front to end

func (Runes) IndexAllBck

func (rs Runes) IndexAllBck(r, sub []rune) int

Search in runes from end to front

func (Runes) IndexAllBckEx

func (rs Runes) IndexAllBckEx(r, sub []rune, fold bool) int

func (Runes) IndexAllEx

func (rs Runes) IndexAllEx(r, sub []rune, fold bool) int

func (Runes) TrimSpaceLeft

func (Runes) TrimSpaceLeft(in []rune) []rune

func (Runes) Width

func (Runes) Width(r rune) int

func (Runes) WidthAll

func (Runes) WidthAll(r []rune) (length int)

type State

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

State contains the state of a terminal.

func GetState

func GetState(fd int) (*State, error)

GetState returns the current state of a terminal which may be useful to restore the terminal after a signal.

func MakeRaw

func MakeRaw(fd int) (*State, error)

MakeRaw put the terminal connected to the given file descriptor into raw mode and returns the previous state of the terminal so that it can be restored.

type Terminal

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

func NewTerminal

func NewTerminal(cfg *Config) (*Terminal, error)

func (*Terminal) Bell

func (t *Terminal) Bell()

func (*Terminal) Close

func (t *Terminal) Close() error

func (*Terminal) EnterRawMode

func (t *Terminal) EnterRawMode() (err error)

func (*Terminal) ExitRawMode

func (t *Terminal) ExitRawMode() (err error)

func (*Terminal) GetConfig

func (t *Terminal) GetConfig() *Config

func (*Terminal) GetOffset

func (t *Terminal) GetOffset(f func(offset string))

func (*Terminal) IsReading

func (t *Terminal) IsReading() bool

func (*Terminal) KickRead

func (t *Terminal) KickRead()

func (*Terminal) Print

func (t *Terminal) Print(s string)

func (*Terminal) PrintRune

func (t *Terminal) PrintRune(r rune)

func (*Terminal) ReadRune

func (t *Terminal) ReadRune() rune

return rune(0) if meet EOF

func (*Terminal) Readline

func (t *Terminal) Readline() *Operation

func (*Terminal) SetConfig

func (t *Terminal) SetConfig(c *Config) error

func (*Terminal) SleepToResume

func (t *Terminal) SleepToResume()

SleepToResume will sleep myself, and return only if I'm resumed.

func (*Terminal) Write

func (t *Terminal) Write(b []byte) (int, error)

Directories

Path Synopsis
example
readline-pass-strength
This file is licensed under the WTFPL: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
This file is licensed under the WTFPL: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

Jump to

Keyboard shortcuts

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