termfun

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

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

Go to latest
Published: Apr 13, 2023 License: MIT Imports: 9 Imported by: 0

README

TermFun

TermFun is a package to do some fun things in your Go Terminal.

ReadKey

func ReadKey(reader *bufio.Reader) (r rune, size int, err error)

ReadKey is a drop-in replacement for bufio.ReadRune but returns common keyboard keypresses that are multi-rune as a single utf-16 surrogate rune.

Example: ./examples/readkey.go Example: ./examples/pong.go

	KeyUnknown
	KeyUp
	KeyDown
	KeyLeft
	KeyRight
	KeyBackTab
	KeyDel

CSI Codes

A collection of CSI functions to control the raw terminal display. All functions return strings that can be printed to the raw display.

See: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences

Example: ./examples/box.go

Example: ./examples/tile.go

// CUU - Cursor Up
func CUU(n int) string 

// CUD - Cursor Down
func CUD(n int) string

// CUF - Cursor Forward
func CUF(n int) string

// CUB - Cursor Back
func CUB(n int) string

// CNL - Cursor Next Line
func CNL(n int) string 

// CPL - Cursor Previous Line
func CPL(n int) string

// CHA - Cursor Horizontal Absolute
func CHA(n int) string

// CUP - Cursor Position
func CUP(m, n int) string 

// ED - Erase in Display
func ED(n EraseType) string

// SU - Scroll Up
func SU(n int) string 

// HVP - Horizontal Vertical Position
func HVP(m, n int) string

// SGR - Select Graphic Rendition, other data may follow
func SGR(n ...SGRType) string 

Canvas

The Canvas type and methods allow simple 2d graphics with unicode block characters.

Example: ./examples/life.go Example: ./examples/pong.go

// Return a new initialized Canvas
func NewCanvas(w, h int) *Canvas

// Init an existing Canvas, w and h are in pixels
// So Init(80,80) renders an array of 40x40 characters as StringDense
// and an array of 80x40 characters as StringAspect
func (c *Canvas) Init(w, h int)

// Width returns the width of the canvas, in pixels
func (c *Canvas) Width() int  { return c.pwidth }

// Height returns the height of the canvas, in pixels
func (c *Canvas) Height() int { return c.pheight }

// PlotXor sets the plot mode to xor the new pixel on the existing pixel
func (c *Canvas) PlotXor()    { c.xor = true }

// PlotOr sets the plot mode to or the new pixel on the existing pixel (default)
func (c *Canvas) PlotOr()     { c.xor = false }

// Plotwrap sets the plot mode to wrap cordinates on the borders
func (c *Canvas) PlotWrap()   { c.wrap = true }

// Plotwrap sets the plot mode to not wrap cordinates on the borders (default)
func (c *Canvas) PlotUnwrap() { c.wrap = false }

// Plot a pixel on the Canvas
func (c *Canvas) Plot(x, y int)

// Read a location from the Canvas to see if it is set
func (c *Canvas) Read(x, y int) bool

// Clear the Canvas
func (c *Canvas) Clear()

// Render the Canvas to a string as "square" (1x2) pixels
func (c *Canvas) StringAspect() string

// Render the Canvas to a string as "square" (1x2) pixels, with a border
func (c *Canvas) StringAspectBorder() string

// Render the Canvas to a string as dense (2x2) rectangle pixels
func (c *Canvas) StringDense() string

// Render the Canvas to a string as dense (2x2) rectangle pixels, with a border
func (c *Canvas) StringDenseBorder() string

// Line uses Bresenham's algorithm to plot a line
func (c *Canvas) Line(x0, y0, x1, y1 int) 

// Bmp plots a [][]byte to the Canvas
func (c *Canvas) Bmp(x, y int, bmp [][]byte)

TileTerm

The TileTerm type and methods allow rendering multiple tiled display regions. CtrlT will cycle between all the tiles, giving "focus" to each tile in order. CtrlU will make the current focus tile bigger and squish the surrounding tiles. CtrlU again will undo that. The focus tile will act differently on the keypresses depending on its TileType and the specific application.

Example: ./examples/tile.go

