term

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: MIT Imports: 42 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CachedQuery

func CachedQuery(qu Querier, qs string, tty TTY, p Parser, prIn, prOut environ.Properties) (string, error)

func DisableDrawer

func DisableDrawer(name string)

func DisableTerminal

func DisableTerminal(name string)

func Draw

func Draw(img image.Image, bounds image.Rectangle, term *Terminal, dr Drawer) error

Draw draws an image on the terminal. bounds is the drawing area in cells. if the passed drawer is nil, the Terminals drawer is used.

func QueryDeviceAttributes

func QueryDeviceAttributes(qu Querier, tty TTY, prIn, prOut environ.Properties) error

QueryDeviceAttributes should only be used for external TermCheckers

func RegisterDrawer

func RegisterDrawer(d Drawer)

RegisterDrawer ...

func RegisterTermChecker

func RegisterTermChecker(t TermChecker)

RegisterTerminal ...

func ResetDrawerList

func ResetDrawerList()

func ResetTerminalCheckerList

func ResetTerminalCheckerList()

ResetTerminalCheckerList

Types

type CachedQuerier

type CachedQuerier interface {
	CachedQuery(string, TTY, Parser, environ.Properties) (string, error)
}

CachedQuerier ...

func NewCachedQuerier

func NewCachedQuerier(qu Querier) CachedQuerier

type Canvas added in v0.0.3

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

Canvas has to be created with (*term.Terminal).NewCanvas()

func (*Canvas) At added in v0.0.3

func (c *Canvas) At(x, y int) color.Color

func (*Canvas) Bounds added in v0.0.3

func (c *Canvas) Bounds() image.Rectangle

func (*Canvas) CellArea added in v0.0.3

func (c *Canvas) CellArea() image.Rectangle

func (*Canvas) Close added in v0.0.4

func (c *Canvas) Close() error

func (*Canvas) ColorModel added in v0.0.3

func (c *Canvas) ColorModel() color.Model

func (*Canvas) Draw added in v0.0.3

func (c *Canvas) Draw(img image.Image) error

Draw flushes stored image when img is nil

func (*Canvas) Offset added in v0.0.3

func (c *Canvas) Offset() image.Point

func (*Canvas) Screenshot added in v0.0.3

func (c *Canvas) Screenshot() (image.Image, error)

func (*Canvas) Set added in v0.0.3

func (c *Canvas) Set(x, y int, col color.Color)

func (*Canvas) SetCellArea added in v0.0.5

func (c *Canvas) SetCellArea(bounds image.Rectangle) error

func (*Canvas) SetImage added in v0.0.4

func (c *Canvas) SetImage(img image.Image) error

func (*Canvas) Video added in v0.0.3

func (c *Canvas) Video(ctx context.Context, vid <-chan image.Image, frameDur time.Duration) error

type Drawer

type Drawer interface {
	Name() string
	New() Drawer
	IsApplicable(DrawerCheckerInput) (bool, environ.Properties)
	Draw(img image.Image, bounds image.Rectangle, term *Terminal) error
	Prepare(ctx context.Context, img image.Image, bounds image.Rectangle, term *Terminal) (drawFn func() error, _ error)
}

Drawer ...

func AllDrawers

func AllDrawers() []Drawer

AllDrawers returns all registered drawers

func DrawersFor

func DrawersFor(inp DrawerCheckerInput) ([]Drawer, error)

DrawersFor ...

func EnabledDrawers

func EnabledDrawers() []Drawer

EnabledDrawers returns all enabled registered drawers

func GetRegDrawerByName

func GetRegDrawerByName(name string) Drawer

GetRegDrawerByName returns registered drawers

type DrawerCheckerInput

type DrawerCheckerInput interface {
	Name() string
	environ.Properties
	Querier
	TTY
	wm.Window // TODO remove Close()
}

type Image

type Image struct {
	Original image.Image
	Resized  image.Image
	Cropped  image.Image
	FileName string // lazily loaded
	Encoded  []byte // lazily loaded

	internal.Closer
	// contains filtered or unexported fields
}

Image ...

func NewImage

func NewImage(img image.Image) *Image

NewImage ...

func NewImageBytes

func NewImageBytes(imgBytes []byte) *Image

NewImageBytes - for lazy loading the file

func NewImageFilename added in v0.0.3

func NewImageFilename(imgFile string) *Image

NewImageFilename - for lazy loading the file

func (*Image) At

func (i *Image) At(x, y int) color.Color

At ...

func (*Image) Bounds

