terminal

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2022 License: MIT Imports: 26 Imported by: 4

Documentation

Overview

Package terminal provides a widget that functions as a unix terminal. Like urwid, it emulates a vt220 (roughly). Mouse support is provided. See the terminal demo for more.

Index

Constants

View Source
const (
	CharsetDefault = iota
	CharsetUTF8    = iota
)
View Source
const (
	DecSpecialChars    = "▮◆▒␉␌␍␊°±␤␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π≠£·"
	AltDecSpecialChars = "_`abcdefghijklmnopqrstuvwxyz{|}~"
)
View Source
const (
	EscByte byte = 27
)

Variables

This section is empty.

Functions

func PtyStart1

func PtyStart1(c *exec.Cmd) (pty2, tty *os.File, err error)

PtyStart1 connects the supplied Cmd's stdin/stdout/stderr to a new tty object. The function returns the pty and tty, and also an error which is nil if the operation was successful.

func TCellEventToBytes

func TCellEventToBytes(ev interface{}, mouse IMouseSupport, last gowid.MouseState, paster IPaste, ti *terminfo.Terminfo) ([]byte, bool)

TCellEventToBytes converts TCell's representation of a terminal event to the string of bytes that would be the equivalent event according to the supplied Terminfo object. It returns a tuple of the byte slice representing the terminal event (if successful), and a bool (denoting success or failure). This function is used by the TerminalWidget. Its subprocess is connected to a tty controlled by gowid. Events from the user are parsed by gowid via TCell - they are then translated by this function before being written to the TerminalWidget subprocess's tty.

func UserInput