TileType_ScrollDown:

Use this for nicely flow formatting text within the horizontal boundary. The lines try to automatically break at spaces. You can scroll up/down to see more.

KeyUp: scroll up

KeyDown: scroll down

TileType_ScrollDownClip:

Use this to preserve existing line formatting. You may need to scroll left/right to see all of the clipped region.

KeyUp: scroll up

KeyDown: scroll down

KeyLeft: scroll left

KeyRight: scroll right

TileType_ScrollDownClipRaw:

Use this for completely custom key handling, you must handle all keys in keyCallBack. It can also be used to disable all keys in a tile.

All keys: call the keyCallBack function

TileType_ScrollUp:

Use this to scroll up, like a traditional terminal. The return key sends the line to the lineCallBack function for application handling. The up/down keys support a command ring buffer.

KeyEnter: send line to lineCallBack function and add line to ringbuffer.

KeyUp: cycle through ringbuffer and replace line

KeyDown: uncycle through ringbuffer and replace line

KeyBackspace: delete last character from line

TileTerm API
// NewTileTerm returns a new TileTerm session, typically pass in stdin and stdout
func NewTileTerm(in, out *os.File) *TileTerm

// AddTile adds a new tile to the TileTerm session
// name: Title name of the tile, or ""
// cursor: Cursor suffix for the tile, or ""
// outline: Box character set to use for tile border
// fraction: % of parent tile to take for this child
// location: location within parent tile to use for this child
// parent: which existing tile to use as parent for this child (root is nil)
// handler: which tile handler set to use
func (tTerm *TileTerm) AddTile(name string, cursor string, outline [6]int, fraction float32, location LocType, parent *Tile, handler TileType) (*Tile, error)

// DeleteTile deletes a tile and all of its children
func (tTerm *TileTerm) DeleteTile(tile *Tile)

// TileByIndex returns a tile by its index
func (tTerm *TileTerm) TileByIndex(index int) (tile *Tile)

// String returns the current string of the rendered TileTerm session
func (tTerm *TileTerm) String() string 

// Start begins the TileTerm session
func (tTerm *TileTerm) Start() error 

// Render renders the TileTerm session to Out
// It is a convenience function for String()
func (tTerm *TileTerm) Render() 
Tile API
// Width returns the Tile's Width
func (t *Tile) Width() int

// Height returns the Tile's Height
func (t *Tile) Height() int

// ResetBuffer resets the Tile's string buffer
func (t *Tile) ResetBuffer()

// Cursor returns the current Cursor string for the Tile
func (t *Tile) Cursor() string 

// Line returns the current Line string for the Tile
func (t *Tile) Line() string 

// SetKeyCallback sets the Key Callback function for TileType_ScrollDownClipRaw
func (t *Tile) SetKeyCallback(c KeyCallback) error

// SetLineCallback sets the Line Callback function for TileType_ScrollUp
func (t *Tile) SetLineCallback(c LineCallback) error

// Write to support io.Writer interface, so you can also, for instance,  fmt.Fprint(tile, "Hello")
func (tile *Tile) Write(buf []byte) (n int, err error)

// Print text to a tile buffer, convenience for Write
func (tile *Tile) Print(s ...any) 

// Println text to a tile buffer, convenience for Write
func (tile *Tile) Println(s ...any) 

// Printf text to a tile buffer, convenience for Write
func (tile *Tile) Printf(format string, s ...any)

Documentation

Index

Constants