func (i *Image) Bounds() image.Rectangle

Bounds ...

func (*Image) ColorModel

func (i *Image) ColorModel() color.Model

ColorModel ...

func (*Image) Decode

func (i *Image) Decode() error

Decode decodes and stores the image file in the struct. this is not required for some drawers where the file path is passed to the terminal.

Decode requires registration of image decoders.

func (*Image) DrawerObject added in v0.0.4

func (i *Image) DrawerObject(d Drawer) (any, error)

DrawerObject ...

func (*Image) Fit

func (i *Image) Fit(bounds image.Rectangle, rsz Resizer, sv Surveyor) error

nil Resizer is allowed (default resizer crops instead of resize)

func (*Image) Image

func (i *Image) Image() (image.Image, error)

Image ...

func (*Image) Inband added in v0.0.4

func (i *Image) Inband(placementCells image.Rectangle, d Drawer, t *Terminal) (string, error)

Inband ...

func (*Image) PosObject added in v0.0.4

func (i *Image) PosObject(placementCells image.Rectangle, d Drawer, t *Terminal) (any, error)

PosObject ...

func (*Image) SaveAsFile

func (i *Image) SaveAsFile(t *Terminal, fileExt string, enc ImageEncoder) (rm func() error, err error)

SaveAsFile writes the image to a temporary file. Defer Image.Close() or call rm() when no longer needed.

func (*Image) SetDrawerObject added in v0.0.3

func (i *Image) SetDrawerObject(obj any, d Drawer) error

SetDrawerObject ...

func (*Image) SetInband

func (i *Image) SetInband(placementCells image.Rectangle, inband string, d Drawer, t *Terminal) error

SetInband ...

func (*Image) SetPosObject added in v0.0.3

func (i *Image) SetPosObject(placementCells image.Rectangle, obj any, d Drawer, t *Terminal) error

SetPosObject ...

type ImageEncoder

type ImageEncoder = internal.ImageEncoder

type OptFunc added in v0.0.3

type OptFunc func(*Terminal) error

func (OptFunc) ApplyOption added in v0.0.3

func (o OptFunc) ApplyOption(t *Terminal) error

type Option added in v0.0.3

type Option interface {
	ApplyOption(t *Terminal) error
}
var (
	TUIMode              Option = tuiMode
	CLIMode              Option = cliMode
	ManualComposition    Option = manualComposition    // disable terminal detection
	NoCleanUpOnInterrupt Option = noCleanUpOnInterrupt // disable terminal cleanup on Interrupt or TERM signal
)

func AfterSetup added in v0.0.5

func AfterSetup(f func(*Terminal)) Option

func SetArgs added in v0.0.3

func SetArgs(args []string) Option

func SetDrawers added in v0.0.3

func SetDrawers(drs []Drawer) Option

func SetExe added in v0.0.3

func SetExe(exe string) Option

func SetLogFile added in v0.0.4

func SetLogFile(filename string, enable bool) Option

func SetPTYName added in v0.0.3

func SetPTYName(ptyName string) Option

func SetProprietor added in v0.0.3

func SetProprietor(pr environ.Properties, merge bool) Option

func SetQuerier added in v0.0.3

func SetQuerier(qu Querier, enforce bool) Option

func SetResizer added in v0.0.3

func SetResizer(rsz Resizer) Option

func SetSLogHandler added in v0.0.4

func SetSLogHandler(h slog.Handler, enable bool) Option

func SetSurveyor added in v0.0.3

func SetSurveyor(ps PartialSurveyor, enforce bool) Option

func SetTTY added in v0.0.3

func SetTTY(tty TTY, enforce bool) Option

func SetTTYProvider added in v0.0.3

func SetTTYProvider[T TTY, F func(ptyName string) (T, error)](ttyProv F, enforce bool) Option

ttyProv is implemented by the New constructors in the subpackages of the tty package

func SetTerminalName added in v0.0.3

func SetTerminalName(termName string) Option

func SetWindow added in v0.0.3

func SetWindow(w wm.Window) Option

func SetWindowProvider added in v0.0.3

func SetWindowProvider(wProv wm.WindowProvider, enforce bool) Option

type Options added in v0.0.2

type Options []Option

func (Options) ApplyOption added in v0.0.3

func (o Options) ApplyOption(t *Terminal) error

type Parser

type Parser interface {
	Parse(rune) bool
}

Parser.Parse returns true when end of terminal reply is reached.

type ParserFunc

type ParserFunc func(rune) bool

ParserFunc returns true when end of terminal reply is reached.

