ansie

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 8 Imported by: 0

README

ANSIe - Go library for ANSI colours in terminal

This library API is inspired by jansi library.

Usage

Get the library:

go get github.com/uaraven/ansie

You will need Go 1.23 or higher to use it.

ansie supports basic 7-colour, 256 colour and true colour modes. You can also use various attributes, such as underline, strike-out, etc.

Simple coloured output:

import . "github.com/uaraven/ansie"

errorMsg := Ansi.A("Error: ").Fg(Red).S("File not found: %s", fileName).Reset().A("Try a different name").String()

img.png

Underlined text:

import . "github.com/uaraven/ansie"

errorMsg := Ansi.A("This is ").Attr(Underline).A("important").String()

img.png

Custom ANSI attributes

import . "github.com/uaraven/ansie"

fmt.Printf(Ansi.A("This is ").Esc('m', ':', 4, 3).A("squiggly underlined").CR().String())

img.png

Disable colour if output is being redirected to file:

import . "github.com/uaraven/ansie"

a := NewAnsiFor(os.stdout)

errorMsg := a.A("Error: ").Fg(Red).S("File not found: %s", fileName).Reset().A("Try a different name").String()

Compatibility

Basic colours

ansie supports basic terminal colours: Black, Red, Green, Yellow, Blue, Magenta, Cyan and White. These colours should be compatible with all terminals of the last 30 years.

Use AnsiBuffer.Fg() and AnsiBuffer.Bg() to set these colours.

256 colours

Extended colours, like BrightBlue, Teal, Fuchsia or LightGoldenrod3 will use 256-colour ANSI sequences and should also work with any terminal of the last 30 years.

Use AnsiBuffer.Fg() and AnsiBuffer.Bg() to set these colours.

216 colours of the Xterm palette can be set using RGB values, where each colour component is limited to the values from 0 to 5 (inclusive), forming a 6x6x6 colour cube. Use following functions to set 6-value component colours: AnsiBuffer.FgRgb6(r,g,b uint) and AnsiBuffer.BgRgb6(r,g,b uint)

Shade of gray colours can be set for foreground and background using intensity values.

One can use integer values from 0 to 23 which directly map to grayscale palette indices from 232 to 255. Use methods AnsiBuffer.FgGray(uint) and AnsiBuffer.BgGray(uint) to set grayscale colour by integer index.

Alternatively one can use floating point intensity value in the range from 0 to 1, it will be automatically mapped to one of the closest 24 grayscale colours.

Use methods AnsiBuffer.FgGrayF(float64) and AnsiBuffer.BgGrayF(float64).

"True colour"

ansie supports full 24-bit colours using RGB values, which should be supported by all modern terminals, but it isn't. Standard MacOS terminal, for example, doesn't support 24 bit color Use following functions to set 24-bit colours: AnsiBuffer.FgRgb(r,g,b int), AnsiBuffer.FgRgbI(rgb int), AnsiBuffer.BgRgb(r,g,b int) and AnsiBuffer.BgRgbI(rgb int)

To use 24-bit colours on terminals that do not support 256-colour mode, you can convert 24-bit colour to 256-colour palette using the Rgb6x6x6 function. This function will map RGB values to the closest 256-colour index, including grayscale.

    color256 := ansie.Rgb6x6x6(255, 128, 64) // Converts RGB to 256-colour index
True colour compatibility mode

To use 24-bit colours on terminals that do not support 256-colour mode, you can enable compatibility mode.

Compatibility mode works by writing two sequences to the terminal: one for 256-colour mode and another for true colour. If terminal supports true colour, then true colour sequence will override 256-colour sequence, otherwise it will be ignored.

Colour compatibility mode is disabled by default, but you can enable it by setting ColorCompatibility field of AnsiBuffer to true.

import . "github.com/uaraven/ansie"
a := NewAnsiFor(os.Stdout)
a.ColorCompatibility = true // Enable compatibility mode
a.FgRgb(255, 128, 64).A("This text will be in true colour").Reset().CR().Print()

