ansipixels

package
v0.27.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

ansipixel provides terminal drawing and key reading abilities. fps/fps.go and life/life.go are examples/demos of how to use it.

Index

Constants

View Source
const (
	RoundTopLeft     = "╭"
	RoundTopRight    = "╮"
	RoundBottomLeft  = "╰"
	RoundBottomRight = "╯"

	SquareTopLeft     = "┌"
	SquareTopRight    = "┐"
	SquareBottomLeft  = "└"
	SquareBottomRight = "┘"

	Horizontal = "─"
	Vertical   = "│"

	TopT        = "┬"
	BottomT     = "┴"
	LeftT       = "├"
	RightT      = "┤"
	MiddleCross = "┼"

	FullPixel       = '█'
	TopHalfPixel    = '▀'
	BottomHalfPixel = '▄'

	QLowLeft  = '▖'
	QLowRight = '▗'
	QUpLeft   = '▘'
	QUpRight  = '▝'
)
View Source
const (
	Bold       = "\x1b[1m"
	Dim        = "\x1b[2m"
	Underlined = "\x1b[4m"
	Blink      = "\x1b[5m"
	Reverse    = "\x1b[7m"

	MoveLeft = "\033[1D"

	Reset = "\033[0m"
	// Foreground Colors.
	Black        = "\033[30m"
	Red          = "\033[31m"
	Green        = "\033[32m"
	Yellow       = "\033[33m"
	Blue         = "\033[34m"
	Purple       = "\033[35m"
	Cyan         = "\033[36m"
	Gray         = "\033[37m"
	DarkGray     = "\033[90m"
	BrightRed    = "\033[91m"
	BrightGreen  = "\033[92m"
	BrightYellow = "\033[93m"
	BrightBlue   = "\033[94m"
	BrightPurple = "\033[95m"
	BrightCyan   = "\033[96m"
	White        = "\033[97m"

	// Select colors from the 256 colors set that are missing from.
	Orange = "\033[38;5;214m"

	// Combo for RGB full pixel (used by fps).
	RedPixel   = Red + "█"
	GreenPixel = Green + "█"
	BluePixel  = Blue + "█"
	ResetClear = Reset + " "
)

Ansi codes.

View Source
const (
	MouseLeft       = 0b00
	MouseMiddle     = 0b01
	MouseRight      = 0b10
	MouseMove       = 0b100000
	MouseWheelUp    = 0b1000000
	MouseWheelDown  = 0b1000001
	Shift           = 0b000100
	Alt             = 0b001000
	Ctrl            = 0b010000
	AllModifiers    = Shift | Alt | Ctrl
	AnyModifierMask = ^AllModifiers
	// On a mac with a physical mouse, shift mousewheel is translated to button 6,7 which
	// here looks like we set the MouseRight bit (when shift-mousewheeling).
	MouseWheelMask = ^(AllModifiers | MouseRight)
)

Variables

This section is empty.

Functions

func AnsiClean added in v0.21.0

func AnsiClean(str []byte) ([]byte, int)

AnsiClean removes all Ansi code from a given byte slice. Useful among other things to get the correct string to pass to uniseq.StringWidth (ap.StringWidth does it for you). Returns the length of the processed input - unterminated sequences can be reprocessed using data from str[returnedVal:] plus additional data (see tests and nocolor for usage).

func DrawAALine added in v0.21.0

func DrawAALine(img *image.NRGBA, x0, y0, x1, y1 float64, c color.NRGBA)

Draw an anti-aliased line using Xiaolin Wu's algorithm. https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm This may or not be correct as it was mostly generated by our AI overlords.

func DrawLine added in v0.21.0

func DrawLine(img *image.NRGBA, x0, y0, x1, y1 float64, c color.NRGBA)

Non aliased version. Brute force implementation of Bresenham's line algorithm.

func FormatDate added in v0.20.0

func FormatDate(d *time.Time) string

func HSLToRGB added in v0.21.0

func HSLToRGB(h, s, l float64) color.NRGBA

HSLToRGB converts HSL values to RGB. h, s and l in [0,1]. Initially from grol.io/grol/extensions images.go.

func MergePlot added in v0.21.0

func MergePlot(img *image.NRGBA, x, y int, c color.NRGBA, newalpha float64)

Merge (additively) the pixel with alpha on top of the existing image.

Types

type AnsiPixels