View Source
const (
	SBox_Horiz = 0x2501 //single line
	SBox_Vert  = 0x2503
	SBox_UL    = 0x250F
	SBox_UR    = 0x2513
	SBox_LL    = 0x2517
	SBox_LR    = 0x251B

	DBox_Horiz = 0x2550 //double line
	DBox_Vert  = 0x2551
	DBox_UL    = 0x2554
	DBox_UR    = 0x2557
	DBox_LL    = 0x255A
	DBox_LR    = 0x255D

	BBox_Horiz = 0x2509 //broken line
	BBox_Vert  = 0x250B
	BBox_UL    = 0x250F
	BBox_UR    = 0x2513
	BBox_LL    = 0x2517
	BBox_LR    = 0x251B

	HBox_Horiz = 0x2501 //horiz only
	HBox_Vert  = 0x20
	HBox_UL    = 0x20
	HBox_UR    = 0x20
	HBox_LL    = 0x20
	HBox_LR    = 0x20
)
View Source
const (
	CtrlA = 0x01 + iota
	CtrlB
	CtrlC
	CtrlD
	CtrlE
	CtrlF
	CtrlG
	CtrlH
	CtrlI
	CtrlJ
	CtrlK
	CtrlL
	CtrlM
	CtrlN
	CtrlO
	CtrlP
	CtrlQ
	CtrlR
	CtrlS
	CtrlT
	CtrlU
	CtrlV
	CtrlW
	CtrlX
	CtrlY
	CtrlZ
)
View Source
const (
	KeyTab       = 9
	KeyEnter     = 13
	KeyEscape    = 27
	KeySpace     = 32
	KeyLBracket  = 91
	KeyBackspace = 127
)
View Source
const (
	KeyUnknown = 0xd800 + iota
	KeyUp
	KeyDown
	KeyLeft
	KeyRight
	KeyBackTab
	KeyDel
)
View Source
const (
	Box_Horiz = iota
	Box_Vert
	Box_UL
	Box_UR
	Box_LL
	Box_LR
)

box part indices within a char set

View Source
const CSI = "\x1b\x5b"

Variables

View Source
var BlocksAspect = [16][2]int{
	{0x20, 0x20},
	{0x2580, 0x20},
	{0x20, 0x2580},
	{0x2580, 0x2580},
	{0x2584, 0x20},
	{0x2588, 0x20},
	{0x2584, 0x2580},
	{0x2588, 0x2580},
	{0x20, 0x2584},
	{0x2580, 0x2584},
	{0x20, 0x2588},
	{0x2580, 0x2588},
	{0x2584, 0x2584},
	{0x2588, 0x2584},
	{0x2584, 0x2588},
	{0x2588, 0x2588},
}
View Source
var BlocksDense = [16]int{
	0x20,
	0x2598,
	0x259d,
	0x2580,
	0x2596,
	0x258c,
	0x259e,
	0x259b,
	0x2597,
	0x259a,
	0x2590,
	0x259c,
	0x2584,
	0x2599,
	0x259f,
	0x2588,
}

1 2 4 8

box char sets

Functions

func Box

func Box(r Rect, chars [6]int, text string, style ...SGRType) string

Box returns a box using a char set, with optional top title that can be styled

func CHA

func CHA(n int) string

CHA - Cursor Horizontal Absolute

func CNL

func CNL(n int) string

CNL - Cursor Next Line

func CPL

func CPL(n int) string

CPL - Cursor Previous Line

func CUB

func CUB(n int) string

CUB - Cursor Back

func CUD

func CUD(n int) string

CUD - Cursor Down

func CUF

func CUF(n int) string

CUF - Cursor Forward

func CUP

func CUP(m, n int) string

CUP - Cursor Position

func CUU

func CUU(n int) string

CUU - Cursor Up

func CharAt

func CharAt(x, y, c int) string

CharAt place a character at a location

func ClearRect

func ClearRect(r Rect) string

ClearRect returns a clear rectangle of spaces

func ED

func ED(n EraseType) string

ED - Erase in Display If n is 0, clear from cursor to end of screen. If n is 1, clear from cursor to beginning of the screen. If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).

func EL

func EL(n EraseType) string

EL - Erase in Line If n is 0 (or missing), clear from cursor to the end of the line. If n is 1, clear from cursor to beginning of the line. If n is 2, clear entire line. Cursor position does not change.

func HLine

func HLine(x1, x2, y, c int) string

HLine is a helper to make a horizontal line using a character

func HLineText

func HLineText(x1, x2, y, c int, text string, style ...SGRType) string

HLineText is a helper to make a horizontal line with text in the center SGRType is applied to the text

func HVP

func HVP(m, n int) string

HVP - Horizontal Vertical Position

func ReadKey

func ReadKey(reader *bufio.Reader) (r rune, size int, err error)