func (ParserFunc) Parse

func (f ParserFunc) Parse(r rune) bool

Parse returns true when end of terminal reply is reached.

type PartialSurveyor

type PartialSurveyor interface {
	// TODO doc
	// TODO wm.Window func
	IsPartialSurveyor()
}

PartialSurveyor implements some of:

  • CellSize(tty TTY) (width, height float64, err error)
  • CellSizeQuery(qu Querier, tty TTY) (width, height float64, err error)
  • SizeInCells(tty TTY) (widthCells, heightCells uint, err error)
  • SizeInCellsQuery(qu Querier, tty TTY) (widthCells, heightCells uint, err error)
  • SizeInPixels(tty TTY) (widthPixels, heightPixels uint, err error)
  • SizeInPixelsQuery(qu Querier, tty TTY) (widthPixels, heightPixels uint, err error)
  • SizeInPixelsWindow(w wm.Window) (widthPixels, heightPixels uint, err error)
  • SizeInCellsAndPixels(tty TTY) (widthCells, heightCells, widthPixels, heightPixels uint, err error)
  • Cursor(tty TTY) (xPosCells, yPosCells uint, err error)
  • CursorQuery(qu Querier, tty TTY) (widthCells, heightCells uint, err error)
  • SetCursor(xPosCells, yPosCells uint, tty TTY) (err error)
  • SetCursorQuery(xPosCells, yPosCells uint, qu Querier, tty TTY) (err error)
  • ResizeEvents(tty TTY) (<-chan Resolution, error)
  • ResizeEventsWindow(w wm.Window) (<-chan Resolution, error)

func DefaultSurveyor added in v0.0.3

func DefaultSurveyor() PartialSurveyor

type Querier

type Querier interface {
	Query(string, TTY, Parser) (string, error)
}

Querier sends escapes sequences to the terminal and returns the answer.

type Resizer

type Resizer interface {
	Resize(img image.Image, size image.Point) (image.Image, error)
}

Resizer resizes images

func ResizerDefault

func ResizerDefault() Resizer

type Resolution added in v0.0.4

type Resolution struct {
	TermInCellsW, TermInCellsH uint
	TermInPxlsW, TermInPxlsH   uint
	CellInPxlsW, CellInPxlsH   float64
}

type Surveyor

type Surveyor interface {
	// passes stored TTY, Querier, wm.Window, Proprietor to a SurveyorLight
	CellSize() (width, height float64, err error)
	SizeInCells() (width, height uint, err error)
	SizeInPixels() (width, height uint, err error)
	Cursor() (xPosCells, yPosCells uint, err error)
	SetCursor(xPosCells, yPosCells uint) (err error)
	CellScale(ptSrcPx, ptDstCl image.Point) (ptSrcCl image.Point, _ error)
}

Surveyor is implemented by Terminal

type SurveyorDefault

type SurveyorDefault struct {
}

func (*SurveyorDefault) CursorQuery added in v0.0.4

func (s *SurveyorDefault) CursorQuery(qu Querier, tty TTY) (widthCells, heightCells uint, err error)

CursorQuery

func (*SurveyorDefault) IsPartialSurveyor

func (s *SurveyorDefault) IsPartialSurveyor()

func (*SurveyorDefault) ResizeEvents added in v0.0.4

func (s *SurveyorDefault) ResizeEvents(tty TTY) (_ <-chan Resolution, closeFunc func() error, _ error)

func (*SurveyorDefault) SetCursorQuery

func (s *SurveyorDefault) SetCursorQuery(xPosCells, yPosCells uint, qu Querier, tty TTY) (err error)

SetCursorQuery

func (*SurveyorDefault) SizeInCellsAndPixels

func (s *SurveyorDefault) SizeInCellsAndPixels(tty TTY) (widthCells, heightCells, widthPixels, heightPixels uint, err error)

SizeInCellsAndPixels ...

func (*SurveyorDefault) SizeInCellsQuery

func (s *SurveyorDefault) SizeInCellsQuery(qu Querier, tty TTY) (widthCells, heightCells uint, e error)

SizeInCellsQuery - dtterm window manipulation CSI 18 t

func (*SurveyorDefault) SizeInPixelsQuery

func (s *SurveyorDefault) SizeInPixelsQuery(qu Querier, tty TTY) (widthPixels, heightPixels uint, e error)

SizeInPixelsQuery - dtterm window manipulation CSI 14 t

func (*SurveyorDefault) SizeInPixelsWindow added in v0.0.3