func UserInput(w IWidget, ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool

Types

type AliasCSICommand

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

func (AliasCSICommand) Alias

func (c AliasCSICommand) Alias() byte

func (AliasCSICommand) Call

func (c AliasCSICommand) Call(canvas *Canvas, args []int, qmark bool)

func (AliasCSICommand) FallbackArg

func (c AliasCSICommand) FallbackArg() int

func (AliasCSICommand) IsAlias

func (c AliasCSICommand) IsAlias() bool

func (AliasCSICommand) MinArgs

func (c AliasCSICommand) MinArgs() int

type Bell

type Bell struct{}

For callback registration

type CSIFunction

type CSIFunction func(canvas *Canvas, args []int, qmark bool)

type CSIMap

type CSIMap map[byte]ICSICommand

type Canvas

type Canvas struct {
	*ViewPortCanvas

	gowid.ICallbacks
	// contains filtered or unexported fields
}

Canvas implements gowid.ICanvas and stores the state of the terminal drawing area associated with a terminal (and TerminalWidget).

func NewCanvasOfSize

func NewCanvasOfSize(cols, rows int, scrollback int, widget ITerminal) *Canvas

func (*Canvas) CSIClearTabstop

func (c *Canvas) CSIClearTabstop(mode int)

func (*Canvas) CSIEraseDisplay

func (c *Canvas) CSIEraseDisplay(mode int)

func (*Canvas) CSIEraseLine

func (c *Canvas) CSIEraseLine(mode int)

func (*Canvas) CSIGetDeviceAttributes

func (c *Canvas) CSIGetDeviceAttributes(qmark bool)

Report as vt102, like vterm.py

func (*Canvas) CSISetAttr

func (c *Canvas) CSISetAttr(args []int)

func (*Canvas) CSISetKeyboardLEDs

func (c *Canvas) CSISetKeyboardLEDs(mode int)

func (*Canvas) CSISetModes

func (c *Canvas) CSISetModes(args []int, qmark bool, reset bool)

func (*Canvas) CSISetScroll

func (c *Canvas) CSISetScroll(top, bottom int)

CSISetScroll sets the scrolling region in the current terminal. top is the line number of the first line, bottom the bottom line. If both are 0, the whole screen is used.

func (*Canvas) CSIStatusReport

func (c *Canvas) CSIStatusReport(mode int)

func (*Canvas) CarriageReturn

func (c *Canvas) CarriageReturn()

func (*Canvas) Clear

func (c *Canvas) Clear(newcx, newcy gwutil.IntOption)

func (*Canvas) ConstrainCoords

func (c *Canvas) ConstrainCoords(x, y int, ignoreScrolling bool) (int, int)

func (*Canvas) DECAln

func (c *Canvas) DECAln()

func (*Canvas) Duplicate added in v1.3.0

func (c *Canvas) Duplicate() gowid.ICanvas

func (*Canvas) Erase

func (c *Canvas) Erase(startx, starty, endx, endy int)

func (*Canvas) InitTabstops

func (c *Canvas) InitTabstops(extend bool)

func (*Canvas) InsertChars

func (c *Canvas) InsertChars(startx, starty gwutil.IntOption, chars int, charo gwutil.RuneOption)

func (*Canvas) InsertLines

func (c *Canvas) InsertLines(atCursor bool, lines int)

InsertLines processes "CSI n L" e.g. "\033[5L". Lines are pushed down and blank lines inserted. Note that the 5 is only processed if a scroll region is defined - otherwise one line is inserted.

func (*Canvas) IsScrollRegionSet

func (c *Canvas) IsScrollRegionSet() bool

func (*Canvas) IsTabstop

func (c *Canvas) IsTabstop(x int) bool

func (*Canvas) LeaveEscapeResetState added in v1.2.0

func (c *Canvas) LeaveEscapeResetState()

func (*Canvas) LineFeed

func (c *Canvas) LineFeed(reverse bool)

func (*Canvas) MakeCellFrom

func (c *Canvas) MakeCellFrom(r rune) gowid.Cell

func (*Canvas) MoveCursor

func (c *Canvas) MoveCursor(x, y int, relative bool, relativeX bool, relativeY bool)

func (*Canvas) NewLine

func (c *Canvas) NewLine()

func (*Canvas) ParseCSI

func (c *Canvas) ParseCSI(r byte)

func (*Canvas) ParseEscape

func (c *Canvas) ParseEscape(r byte)

TODO am I always guaranteed to have something in escbuf?

func (*Canvas) ParseNonCSI

func (c *Canvas) ParseNonCSI(r byte, mod byte)

func (*Canvas) ParseOSC

func (c *Canvas) ParseOSC(osc []byte)

func (*Canvas) ProcessByte

func (c *Canvas) ProcessByte(b byte)

func (*Canvas) ProcessByteOrCommand

func (c *Canvas) ProcessByteOrCommand(r rune)

func (*Canvas) PushCursor

func (c *Canvas) PushCursor(r rune)

func (*Canvas) PushRune

func (c *Canvas) PushRune(r rune, x, y int)

func (*Canvas) RemoveChars

func (c *Canvas) RemoveChars(startx, starty gwutil.IntOption, chars int)

func (*Canvas) RemoveLines

func (c *Canvas) RemoveLines(atCursor bool, lines int)

func (*Canvas) Reset

func (c *Canvas) Reset()

func (*Canvas) ResetScroll

func (c *Canvas) ResetScroll()

func (*Canvas) Resize

func (c *Canvas) Resize(width, height int)

func (*Canvas) RestoreCursor

func (c *Canvas) RestoreCursor(withAttrs bool)

func (*Canvas) ReverseVideo

func (c *Canvas) ReverseVideo(undo bool)

TODO urwid uses undo - implement it

func (*Canvas) SGIToAttribs

func (c *Canvas) SGIToAttribs(args []int, fg, bg gwutil.IntOption, styles map[string]bool) (gwutil.IntOption, gwutil.IntOption, map[string]bool)

func (*Canvas) SaveCursor

func (c *Canvas) SaveCursor(withAttrs bool)

func (*Canvas) Scroll

func (c *Canvas) Scroll(dir ScrollDir)

func (*Canvas) ScrollBuffer

func (c *Canvas) ScrollBuffer(dir ScrollDir, reset bool, linesOpt gwutil.IntOption) int

ScrollBuffer will return the number of lines actually scrolled.

func (*Canvas) SetG01

func (c *Canvas) SetG01(r byte, mod byte)

func (*Canvas) SetMode

func (c *Canvas) SetMode(mode int, flag bool, qmark bool, reset bool)

func (*Canvas) SetRune

func (c *Canvas) SetRune(r rune)

func (*Canvas) SetRuneAt

func (c *Canvas) SetRuneAt(x, y int, r rune)

func (*Canvas) SetTabstop

func (c *Canvas) SetTabstop(x2 gwutil.IntOption, remove bool, clear bool)

func (*Canvas) SetTermCursor

func (c *Canvas) SetTermCursor(x2, y2 gwutil.IntOption)

func (*Canvas) Tab

func (c *Canvas) Tab(tabstop int)

func (*Canvas) TermCursor

func (c *Canvas) TermCursor() (x, y int)

func (*Canvas) UseAlternateScreen

func (c *Canvas) UseAlternateScreen()

func (*Canvas) UseOriginalScreen

func (c *Canvas) UseOriginalScreen()

func (*Canvas) Write

func (c *Canvas) Write(p []byte) (n int, err error)

Write is an io.Writer for a terminal canvas, which processes the input as terminal codes, and writes with respect to the current cursor position.

type Charset

type Charset struct {
	SgrMapping bool
	Active     int
	Current    rune
	Mapping    []string
}

func NewTerminalCharset

func NewTerminalCharset() *Charset

func (*Charset) Activate

func (t *Charset) Activate(g int)

func (*Charset) ApplyMapping

func (t *Charset) ApplyMapping(r rune) rune

func (*Charset) Define

func (t *Charset) Define(g int, charset string)

func (*Charset) ResetSgrIbmpc

func (t *Charset) ResetSgrIbmpc()

func (*Charset) SetSgrIbmpc

func (t *Charset) SetSgrIbmpc()

type EventNotSupported

type EventNotSupported struct {
	Event interface{}
}

func (EventNotSupported) Error

func (e EventNotSupported) Error() string

type HotKey

type HotKey struct {
	K tcell.Key
}

func (HotKey) HotKey

func (t HotKey) HotKey() tcell.Key

type HotKeyCB added in v1.4.0

type HotKeyCB struct{}

type HotKeyDuration

type HotKeyDuration struct {
	D time.Duration
}

func (HotKeyDuration) HotKeyDuration

func (t HotKeyDuration) HotKeyDuration() time.Duration

type HotKeyInputFn added in v1.4.0

type HotKeyInputFn func(ev *tcell.EventKey, w IWidget, app gowid.IApp) bool

type ICSICommand

type ICSICommand interface {
	MinArgs() int
	FallbackArg() int
	IsAlias() bool
	Alias() byte
	Call(canvas *Canvas, args []int, qmark bool)
}

type IHotKeyFunctions added in v1.4.0

type IHotKeyFunctions interface {
	// Customized handling of hotkey sequences
	HotKeyFunctions() []HotKeyInputFn
}

type IHotKeyPersistence

type IHotKeyPersistence interface {
	HotKeyDuration() time.Duration
}

type IHotKeyProvider

type IHotKeyProvider interface {
	HotKey() tcell.Key
}

type IMouseSupport

type IMouseSupport interface {
	MouseEnabled() bool
	MouseIsSgr() bool
	MouseReportButton() bool
	MouseReportAny() bool
}

type IPaste added in v1.4.0

type IPaste interface {
	PasteState(...bool) bool
}

type IScrollbar added in v1.4.0

type IScrollbar interface {
	ScrollbarEnabled() bool
	EnableScrollbar(app gowid.IApp)
	DisableScrollbar(app gowid.IApp)
}

type ITerminal

type ITerminal interface {
	io.Writer
	Width() int
	Height() int
	Modes() *Modes
	Terminfo() *terminfo.Terminfo
}

ITerminal is the interface required by terminal.Canvas. For example, when the pty sends a byte sequence, the canvas needs to pass it on to the terminal implementation - hence io.Writer.

type IWidget

type IWidget interface {
	// All the usual widget requirements
	gowid.IWidget
	// Support terminal interfaces needed by terminal.Canvas
	ITerminal
	// IHotKeyProvider specifies the keypress that will "unfocus" the widget, that is that will
	// for a period of time ensure that the widget does not accept keypresses. This allows
	// the containing gowid application to change focus to another widget e.g. by hitting
	// the cursor key inside a pile or column widget.
	IHotKeyProvider
	// IHotKeyPersistence determines how long a press of the hotkey will be in effect before
	// keyboard user input is sent back to the underlying terminal.
	IHotKeyPersistence
	// IPaste tracks whether the paste start sequence has been seen wthout a matching
	// paste end sequence
	IPaste
	// HotKeyActive returns true if the hotkey is currently in effect.
	HotKeyActive() bool
	// SetHotKeyActive sets the state of HotKeyActive.
	SetHotKeyActive(app gowid.IApp, down bool)
	// HotKeyDownTime returns the time at which the hotkey was pressed.
	HotKeyDownTime() time.Time
	// Scroll the terminal's buffer.
	Scroll(dir ScrollDir, page bool, lines int)
	// Reset the the terminal's buffer scroll; display what was current.
	ResetScroll()
	// Currently scrolled away from normal view
	Scrolling() bool
}

IWidget encapsulates the requirements of a gowid widget that can represent and interact with a terminal.

type LEDSState

type LEDSState int
const (
	LEDSClear      LEDSState = 0
	LEDSScrollLock LEDSState = 1
	LEDSNumLock    LEDSState = 2
	LEDSCapsLock   LEDSState = 3
)

type LEDs

type LEDs struct{}

type Modes

type Modes struct {
	DisplayCtrl        bool
	Insert             bool
	LfNl               bool
	KeysAutoWrap       bool
	ReverseVideo       bool
	ConstrainScrolling bool
	DontAutoWrap       bool
	InvisibleCursor    bool
	Charset            int
	VT200Mouse         bool // #define SET_VT200_MOUSE             1000
	ReportButton       bool // #define SET_BTN_EVENT_MOUSE         1002
	ReportAny          bool // #define SET_ANY_EVENT_MOUSE         1003
	SgrModeMouse       bool // #define SET_SGR_EXT_MODE_MOUSE      1006
}

Modes is used to track the state of this terminal - which modes are enabled, etc. It tracks the mouse state in particular so implements IMouseSupport.

func (Modes) MouseEnabled

func (t Modes) MouseEnabled() bool

func (Modes) MouseIsSgr

func (t Modes) MouseIsSgr() bool

func (Modes) MouseReportAny

func (t Modes) MouseReportAny() bool

func (Modes) MouseReportButton

func (t Modes) MouseReportButton() bool

type Options

type Options struct {
	Command                 []string
	Env                     []string
	HotKey                  IHotKeyProvider
	HotKeyPersistence       IHotKeyPersistence // the period of time a hotKey sticks after the first post-hotKey keypress
	Scrollback              int
	Scrollbar               bool            // disabled regardless of setting if there is no scrollback
	HotKeyFns               []HotKeyInputFn // allow custom behavior after pressing the hotkey
	EnableBracketedPaste    bool
	KeyPressToEndScrollMode bool // set to true to enable legacy behavior - when the user has scrolled

}

type ProcessExited

type ProcessExited struct{}

type RegularCSICommand

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

func (RegularCSICommand) Alias

func (c RegularCSICommand) Alias() byte

func (RegularCSICommand) Call

func (c RegularCSICommand) Call(canvas *Canvas, args []int, qmark bool)

func (RegularCSICommand) FallbackArg

func (c RegularCSICommand) FallbackArg() int

func (RegularCSICommand) IsAlias

func (c RegularCSICommand) IsAlias() bool

func (RegularCSICommand) MinArgs

func (c RegularCSICommand) MinArgs() int

type ScrollDir

type ScrollDir bool
const (
	ScrollDown ScrollDir = false
	ScrollUp   ScrollDir = true
)

type StartCommandError

type StartCommandError struct {
	Command []string
	Err     error
}

func (StartCommandError) Cause

func (e StartCommandError) Cause() error

func (StartCommandError) Error

func (e StartCommandError) Error() string

func (StartCommandError) Unwrap added in v1.2.0

func (e StartCommandError) Unwrap() error

type Title

type Title struct{}

type ViewPortCanvas

type ViewPortCanvas struct {
	*gowid.Canvas
	Offset int
	Height int
}

ViewPortCanvas implements ICanvas by embedding a Canvas pointer, but reimplementing Line and Cell access APIs relative to an Offset and a Height. The Height specifies the number of visible rows in the ViewPortCanvas; the rows that are not visible are logically "above" the visible rows. If Offset is reduced, the view of the underlying large Canvas is shifted up. This type is used by the terminal widget to hold the terminal's scrollback buffer.

func NewViewPort

func NewViewPort(c *gowid.Canvas, offset, height int) *ViewPortCanvas

func (*ViewPortCanvas) BoxRows

func (v *ViewPortCanvas) BoxRows() int

func (*ViewPortCanvas) CellAt

func (v *ViewPortCanvas) CellAt(col, row int) gowid.Cell

func (*ViewPortCanvas) Duplicate added in v1.3.0

func (c *ViewPortCanvas) Duplicate() gowid.ICanvas

func (*ViewPortCanvas) Line

func (*ViewPortCanvas) MergeUnder added in v1.3.0

func (c *ViewPortCanvas) MergeUnder(c2 gowid.IMergeCanvas, leftOffset, topOffset int, bottomGetsCursor bool)

func (*ViewPortCanvas) SetCellAt

func (v *ViewPortCanvas) SetCellAt(col, row int, c gowid.Cell)

func (*ViewPortCanvas) SetLineAt

func (v *ViewPortCanvas) SetLineAt(row int, line []gowid.Cell)

func (*ViewPortCanvas) String

func (c *ViewPortCanvas) String() string

type Widget

type Widget struct {
	IHotKeyProvider
	IHotKeyPersistence

	Cmd *exec.Cmd

	Callbacks *gowid.Callbacks
	gowid.IsSelectable
	// contains filtered or unexported fields
}

Widget is a widget that hosts a terminal-based application. The user provides the command to run, an optional environment in which to run it, and an optional hotKey. The hotKey is used to "escape" from the terminal (if using only the keyboard), and serves a similar role to the default ctrl-b in tmux. For example, to move focus to a widget to the right, the user could hit ctrl-b <right>. See examples/gowid-editor for a demo.

func New

func New(command []string) (*Widget, error)

func NewExt

func NewExt(opts Options) (*Widget, error)

func (*Widget) Bell

func (w *Widget) Bell(app gowid.IApp)

func (*Widget) Canvas

func (w *Widget) Canvas() *Canvas

func (*Widget) Connected

func (w *Widget) Connected() bool

func (*Widget) DisableScrollbar added in v1.4.0

func (w *Widget) DisableScrollbar(app gowid.IApp)

func (*Widget) EnableScrollbar added in v1.4.0

func (w *Widget) EnableScrollbar(app gowid.IApp)

func (*Widget) GetLEDs

func (w *Widget) GetLEDs() LEDSState

func (*Widget) GetTitle

func (w *Widget) GetTitle() string

func (*Widget) Height

func (w *Widget) Height() int

func (*Widget) HotKeyActive

func (w *Widget) HotKeyActive() bool

func (*Widget) HotKeyDownTime

func (w *Widget) HotKeyDownTime() time.Time

func (*Widget) HotKeyFunctions added in v1.4.0

func (w *Widget) HotKeyFunctions() []HotKeyInputFn

func (*Widget) Modes

func (w *Widget) Modes() *Modes

func (*Widget) OnBell

func (w *Widget) OnBell(f gowid.IWidgetChangedCallback)

func (*Widget) OnHotKey added in v1.4.0

func (w *Widget) OnHotKey(f gowid.IWidgetChangedCallback)

func (*Widget) OnProcessExited

func (w *Widget) OnProcessExited(f gowid.IWidgetChangedCallback)

func (*Widget) OnSetTitle

func (w *Widget) OnSetTitle(f gowid.IWidgetChangedCallback)

func (*Widget) PasteState added in v1.4.0

func (w *Widget) PasteState(b ...bool) bool

func (*Widget) RemoveOnBell

func (w *Widget) RemoveOnBell(f gowid.IIdentity)

func (*Widget) RemoveOnHotKey added in v1.4.0

func (w *Widget) RemoveOnHotKey(f gowid.IIdentity)

func (*Widget) RemoveOnProcessExited

func (w *Widget) RemoveOnProcessExited(f gowid.IIdentity)

func (*Widget) RemoveOnSetTitle

func (w *Widget) RemoveOnSetTitle(f gowid.IIdentity)

func (*Widget) Render

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas

func (*Widget) RenderSize

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox

func (*Widget) RequestTerminate

func (w *Widget) RequestTerminate() error

func (*Widget) ResetScroll

func (w *Widget) ResetScroll()

func (*Widget) Scroll

func (w *Widget) Scroll(dir ScrollDir, page bool, lines int)

func (*Widget) ScrollbarEnabled added in v1.4.0

func (w *Widget) ScrollbarEnabled() bool

func (*Widget) Scrolling

func (w *Widget) Scrolling() bool

func (*Widget) SetCanvas

func (w *Widget) SetCanvas(app gowid.IApp, c *Canvas)

func (*Widget) SetHotKeyActive

func (w *Widget) SetHotKeyActive(app gowid.IApp, down bool)

func (*Widget) SetLEDs

func (w *Widget) SetLEDs(app gowid.IApp, mode LEDSState)

func (*Widget) SetTerminalSize

func (w *Widget) SetTerminalSize(width, height int) error

func (*Widget) SetTitle

func (w *Widget) SetTitle(title string, app gowid.IApp)

func (*Widget) Signal

func (w *Widget) Signal(sig syscall.Signal) error

func (*Widget) StartCommand

func (w *Widget) StartCommand(app gowid.IApp, width, height int) error

func (*Widget) StopCommand

func (w *Widget) StopCommand()

func (*Widget) String

func (w *Widget) String() string

func (*Widget) Terminfo

func (w *Widget) Terminfo() *terminfo.Terminfo

func (*Widget) TouchTerminal

func (w *Widget) TouchTerminal(width, height int, app gowid.IApp)

func (*Widget) UserInput

func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool

func (*Widget) Width

func (w *Widget) Width() int

func (*Widget) Write

func (w *Widget) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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