FgRgb(), FgRgbI(), BgRgb() and BgRgbI() methods support compatibility mode.

Colour names

ansie defines constants for the 256-colour palette with the names taken from here and matching Xterm names.

Note that some of the Xterm colour names are duplicated with the different palette index value. In the case of duplicates one random name was selected and added to ansie constants.

Terminal manipulation

ansie provides a basic terminal manipulation API, which allows you to read terminal size, move the cursor, clear the screen. You can use Screen struct to manipulate the terminal.

import . "github.com/uaraven/ansie"

screen, err := NewScreen() // Create a new screen for stdout
if err != nil {
    panic(err) // Handle error if screen creation fails
}
defer screen.Close() // Ensure the screen is closed when done
screen.Clear() // Clear the screen
screen.MoveCursorTo(10, 20) // Move cursor to line 10, column 20
fmt.Println(Ansi.FgRgb(255,255,255).A("Hello, world!").Reset().String()) // Print text at the new cursor position

When creating a new Screen, it will automatically switch to an alternative buffer, which allows you to manipulate the terminal without affecting the current output. You must use Screen.Close() to return to the main buffer and restore the terminal state.

Raw mode is not enabled by default, but you can enable it with Screen.SetRawMode(true).

Screen struct is in beta state and may change in the future. It is not recommended to use it in production code yet.

Terminal manipulation API is not supported on Windows.

License

ansie is distributed under the terms of MIT license.

Copy of the license text is available in the license.txt file.

Documentation

Index

Constants

This section is empty.

Variables

Ansi is a default instance of AnsiBuffer

Functions

This section is empty.

Types

type AnsiBuffer

type AnsiBuffer struct {

	// ColorCompatibility allows usage of 24-bit colours on terminals that support only 256-colour mode when enabled.
	ColorCompatibility bool
	// contains filtered or unexported fields
}

func NewAnsi

func NewAnsi() *AnsiBuffer

NewAnsi creates a new AnsiBuffer. It doesn't assume anything about the device that the output will be directed to.

func NewAnsiFor

func NewAnsiFor(f *os.File) *AnsiBuffer

NewAnsiFor creates a new AnsiBuffer for a given device. It will not automatically print to this device, but it will disable ANSI colours if the device doesn't seem to support them, like when redirecting standard output into a file or piping it to another program

func (*AnsiBuffer) A

func (ap *AnsiBuffer) A(text string) *AnsiBuffer

A adds text to the AnsiBuffer's buffer. The text will be output with the current colours and attributes

func (*AnsiBuffer) Attr

func (ap *AnsiBuffer) Attr(attr Attribute) *AnsiBuffer

Attr sets font attribute

func (*AnsiBuffer) Bg

func (ap *AnsiBuffer) Bg(colour Colour) *AnsiBuffer

Bg sets background colour. If colour is one of the standard 8 colours, it will use a basic ANSI sequence. If colour is larger than 8, it will be treated as a 256-colour code and the corresponding ANSI sequence will be used. To use 24-bit colour on supported terminals, use BgRgb or BgRgbI methods instead. To use 24-bit colour with 256-colour terminals, use BgRgb6 method or Rgb6x6x6 function to convert RGB values to 256-colour code.

func (*AnsiBuffer) BgGray

func (ap *AnsiBuffer) BgGray(intensity uint) *AnsiBuffer

BgGray sets background colour that is the shade of grey. intensity is a value in a range [0..24]. It is converted to one of standard 24 grey shades in the 256-colour palette

func (*AnsiBuffer) BgGrayF

func (ap *AnsiBuffer) BgGrayF(intensity float64) *AnsiBuffer

BgGrayF sets background colour that is the shade of grey. intensity is a floating point value in a range [0..1]. It is converted to one of standard 24 grey shades in the 256-colour palette

func (*AnsiBuffer) BgHi

func (ap *AnsiBuffer) BgHi(colour Colour) *AnsiBuffer

BgHi sets background colour to the high intensity version of one of standard 8 colours