func (s *SurveyorDefault) SizeInPixelsWindow(w wm.Window) (widthPixels, heightPixels uint, err error)

SizeInPixelsWindow

type SurveyorLight

type SurveyorLight interface {
	CellSize(TTY, Querier, wm.Window, environ.Properties) (width, height float64, err error)
	SizeInCells(TTY, Querier, wm.Window, environ.Properties) (width, height uint, err error)
	SizeInPixels(TTY, Querier, wm.Window, environ.Properties) (width, height uint, err error)
	Cursor(TTY, Querier, wm.Window, environ.Properties) (xPosCells, yPosCells uint, err error)
	SetCursor(xPosCells, yPosCells uint, tty TTY, qu Querier, w wm.Window, pr environ.Properties) (err error)
	WatchResizeEventsStart(TTY, Querier, wm.Window, environ.Properties) (_ <-chan Resolution, closeFunc func() error, _ error)
	WatchResizeEventsStop() error
}

type SurveyorNoANSI

type SurveyorNoANSI struct{}

func (*SurveyorNoANSI) IsPartialSurveyor

func (s *SurveyorNoANSI) IsPartialSurveyor()

func (*SurveyorNoANSI) ResizeEvents added in v0.0.4

func (s *SurveyorNoANSI) ResizeEvents(tty TTY) (_ <-chan Resolution, closeFunc func() error, _ error)

func (*SurveyorNoANSI) SizeInCellsAndPixels

func (s *SurveyorNoANSI) SizeInCellsAndPixels(tty TTY) (widthCells, heightCells, widthPixels, heightPixels uint, err error)

SizeInCellsAndPixels ...

type SurveyorNoTIOCGWINSZ

type SurveyorNoTIOCGWINSZ struct{}

func (*SurveyorNoTIOCGWINSZ) IsPartialSurveyor

func (s *SurveyorNoTIOCGWINSZ) IsPartialSurveyor()

func (*SurveyorNoTIOCGWINSZ) ResizeEvents added in v0.0.4

func (s *SurveyorNoTIOCGWINSZ) ResizeEvents(tty TTY) (_ <-chan Resolution, closeFunc func() error, _ error)

func (*SurveyorNoTIOCGWINSZ) SizeInCellsQuery

func (s *SurveyorNoTIOCGWINSZ) SizeInCellsQuery(qu Querier, tty TTY) (widthCells, heightCells uint, e error)

SizeInCellsQuery - dtterm window manipulation CSI 18 t

func (*SurveyorNoTIOCGWINSZ) SizeInPixelsQuery

func (s *SurveyorNoTIOCGWINSZ) SizeInPixelsQuery(qu Querier, tty TTY) (widthPixels, heightPixels uint, e error)

SizeInPixelsQuery - dtterm window manipulation CSI 14 t

type TTY

type TTY interface {
	io.ReadWriteCloser
	TTYDevName() string
}

TTY ... optional methods:

  • ResizeEvents() (_ <-chan Resolution, closeFunc func() error, _ error)
  • SizePixel() (cw int, ch int, pw int, ph int, e error)
  • ReadRune() (r rune, size int, err error) // io.RuneReader

type TermChecker

type TermChecker interface {
	// TODO: implement all optional methods through the core and check for nil?
	// The following methods are implemented by embedded *termCheckerCore.
	Name() string
	CheckExclude(environ.Properties) (mightBe bool, p environ.Properties)
	CheckIsQuery(Querier, TTY, environ.Properties) (is bool, p environ.Properties)
	CheckIsWindow(wm.Window) (is bool, p environ.Properties)
	Check(qu Querier, tty TTY, inp environ.Properties) (is bool, p environ.Properties)
	NewTerminal(...Option) (*Terminal, error)
	Init(tc TermChecker) // called during registration
}

TermChecker must implement at least one of CheckExclude, CheckIs, CheckWindow. For passing on properties create a new Proprietor with NewProprietor. CheckExclude is a preliminary check if the to be checked terminal might be the matching terminal of the TermChecker. CheckExclude can set properties for later exclusion of CheckIs checks, etc, no ANSI querying shall be done during this stage. CheckIs is the final check, ANSI querying is allowed, if not prohibited by the TerminalCheckerInput properties. Alternatively a wm.Window can be compared against with CheckWindow.

  • CheckExclude(TerminalCheckerInput) (mightBe bool, p Proprietor)
  • CheckIs(Querier, TTY, TerminalCheckerInput) (is bool, p Proprietor)
  • CheckWindow(wm.Window) (is bool, p Proprietor)