ReadKey is a drop-in replacement for bufio.ReadRune but returns common keyboard keypresses that are multi-rune as a single utf-16 surrogate rune per the consts above.

func SD

func SD(n int) string

SD - Scroll Down

func SGR

func SGR(n ...SGRType) string

SGR - Select Graphic Rendition, other data may follow

func SU

func SU(n int) string

SU - Scroll Up

func VLine

func VLine(x, y1, y2, c int) string

Vline is a helper to make a vertical line using a character

Types

type Canvas

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

Canvas holds a bit buffer for plotting Canvas assumes a virtual canvas of bits arranged as width 0 -> height 0 | v Upper left is 0,0 bits are arranged within a byte as:

x x

y 1 2 y 4 8 (y, x) (00)(01) (10)(11) if xor is true the new plot is xor'd on the existing canvas, otherwise it is just set if wrap is true the x, y coordinates are moded to wrap to the boundary

func NewCanvas

func NewCanvas(w, h int) *Canvas

Return a new initialized Canvas

func (*Canvas) Bmp

func (c *Canvas) Bmp(x, y int, bmp [][]byte)

Bmp plots a [][]byte to the Canvas

func (*Canvas) Clear

func (c *Canvas) Clear()

Clear the Canvas

func (*Canvas) Height

func (c *Canvas) Height() int

Height returns the height of the canvas, in pixels

func (*Canvas) Init

func (c *Canvas) Init(w, h int)

Init an existing Canvas, w and h are in pixels So Init(80,80) renders an array of 40x40 characters as StringDense and an array of 80x40 characters as StringAspect

func (*Canvas) Line

func (c *Canvas) Line(x0, y0, x1, y1 int)

Line uses Bresenham's algorithm to plot a line

func (*Canvas) Plot

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

Plot a pixel on the Canvas

func (*Canvas) PlotOr

func (c *Canvas) PlotOr()

PlotOr sets the plot mode to or the new pixel on the existing pixel (default)

func (*Canvas) PlotUnwrap

func (c *Canvas) PlotUnwrap()

Plotwrap sets the plot mode to not wrap cordinates on the borders (default)

func (*Canvas) PlotWrap

func (c *Canvas) PlotWrap()

Plotwrap sets the plot mode to wrap cordinates on the borders

func (*Canvas) PlotXor

func (c *Canvas) PlotXor()

PlotXor sets the plot mode to xor the new pixel on the existing pixel

func (*Canvas) Read

func (c *Canvas) Read(x, y int) bool

Read a location from the Canvas to see if it is set

func (*Canvas) StringAspect

func (c *Canvas) StringAspect() string

Render the Canvas to a string as "square" (1x2) pixels

func (*Canvas) StringAspectBorder

func (c *Canvas) StringAspectBorder() string

Render the Canvas to a string as "square" (1x2) pixels, with a border

func (*Canvas) StringDense

func (c *Canvas) StringDense() string

Render the Canvas to a string as dense (2x2) rectangle pixels

func (*Canvas) StringDenseBorder

func (c *Canvas) StringDenseBorder() string

Render the Canvas to a string as dense (2x2) rectangle pixels, with a border

func (*Canvas) Width

func (c *Canvas) Width() int

Width returns the width of the canvas, in pixels

type EraseType

type EraseType int
const (
	EraseToEnd   EraseType = 0
	EraseToBegin EraseType = 1
	EraseAll     EraseType = 2
)

type KeyCallback

type KeyCallback func(r rune) bool

returning true from any KeyCallback will exit TileTerm

type LineCallback

type LineCallback func(string) bool

returning true from any LineCallback will exit TileTerm

type LocType

type LocType int

LocType is a location within the parent

const (
	Loc_Top LocType = iota
	Loc_Bottom
	Loc_Left
	Loc_Right
)

type Point

type Point struct {
	X, Y int
}

type Rect

type Rect struct {
	Min, Max Point
}

func DecRect

func DecRect(r Rect) Rect

DecRect returns a rect decreased by 1, to allow for an outline

func IncRect

func IncRect(r Rect) Rect

IncRect returns a rect increased by 1