If used with one of 256 colour codes, it will just set the colour, without modifying the intensity

func (*AnsiBuffer) BgRgb

func (ap *AnsiBuffer) BgRgb(r, g, b uint) *AnsiBuffer

BgRgb sets foreground colour using "true colour" RGB colour

func (*AnsiBuffer) BgRgb6

func (ap *AnsiBuffer) BgRgb6(r, g, b uint) *AnsiBuffer

BgRgb6 sets background RGB colour converted to 9-bit colour (3;3;3) as supported by 256-colour ANSI sequence

func (*AnsiBuffer) BgRgbI

func (ap *AnsiBuffer) BgRgbI(i uint) *AnsiBuffer

BgRgbI sets background colour using "true colour" RGB colour represented as a single integer

func (*AnsiBuffer) CR

func (ap *AnsiBuffer) CR() *AnsiBuffer

CR adds carriage return character (ASCII 13) to the AnsiBuffer's buffer

func (*AnsiBuffer) Clear

func (ap *AnsiBuffer) Clear() *AnsiBuffer

Clear resets the internal buffer, after calling it the AnsiBuffer is in a clean state, as if just created

func (*AnsiBuffer) CursorDown added in v0.3.3

func (ap *AnsiBuffer) CursorDown(count int) *AnsiBuffer

func (*AnsiBuffer) CursorLeft added in v0.3.3

func (ap *AnsiBuffer) CursorLeft(count int) *AnsiBuffer

func (*AnsiBuffer) CursorRight added in v0.3.3

func (ap *AnsiBuffer) CursorRight(count int) *AnsiBuffer

func (*AnsiBuffer) CursorUp added in v0.3.3

func (ap *AnsiBuffer) CursorUp(count int) *AnsiBuffer

func (*AnsiBuffer) Esc

func (ap *AnsiBuffer) Esc(command rune, sep rune, codes ...int) *AnsiBuffer

Esc allows adding a custom Esc sequence to the buffer The sequence that will be added is:

ESC[codes sep codes sep codes sep command

i.e. Esc('m', ':', 4, 3) will create sequence 'ESC[4:3m' which will create curly underline in iTerm2

func (*AnsiBuffer) EscM

func (ap *AnsiBuffer) EscM(codes ...int) *AnsiBuffer

EscM allows adding custom SGR sequences to the output EscM(38, 2, 0, 0, 255) will add sequence 'ESC[38;2;0;0;255m' to enable bright blue RGB colour

func (*AnsiBuffer) Fg

func (ap *AnsiBuffer) Fg(colour Colour) *AnsiBuffer

Fg sets foreground colour. If colour is one of the standard 8 colours, it will use a basic ANSI sequence. If colour is larger than 8, it will be treated as a 256-colour code and the corresponding ANSI sequence will be used. To use 24-bit colour on supported terminals, use FgRgb or FgRgbI methods instead. To use 24-bit colour with 256-colour terminals, use FgRgb6 method or Rgb6x6x6 function to convert RGB values to 256-colour code.

func (*AnsiBuffer) FgGray

func (ap *AnsiBuffer) FgGray(intensity uint) *AnsiBuffer

FgGray sets foreground colour that is the shade of grey. intensity is a value in a range [0..23]. It is converted to one of standard 24 grey shades in the 256-colour palette

func (*AnsiBuffer) FgGrayF

func (ap *AnsiBuffer) FgGrayF(intensity float64) *AnsiBuffer

FgGrayF sets foreground colour that is the shade of grey. intensity is a floating point value in a range [0..1]. It is converted to one of standard 24 grey shades in the 256-colour palette

func (*AnsiBuffer) FgHi

func (ap *AnsiBuffer) FgHi(colour Colour) *AnsiBuffer

FgHi sets foreground colour to the high intensity version of one of standard 8 colours

If used with one of 256 colour codes, it will just set the colour, without modifying the intensity

func (*AnsiBuffer) FgRgb

func (ap *AnsiBuffer) FgRgb(r, g, b uint) *AnsiBuffer

FgRgb sets foreground colour using "true colour" RGB colour

func (*AnsiBuffer) FgRgb6

func (ap *AnsiBuffer) FgRgb6(r, g, b uint) *AnsiBuffer

FgRgb6 sets foreground RGB colour as supported by 256-colour ANSI sequence In this mode each colour component is represented by a value from 0 to 5. Values beyond range of [0..5] are clipped R, G and B values are combined to get one of 216 colours supported by terminal

func (*AnsiBuffer) FgRgbI

func (ap *AnsiBuffer) FgRgbI(i uint) *AnsiBuffer

FgRgbI sets foreground colour using "true colour" RGB colour represented as a single integer

func (*AnsiBuffer) GetBuffer

func (ap *AnsiBuffer) GetBuffer() string

GetBuffer retrieves internal buffer as a string without clearing it

func (*AnsiBuffer) IsEnabled

func (ap *AnsiBuffer) IsEnabled() bool

IsEnabled returns true if colour output is enabled

func (*AnsiBuffer) Reset

func (ap *AnsiBuffer) Reset() *AnsiBuffer

Reset resets all the colours and attributes to defaults

func (*AnsiBuffer) S

func (ap *AnsiBuffer) S(format string, params ...interface{}) *AnsiBuffer

S adds formatted (similar to fmt.Sprintf) text to the AnsiBuffer's buffer. The text will be output with the current colours and attributes

func (*AnsiBuffer) SetEnabled

func (ap *AnsiBuffer) SetEnabled(value bool)

SetEnabled enables or disables the colour output. This does not affect the string already in AnsiBuffer's buffer

func (*AnsiBuffer) String

func (ap *AnsiBuffer) String() string

String converts the internal buffer to a string. The buffer is cleared after the call

type Attribute

type Attribute = int
const (
	Reset Attribute = iota
	Bold
	Faint
	Italic
	Underline
	SlowBlink
	RapidBlink
	Reverse
	Conceal
	CrossOut

	NoBold Attribute = iota + 11
	Normal
	NoItalic
	NoUnderline
	NoBlink

	NoReverse
	NoConceal
	NoCrossOut
)

type Colour

type Colour = int
const (
	Black             Colour = 0
	Red               Colour = 1
	Green             Colour = 2
	Yellow            Colour = 3
	Blue              Colour = 4
	Magenta           Colour = 5
	Cyan              Colour = 6
	White             Colour = 7
	Maroon            Colour = 1
	Olive             Colour = 3
	Navy              Colour = 4
	Purple            Colour = 5
	Teal              Colour = 6
	Silver            Colour = 7
	Grey              Colour = 8
	BrightRed         Colour = 9
	BrightGreen       Colour = 10
	Lime              Colour = 10
	BrightYellow      Colour = 11
	BrightBlue        Colour = 12
	Fuchsia           Colour = 13
	BrightMagenta     Colour = 13
	Aqua              Colour = 14
	BrightCyan        Colour = 14
	BrightWhite       Colour = 15
	Grey0             Colour = 16
	NavyBlue          Colour = 17
	DarkBlue          Colour = 18
	Blue3             Colour = 20
	Blue1             Colour = 21
	DarkGreen         Colour = 22
	DeepSkyBlue4      Colour = 25
	DodgerBlue3       Colour = 26
	DodgerBlue2       Colour = 27
	Green4            Colour = 28
	SpringGreen4      Colour = 29
	Turquoise4        Colour = 30
	DeepSkyBlue3      Colour = 32
	DodgerBlue1       Colour = 33
	Green3            Colour = 34
	SpringGreen3      Colour = 35
	DarkCyan          Colour = 36
	LightSeaGreen     Colour = 37
	DeepSkyBlue2      Colour = 38
	DeepSkyBlue1      Colour = 39
	SpringGreen2      Colour = 42
	Cyan3             Colour = 43
	DarkTurquoise     Colour = 44
	Turquoise2        Colour = 45
	Green1            Colour = 46
	SpringGreen1      Colour = 48
	MediumSpringGreen Colour = 49
	Cyan2             Colour = 50
	Cyan1             Colour = 51
	Purple4           Colour = 55
	Purple3           Colour = 56
	BlueViolet        Colour = 57
	Grey37            Colour = 59
	MediumPurple4     Colour = 60
	SlateBlue3        Colour = 62
	RoyalBlue1        Colour = 63
	Chartreuse4       Colour = 64
	PaleTurquoise4    Colour = 66
	SteelBlue         Colour = 67
	SteelBlue3        Colour = 68
	CornflowerBlue    Colour = 69
	DarkSeaGreen4     Colour = 71
	CadetBlue         Colour = 72
	SkyBlue3          Colour = 74
	Chartreuse3       Colour = 76
	PaleGreen3        Colour = 77
	SeaGreen3         Colour = 78
	Aquamarine3       Colour = 79
	MediumTurquoise   Colour = 80
	SteelBlue1        Colour = 81
	SeaGreen2         Colour = 83
	SeaGreen1         Colour = 85
	DarkSlateGray2    Colour = 87
	DarkRed           Colour = 88
	DarkMagenta       Colour = 91
	Orange4           Colour = 94
	LightPink4        Colour = 95
	Plum4             Colour = 96
	MediumPurple3     Colour = 98
	SlateBlue1        Colour = 99
	Wheat4            Colour = 101
	Grey53            Colour = 102
	LightSlateGrey    Colour = 103
	MediumPurple      Colour = 104
	LightSlateBlue    Colour = 105
	Yellow4           Colour = 106
	DarkSeaGreen      Colour = 108
	LightSkyBlue3     Colour = 110
	SkyBlue2          Colour = 111
	Chartreuse2       Colour = 112
	DarkSlateGray3    Colour = 116
	SkyBlue1          Colour = 117
	Chartreuse1       Colour = 118
	LightGreen        Colour = 120
	Aquamarine1       Colour = 122
	DarkSlateGray1    Colour = 123
	DeepPink4         Colour = 125
	MediumVioletRed   Colour = 126
	DarkViolet        Colour = 128
	MediumOrchid3     Colour = 133
	MediumOrchid      Colour = 134
	DarkGoldenrod     Colour = 136
	RosyBrown         Colour = 138
	Grey63            Colour = 139
	MediumPurple2     Colour = 140
	MediumPurple1     Colour = 141
	DarkKhaki         Colour = 143
	NavajoWhite3      Colour = 144
	Grey69            Colour = 145
	LightSteelBlue3   Colour = 146
	LightSteelBlue    Colour = 147
	DarkOliveGreen3   Colour = 149
	DarkSeaGreen3     Colour = 150
	LightCyan3        Colour = 152
	LightSkyBlue1     Colour = 153
	GreenYellow       Colour = 154
	DarkOliveGreen2   Colour = 155
	PaleGreen1        Colour = 156
	DarkSeaGreen1     Colour = 158
	PaleTurquoise1    Colour = 159
	Red3              Colour = 160
	DeepPink3         Colour = 162
	Magenta3          Colour = 164
	DarkOrange3       Colour = 166
	IndianRed         Colour = 167
	HotPink3          Colour = 168
	HotPink2          Colour = 169
	Orchid            Colour = 170
	Orange3           Colour = 172
	LightSalmon3      Colour = 173
	LightPink3        Colour = 174
	Pink3             Colour = 175
	Plum3             Colour = 176
	Violet            Colour = 177
	Gold3             Colour = 178
	LightGoldenrod3   Colour = 179
	Tan               Colour = 180
	MistyRose3        Colour = 181
	Thistle3          Colour = 182
	Plum2             Colour = 183
	Yellow3           Colour = 184
	Khaki3            Colour = 185
	LightYellow3      Colour = 187
	Grey84            Colour = 188
	LightSteelBlue1   Colour = 189
	Yellow2           Colour = 190
	DarkOliveGreen1   Colour = 192
	Honeydew2         Colour = 194
	LightCyan1        Colour = 195
	Red1              Colour = 196
	DeepPink2         Colour = 197
	DeepPink1         Colour = 199
	Magenta2          Colour = 200
	Magenta1          Colour = 201
	OrangeRed1        Colour = 202
	IndianRed1        Colour = 204
	HotPink           Colour = 206
	MediumOrchid1     Colour = 207
	DarkOrange        Colour = 208
	Salmon1           Colour = 209
	LightCoral        Colour = 210
	PaleVioletRed1    Colour = 211
	Orchid2           Colour = 212
	Orchid1           Colour = 213
	Orange1           Colour = 214
	SandyBrown        Colour = 215
	LightSalmon1      Colour = 216
	LightPink1        Colour = 217
	Pink1             Colour = 218
	Plum1             Colour = 219
	Gold1             Colour = 220
	LightGoldenrod2   Colour = 222
	NavajoWhite1      Colour = 223
	MistyRose1        Colour = 224
	Thistle1          Colour = 225
	Yellow1           Colour = 226
	LightGoldenrod1   Colour = 227
	Khaki1            Colour = 228
	Wheat1            Colour = 229
	Cornsilk1         Colour = 230
	Grey100           Colour = 231
	Grey3             Colour = 232
	Grey7             Colour = 233
	Grey11            Colour = 234
	Grey15            Colour = 235
	Grey19            Colour = 236
	Grey23            Colour = 237
	Grey27            Colour = 238
	Grey30            Colour = 239
	Grey35            Colour = 240
	Grey39            Colour = 241
	Grey42            Colour = 242
	Grey46            Colour = 243
	Grey50            Colour = 244
	Grey54            Colour = 245
	Grey58            Colour = 246
	Grey62            Colour = 247
	Grey66            Colour = 248
	Grey70            Colour = 249
	Grey74            Colour = 250
	Grey78            Colour = 251
	Grey82            Colour = 252
	Grey85            Colour = 253
	Grey89            Colour = 254
	Grey93            Colour = 255
)

func Rgb6x6x6 added in v0.4.0

func Rgb6x6x6(r uint, g uint, b uint) Colour

Rgb6x6x6 creates a colour in 256-colour palette from 24-bit RGB colour represented as 3 values

func RgbTo216Colours added in v0.3.2

func RgbTo216Colours(r uint, g uint, b uint) Colour

RgbTo216Colours converts a colour represented as R, G, B values of 0 to 5 to one of 216 colours in 256-colour palette

type FileTerminal added in v0.4.0

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

FileTerminal implements the Terminal interface backed by an os.File.

func NewTerminalFromFile added in v0.4.0

func NewTerminalFromFile(file *os.File) (*FileTerminal, error)

func (*FileTerminal) Fd added in v0.4.0

func (t *FileTerminal) Fd() int

func (*FileTerminal) GetSize added in v0.4.0

func (t *FileTerminal) GetSize() (*unix.Winsize, error)

GetSize implements Terminal.

func (*FileTerminal) GetState added in v0.4.0

func (t *FileTerminal) GetState() (*unix.Termios, error)

func (*FileTerminal) IsTerminal added in v0.4.0

func (t *FileTerminal) IsTerminal() bool

func (*FileTerminal) SetState added in v0.4.0

func (t *FileTerminal) SetState(termState *unix.Termios) error

func (*FileTerminal) Write added in v0.4.0

func (t *FileTerminal) Write(s string) (n int, err error)

type MockTerminal added in v0.4.0

type MockTerminal struct {
	FileDesc      int
	Width         int
	Height        int
	CursorX       int
	CursorY       int
	CursorVisible bool
	State         unix.Termios
	Buffer        strings.Builder
}

func NewMockTerminal added in v0.4.0

func NewMockTerminal(width, height int) *MockTerminal

func (*MockTerminal) Fd added in v0.4.0

func (m *MockTerminal) Fd() int

Fd implements Terminal.

func (*MockTerminal) GetSize added in v0.4.0

func (m *MockTerminal) GetSize() (*unix.Winsize, error)

GetSize implements Terminal.

func (*MockTerminal) GetState added in v0.4.0

func (m *MockTerminal) GetState() (*unix.Termios, error)

GetState implements Terminal.

func (*MockTerminal) IsTerminal added in v0.4.0

func (m *MockTerminal) IsTerminal() bool

IsTerminal implements Terminal.

func (*MockTerminal) ResetBuffer added in v0.4.0

func (m *MockTerminal) ResetBuffer()

func (*MockTerminal) SetSize added in v0.4.0

func (m *MockTerminal) SetSize(w, h int, c chan os.Signal)

func (*MockTerminal) SetState added in v0.4.0

func (m *MockTerminal) SetState(termState *unix.Termios) error

SetState implements Terminal.

func (*MockTerminal) Write added in v0.4.0

func (m *MockTerminal) Write(s string) (n int, err error)

Write implements Terminal.

type Screen added in v0.4.0

type Screen struct {

	// Width represents the width of the terminal in characters. It is updated on resize.
	Width int
	// Height represents the height of the terminal in characters. It is updated on resize.
	Height int
	// CursorVisible indicates whether the cursor is currently visible.
	CursorVisible bool
	// contains filtered or unexported fields
}

Screen represents a terminal screen with methods to manipulate the terminal display.

func NewScreen added in v0.4.0

func NewScreen() (*Screen, error)

NewScreen initializes a new Screen using the standard output file descriptor,

func NewScreenFromFile added in v0.4.0

func NewScreenFromFile(f *os.File) (*Screen, error)

NewScreenFromFile initializes a new Screen using file descriptor of f parameter, switches the terminal into alternate buffer mode, and retrieves the terminal size.

func NewScreenFromTerminal added in v0.4.0

func NewScreenFromTerminal(term Terminal) (*Screen, error)

NewScreenFromTerminal initializes a new Screen using the provided Terminal interface,

func (*Screen) Clear added in v0.4.0

func (s *Screen) Clear()

Clear clears the terminal screen and moves the cursor to the home position.

func (*Screen) Close added in v0.4.0

func (s *Screen) Close()

Close closes the screen, restores the terminal state, and exits alternate buffer mode.

func (*Screen) MoveCursorTo added in v0.4.0

func (s *Screen) MoveCursorTo(x, y int)

MoveCursorTo moves the cursor to the specified (x, y) position in the terminal. Coordinates are 1-based, where (1, 1) is the top-left corner

func (*Screen) SetCursorVisible added in v0.4.0

func (s *Screen) SetCursorVisible(visible bool)

SetCursorVisible shows or hides the cursor in the terminal.

func (*Screen) SetRawMode added in v0.4.0

func (s *Screen) SetRawMode(rawMode bool) error

SetRawMode sets the terminal to raw mode or restores it to normal mode. In raw mode, input is not processed (no echo, no line buffering). This is useful for applications that need to handle input directly, like text editors or games.

type ScreenError added in v0.4.0

type ScreenError struct {
	Message string
	Cause   error
}

func NewScreenError added in v0.4.0

func NewScreenError(message string, cause error) ScreenError

func (ScreenError) Error added in v0.4.0

func (err ScreenError) Error() string

type Terminal added in v0.4.0

type Terminal interface {
	// Fd returns the file descriptor of the terminal.
	Fd() int
	// Write writes data to the terminal.
	Write(s string) (n int, err error)
	// IsTerminal checks if the file descriptor is a terminal.
	IsTerminal() bool
	// GetState retrieves the current terminal state.
	GetState() (*unix.Termios, error)
	// SetState sets the terminal state to the provided termios structure.
	SetState(termState *unix.Termios) error
	// GetSize retrieves the size of the terminal.
	GetSize() (*unix.Winsize, error) // Returns width and height of the terminal
}

Terminal interface defines abstract terminal operations.

Jump to

Keyboard shortcuts

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