A new TermChecker has to embed NewTermCheckerCore(name string) providing the TermChecker type identity and the Name() method.

RegisterTermChecker(TermChecker) is used for the registration of the new TermChecker.

Optional methods for setting specific implementations:

  • TTY(pytName string, ci environ.Proprietor) (TTY, error)
  • Querier(environ.Proprietor) Querier
  • Surveyor(environ.Proprietor) PartialSurveyor
  • Window(environ.Proprietor) (wm.Window, error)
  • Args(environ.Proprietor) []string
  • Exe(environ.Proprietor) string

func AllRegisteredTermCheckers added in v0.0.3

func AllRegisteredTermCheckers() []TermChecker

func AllTerminalCheckers

func AllTerminalCheckers() []TermChecker

AllTerminals returns all enabled registered dummy terminals

func NewTermCheckerCore

func NewTermCheckerCore(name string) TermChecker

func RegisteredTermChecker added in v0.0.3

func RegisteredTermChecker(name string) TermChecker

type Terminal

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

func NewTerminal

func NewTerminal(opts ...Option) (*Terminal, error)

NewTerminal tries to recognize the terminal that manages the device ptyName. It will use suggestions provided by the optional TerminalChecker methods:

  • TTY(ptyName string, ci environ.Proprietor) (TTY, error)
  • Querier(environ.Proprietor) Querier
  • Surveyor(environ.Proprietor) PartialSurveyor
  • Window(environ.Proprietor) (wm.Window, error)
  • Args(environ.Proprietor) []string
  • Exe(environ.Proprietor) string // alternative executable name if it differs from Name()

"Enforced" Options have precedence over the TermCheckers suggestion.

func (*Terminal) CellScale added in v0.0.3

func (t *Terminal) CellScale(ptSrcPx, ptDstCl image.Point) (ptSrcCl image.Point, _ error)

CellScale returns a cell size for pixel size <ptSrcPx> to the cell size <ptDstCl> while maintaining the scale. With no passed 0 side length values, the largest subarea is returned. With one passed 0 side length value, the other side length will be fixed. With two passed 0 side length values, pixels in source and destination area at the same position correspond to each other.

func (*Terminal) CellSize

func (t *Terminal) CellSize() (width, height float64, _ error)

func (*Terminal) Close

func (t *Terminal) Close() error

func (*Terminal) CreateTemp

func (t *Terminal) CreateTemp(pattern string) (*os.File, error)

CreateTemp ...

func (*Terminal) Cursor added in v0.0.4

func (t *Terminal) Cursor() (xPosCells, yPosCells uint, err error)

func (*Terminal) Draw

func (t *Terminal) Draw(img image.Image, bounds image.Rectangle) error

func (*Terminal) Drawers

func (t *Terminal) Drawers() []Drawer

func (*Terminal) Env added in v0.0.5

func (t *Terminal) Env() environ.Enver

func (*Terminal) Logger added in v0.0.4

func (t *Terminal) Logger() *slog.Logger

func (*Terminal) Name

func (t *Terminal) Name() string

func (*Terminal) NewCanvas added in v0.0.3

func (t *Terminal) NewCanvas(bounds image.Rectangle) (*Canvas, error)

func (*Terminal) Printf

func (t *Terminal) Printf(format string, a ...any) (int, error)

func (*Terminal) Query

func (t *Terminal) Query(qs string, p Parser) (string, error)

func (*Terminal) Resizer added in v0.0.3

func (t *Terminal) Resizer() Resizer

func (*Terminal) Scroll added in v0.0.4

func (t *Terminal) Scroll(lineCnt int) error

If lineCnt == 0 scroll until cursor is out of view.

func (*Terminal) SetCursor

func (t *Terminal) SetCursor(xPosCells, yPosCells uint) (err error)

func (*Terminal) SetOptions added in v0.0.3

func (t *Terminal) SetOptions(opts ...Option) error

func (*Terminal) SizeInCells

func (t *Terminal) SizeInCells() (width, height uint, err error)

func (*Terminal) SizeInPixels

func (t *Terminal) SizeInPixels() (width, height uint, err error)

func (*Terminal) TTY added in v0.0.4

func (t *Terminal) TTY() TTY

func (*Terminal) Window

func (t *Terminal) Window() wm.Window

func (*Terminal) Write

func (t *Terminal) Write(p []byte) (n int, err error)

func (*Terminal) WriteString

func (t *Terminal) WriteString(s string) (n int, err error)

Jump to

Keyboard shortcuts

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