type SGRType

type SGRType int
const (
	SGR_Off          SGRType = 0  // All attributes off
	SGR_Bold         SGRType = 1  // Bold
	SGR_Underline    SGRType = 4  // Underline
	SGR_Blinking     SGRType = 5  // Blinking
	SGR_Negative     SGRType = 7  // Negative image
	SGR_Invisible    SGRType = 8  // Invisible image
	SGR_BoldOff      SGRType = 22 // Bold off
	SGR_UnderlineOff SGRType = 24 // Underline off
	SGR_BlinkingOff  SGRType = 25 // Blinking off
	SGR_NegativeOff  SGRType = 27 // Negative image off
	SGR_InvisibleOff SGRType = 28 // Invisible image off
)

type Tile

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

Tile contains the state for a Tile

func (*Tile) Clear

func (t *Tile) Clear() string

Clear clears the tile

func (*Tile) Cursor

func (t *Tile) Cursor() string

Cursor returns the current Cursor string for the Tile

func (*Tile) Height

func (t *Tile) Height() int

Height returns the Tile's Height

func (*Tile) Line

func (t *Tile) Line() string

Line returns the current Line string for the Tile

func (*Tile) Print

func (tile *Tile) Print(s ...any)

Print text to a tile buffer

func (*Tile) Printf

func (tile *Tile) Printf(format string, s ...any)

Printf text to a tile buffer

func (*Tile) Println

func (tile *Tile) Println(s ...any)

Println text to a tile buffer

func (*Tile) ResetBuffer

func (t *Tile) ResetBuffer()

ResetBuffer resets the Tile's string buffer

func (*Tile) SetKeyCallback

func (t *Tile) SetKeyCallback(c KeyCallback) error

SetKeyCallback sets the Key Callback function for TileType_ScrollDownClipRaw

func (*Tile) SetLineCallback

func (t *Tile) SetLineCallback(c LineCallback) error

SetLineCallback sets the Line Callback function for TileType_ScrollUp

func (*Tile) Width

func (t *Tile) Width() int

Width returns the Tile's Width

func (*Tile) Write

func (tile *Tile) Write(buf []byte) (n int, err error)

Write to support io.Writer interface

type TileHandler

type TileHandler struct {
	TileType TileType
	Render   func(*Tile) string
	KeyPress func(*Tile, rune) bool
}

type TileTerm

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

TileTerm contains the state for a TileTerm session

func NewTileTerm

func NewTileTerm(in, out *os.File) *TileTerm

NewTileTerm returns a new TileTerm session, typically pass in stdin and stdout

func (*TileTerm) AddTile

func (tTerm *TileTerm) AddTile(name string, cursor string, outline [6]int, fraction float32, location LocType, parent *Tile, handler TileType) (*Tile, error)

AddTile adds a new tile to the TileTerm session name: Title name of the tile, or "" cursor: Cursor suffix for the tile, or "" outline: Box character set to use for tile border fraction: % of parent tile to take for this child location: location within parent tile to use for this child parent: which existing tile to use as parent for this child (root is nil) handler: which tile handler set to use

func (*TileTerm) DeleteTile

func (tTerm *TileTerm) DeleteTile(tile *Tile)

DeleteTile deletes a tile and all of its children

func (*TileTerm) Render

func (tTerm *TileTerm) Render()

Render renders the TileTerm session to Out It is a convenience function for String()

func (*TileTerm) Start

func (tTerm *TileTerm) Start() error

Start begins the TileTerm session

func (*TileTerm) String

func (tTerm *TileTerm) String() string

String returns the current string of the rendered TileTerm session

func (*TileTerm) TileByIndex

func (tTerm *TileTerm) TileByIndex(index int) (tile *Tile)

TileByIndex returns a tile by its index

type TileType

type TileType int
const (
	TileType_ScrollDown TileType = iota
	TileType_ScrollDownClip
	TileType_ScrollDownClipRaw
	TileType_ScrollUp
)

Directories

Path Synopsis
Example of CSI codes to draw a box on the raw terminal
Example of CSI codes to draw a box on the raw terminal

Jump to

Keyboard shortcuts

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