type AnsiPixels struct {
	FdIn int

	Out           *bufio.Writer
	In            *os.File
	InWithTimeout *terminal.TimeoutReader

	Data []byte
	W, H int // Width and Height

	Mouse    bool // Mouse event received
	Mx, My   int  // Mouse last known position
	Mbuttons int  // Mouse buttons and modifier state
	C        chan os.Signal
	// Should image be monochrome, 256 or true color
	TrueColor bool
	Color     bool         // 256 (216) color mode
	Gray      bool         // grayscale mode
	Margin    int          // Margin around the image (image is smaller by 2*margin)
	FPS       float64      // (Target) Frames per second used for Reading with timeout
	OnResize  func() error // Callback when terminal is resized
	// contains filtered or unexported fields
}

func NewAnsiPixels

func NewAnsiPixels(fps float64) *AnsiPixels

func (*AnsiPixels) AltMod added in v0.25.0

func (ap *AnsiPixels) AltMod() bool

func (*AnsiPixels) AnyModifier added in v0.25.0

func (ap *AnsiPixels) AnyModifier() bool

func (*AnsiPixels) ChangeFPS added in v0.17.0

func (ap *AnsiPixels) ChangeFPS(fps float64)

func (*AnsiPixels) ClearEndOfLine

func (ap *AnsiPixels) ClearEndOfLine()

func (*AnsiPixels) ClearScreen

func (ap *AnsiPixels) ClearScreen()

func (*AnsiPixels) CtrlMod added in v0.25.0

func (ap *AnsiPixels) CtrlMod() bool

func (*AnsiPixels) DecodeImage added in v0.11.0

func (ap *AnsiPixels) DecodeImage(inp io.Reader) (*Image, error)

func (*AnsiPixels) Draw216ColorImage added in v0.12.0

func (ap *AnsiPixels) Draw216ColorImage(sx, sy int, img *image.RGBA) error

func (*AnsiPixels) DrawBox

func (ap *AnsiPixels) DrawBox(x, y, w, h int, topLeft, topRight, bottomLeft, bottomRight string)

func (*AnsiPixels) DrawMonoImage added in v0.12.0

func (ap *AnsiPixels) DrawMonoImage(sx, sy int, img *image.Gray, color string) error

func (*AnsiPixels) DrawRoundBox

func (ap *AnsiPixels) DrawRoundBox(x, y, w, h int)

func (*AnsiPixels) DrawSquareBox

func (ap *AnsiPixels) DrawSquareBox(x, y, w, h int)

func (*AnsiPixels) DrawTrueColorImage added in v0.12.0

func (ap *AnsiPixels) DrawTrueColorImage(sx, sy int, img *image.RGBA) error

func (*AnsiPixels) EndSyncMode added in v0.17.0

func (ap *AnsiPixels) EndSyncMode()

End sync (and flush).

func (*AnsiPixels) GetSize

func (ap *AnsiPixels) GetSize() (err error)

func (*AnsiPixels) HandleSignal added in v0.17.0

func (ap *AnsiPixels) HandleSignal(s os.Signal) error

func (*AnsiPixels) HideCursor

func (ap *AnsiPixels) HideCursor()

func (*AnsiPixels) IsResizeSignal

func (ap *AnsiPixels) IsResizeSignal(s os.Signal) bool

func (*AnsiPixels) LeftClick added in v0.24.0

func (ap *AnsiPixels) LeftClick() bool

func (*AnsiPixels) LeftDrag added in v0.24.0

func (ap *AnsiPixels) LeftDrag() bool

func (*AnsiPixels) Middle added in v0.26.1

func (ap *AnsiPixels) Middle() bool

func (*AnsiPixels) MiddleDrag added in v0.26.1

func (ap *AnsiPixels) MiddleDrag() bool

func (*AnsiPixels) MouseClickOff added in v0.21.0

func (ap *AnsiPixels) MouseClickOff()

func (*AnsiPixels) MouseClickOn added in v0.21.0

func (ap *AnsiPixels) MouseClickOn()

func (*AnsiPixels) MouseDecode added in v0.21.0

func (ap *AnsiPixels) MouseDecode()

func (*AnsiPixels) MousePixelsOff added in v0.21.0

func (ap *AnsiPixels) MousePixelsOff()

func (*AnsiPixels) MousePixelsOn added in v0.21.0

func (ap *AnsiPixels) MousePixelsOn()

func (*AnsiPixels) MouseTrackingOff added in v0.21.0

func (ap *AnsiPixels) MouseTrackingOff()

func (*AnsiPixels) MouseTrackingOn added in v0.21.0

func (ap *AnsiPixels) MouseTrackingOn()

func (*AnsiPixels) MouseWheelDown added in v0.26.0

func (ap *AnsiPixels) MouseWheelDown() bool

func (*AnsiPixels) MouseWheelUp added in v0.26.0

func (ap *AnsiPixels) MouseWheelUp() bool

func (*AnsiPixels) MouseX10Off added in v0.21.0

func (ap *AnsiPixels) MouseX10Off()

func (*AnsiPixels) MouseX10On added in v0.21.0

func (ap *AnsiPixels) MouseX10On()

func (*AnsiPixels) MoveCursor

func (ap *AnsiPixels) MoveCursor(x, y int)

func (*AnsiPixels) MoveHorizontally

func (ap *AnsiPixels) MoveHorizontally(x int)

func (*AnsiPixels) Open

func (ap *AnsiPixels) Open() (err error)

func (*AnsiPixels) ReadCursorPos

func (ap *AnsiPixels) ReadCursorPos() (int, int, error)

This also synchronizes the display and ends the syncmode.

func (*AnsiPixels) ReadImage added in v0.11.0

func (ap *AnsiPixels) ReadImage(path string) (*Image, error)

func (*AnsiPixels) ReadOrResizeOrSignal added in v0.17.0

func (ap *AnsiPixels) ReadOrResizeOrSignal() error

Read something or return terminal.ErrSignal if signal is received (normal exit requested case), will automatically call OnResize if set and if a resize signal is received and continue trying to read.

func (*AnsiPixels) ReadOrResizeOrSignalOnce added in v0.19.0

func (ap *AnsiPixels) ReadOrResizeOrSignalOnce() (int, error)

This will return either because of signal, or something read or the timeout (fps) passed. ap.Data is (re)set to the read data.

func (*AnsiPixels) Restore

func (ap *AnsiPixels) Restore()

func (*AnsiPixels) RightClick added in v0.24.0

func (ap *AnsiPixels) RightClick() bool

func (*AnsiPixels) RightDrag added in v0.24.0

func (ap *AnsiPixels) RightDrag() bool

func (*AnsiPixels) ScreenWidth added in v0.20.0

func (ap *AnsiPixels) ScreenWidth(str string) int

func (*AnsiPixels) ShiftMod added in v0.25.0

func (ap *AnsiPixels) ShiftMod() bool

func (*AnsiPixels) ShowCursor

func (ap *AnsiPixels) ShowCursor()

func (*AnsiPixels) ShowImage added in v0.11.0

func (ap *AnsiPixels) ShowImage(imagesRGBA *Image, zoom float64, offsetX, offsetY int, colorString string) error

Color string is the fallback mono color to use when AnsiPixels.TrueColor is false.

func (*AnsiPixels) StartSyncMode added in v0.17.0

func (ap *AnsiPixels) StartSyncMode()

func (*AnsiPixels) TruncateLeftToFit added in v0.21.0

func (ap *AnsiPixels) TruncateLeftToFit(msg string, maxWidth int) (string, int)

func (*AnsiPixels) WriteAt

func (ap *AnsiPixels) WriteAt(x, y int, msg string, args ...interface{})

func (*AnsiPixels) WriteAtStr

func (ap *AnsiPixels) WriteAtStr(x, y int, msg string)

func (*AnsiPixels) WriteBoxed

func (ap *AnsiPixels) WriteBoxed(y int, msg string, args ...interface{})

func (*AnsiPixels) WriteCentered

func (ap *AnsiPixels) WriteCentered(y int, msg string, args ...interface{})

func (*AnsiPixels) WriteRight added in v0.14.2

func (ap *AnsiPixels) WriteRight(y int, msg string, args ...interface{})

func (*AnsiPixels) WriteRightBoxed added in v0.20.0

func (ap *AnsiPixels) WriteRightBoxed(y int, msg string, args ...interface{})

func (*AnsiPixels) WriteRune added in v0.20.0

func (ap *AnsiPixels) WriteRune(r rune)

func (*AnsiPixels) WriteString added in v0.20.0

func (ap *AnsiPixels) WriteString(msg string)

type Image added in v0.11.0

type Image struct {
	Format string
	Width  int
	Height int
	Images []*image.RGBA
	Delays []int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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