terminal_go

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 1 Imported by: 0

README

terminal-go

A Go library for working with ANSI/VT terminal sequences.

Installation

go get github.com/ankddev/terminal-go

Usage

package main

import (
    "fmt"
    term "github.com/ankddev/terminal-go"
)

func main() {
    // Move cursor to position (5,10)
    fmt.Print(term.CursorPosition(5, 10))
    
    // Set text color to red
    fmt.Print(term.SetTextColor(1))
    fmt.Println("This text is red")
    
    // Reset all attributes
    fmt.Print(term.ResetAllAttributes)
    
    // Clear screen
    fmt.Print(term.EraseInDisplay)
    
    // Save cursor position
    fmt.Print(term.SaveCursorPosition)
    
    // Move cursor and restore position
    fmt.Print(term.CursorForward(10))
    fmt.Println("This text is red")
}

Features

  • Cursor movement and positioning
  • Text colors and attributes
  • Screen clearing and line manipulation
  • Scrolling and margins
  • Window manipulation
  • Character sets
  • And more...

Documentation

For detailed documentation of all available functions and constants, please see:

Building from Source

git clone https://github.com/ankddev/terminal-go.git
cd terminal-go
go build

Testing

To run tests:

go test -v

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

ANKDDEV

See Also

Support

If you have any questions or suggestions, please open an issue on GitHub.

Documentation

Index

Examples

Constants

View Source
const (
	// ReverseIndex performs the reverse operation of \n, moves cursor up one line, maintains horizontal position, scrolls buffer if necessary
	ReverseIndex = "\033M"
	// SaveCursorPointerInMemory saves the cursor position in memory
	SaveCursorPointerInMemory = "\0337"
	// RestoreCursorPointerFromMemory restores the cursor position from memory
	RestoreCursorPointerFromMemory = "\0338"

	// CursorBlinking enables cursor blinking
	CursorBlinking = "\033[?12h"
	// CursorBlinkingDisable disables cursor blinking
	CursorBlinkingDisable = "\033[?12l"
	// ShowCursor shows the cursor
	ShowCursor = "\033[?25h"
	// HideCursor hides the cursor
	HideCursor = "\033[?25l"

	// EnterAltScreen switches to the alternate screen buffer
	EnterAltScreen = "\033[?1049h"
	// ExitAltScreen switches back to the main screen buffer
	ExitAltScreen = "\033[?1049l"

	// EnableLineWrap enables line wrapping
	EnableLineWrap = "\033[?7h"
	// DisableLineWrap disables line wrapping
	DisableLineWrap = "\033[?7l"

	// EraseInDisplay clears the screen and moves cursor to home position
	EraseInDisplay = "\033[2J"
	// EraseInLine clears from cursor to the end of line
	EraseInLine = "\033[K"

	// ScrollUp scrolls display up one line
	ScrollUp = "\033[S"
	// ScrollDown scrolls display down one line
	ScrollDown = "\033[T"

	// SaveCursorPosition saves current cursor position
	SaveCursorPosition = "\033[s"
	// RestoreCursorPosition restores cursor to last saved position
	RestoreCursorPosition = "\033[u"

	// EnableVirtualTerminalProcessing enables VT processing
	EnableVirtualTerminalProcessing = "\033[?1h"

	// ResetAllAttributes resets all character attributes
	ResetAllAttributes = "\033[0m"

	// BoldBright sets bold/bright mode
	BoldBright = "\033[1m"
	// NormalIntensity sets normal intensity
	NormalIntensity = "\033[22m"

	// Underline enables underline
	Underline = "\033[4m"
	// UnderlineDisable disables underline
	UnderlineDisable = "\033[24m"

	// Negative sets negative image
	Negative = "\033[7m"
	// Positive sets positive image
	Positive = "\033[27m"

	// TabSet sets a tab stop at current position
	TabSet = "\033H"
	// TabClear clears tab stop at current position
	TabClear = "\033[0g"
	// TabClearAll clears all tab stops
	TabClearAll = "\033[3g"

	// DoubleHeightTop makes current line the top half of double height characters
	DoubleHeightTop = "\033#3"
	// DoubleHeightBottom makes current line the bottom half of double height characters
	DoubleHeightBottom = "\033#4"
	// SingleWidthLine makes current line single-width single-height
	SingleWidthLine = "\033#5"
	// DoubleWidthLine makes current line double-width single-height
	DoubleWidthLine = "\033#6"

	// DeviceStatusReport requests cursor position report
	DeviceStatusReport = "\033[6n"

	// ApplicationKeypad enables application keypad mode
	ApplicationKeypad = "\033="
	// NormalKeypad enables numeric keypad mode
	NormalKeypad = "\033>"

	// AutoWrap enables auto-wrap mode
	AutoWrap = "\033[?7h"
	// AutoWrapOff disables auto-wrap mode
	AutoWrapOff = "\033[?7l"

	// ClearAndResetScrollback clears screen and scrollback buffer
	ClearAndResetScrollback = "\033[3J"
)

Variables

This section is empty.

Functions

func CursorBackward

func CursorBackward(columns int) string

CursorBackward moves cursor backward by specified number of columns

Example

ExampleCursorBackward demonstrates moving cursor backward

fmt.Print("12345")
fmt.Print(CursorBackward(2)) // Moves cursor 2 positions left
fmt.Println("X")             // Overwrites character

func CursorDown

func CursorDown(lines int) string

CursorDown moves cursor down by specified number of lines

func CursorForward

func CursorForward(columns int) string

CursorForward moves cursor forward by specified number of columns

Example

ExampleCursorForward demonstrates moving cursor forward

fmt.Print(CursorForward(5)) // Moves cursor 5 positions right
fmt.Println("Text after 5 spaces")

func CursorHorizontalAbsolute

func CursorHorizontalAbsolute(column int) string

CursorHorizontalAbsolute moves cursor to specified column

func CursorNextLine

func CursorNextLine(lines int) string

CursorNextLine moves cursor to beginning of line n lines down

func CursorPosition

func CursorPosition(row, column int) string

CursorPosition sets the cursor position where subsequent text will begin

Example

ExampleCursorPosition demonstrates moving cursor to specific location

fmt.Print(CursorPosition(5, 10)) // Moves cursor to row 5, column 10
fmt.Println("Text at position 5,10")

func CursorPreviousLine

func CursorPreviousLine(lines int) string

CursorPreviousLine moves cursor to beginning of line n lines up

func CursorUp

func CursorUp(lines int) string

CursorUp moves cursor up by specified number of lines

Example

ExampleCursorUp demonstrates moving cursor up

fmt.Println("Line 1")
fmt.Println("Line 2")
fmt.Print(CursorUp(1))  // Moves cursor up one line
fmt.Println("Override") // Overwrites Line 2

func DeleteCharacters

func DeleteCharacters(n int) string

DeleteCharacters deletes n characters at cursor

func DeleteLines

func DeleteLines(n int) string

DeleteLines deletes n lines at cursor

Example

ExampleDeleteLines demonstrates deleting lines

fmt.Println("Line 1")
fmt.Println("Line 2")
fmt.Print(CursorUp(1))
fmt.Print(DeleteLines(1)) // Deletes current line

func DesignateCharacterSet

func DesignateCharacterSet(g int, charset byte) string

DesignateCharacterSet designates character set g can be 0-3 for G0-G3 charset can be B for US ASCII, 0 for DEC Special Graphics

func EraseInDisplayMode

func EraseInDisplayMode(n int) string

EraseInDisplayMode erases display with specified mode n=0: erase from cursor to end of display n=1: erase from start of display to cursor n=2: erase complete display n=3: erase scrollback buffer

Example

ExampleEraseInDisplayMode demonstrates clearing screen

fmt.Print(EraseInDisplayMode(2)) // Clears entire screen
fmt.Println("Fresh start")

func EraseInLineMode

func EraseInLineMode(n int) string

EraseInLineMode erases line with specified mode n=0: erase from cursor to end of line n=1: erase from start of line to cursor n=2: erase complete line

func InsertCharacters

func InsertCharacters(n int) string

InsertCharacters inserts n spaces at cursor

func InsertLines

func InsertLines(n int) string

InsertLines inserts n lines at cursor

func ReportCursorPosition

func ReportCursorPosition(row, col int) string

ReportCursorPosition formats cursor position report response

func RequestCursorPosition

func RequestCursorPosition() string

RequestCursorPosition requests cursor position and returns response sequence

Example

ExampleRequestCursorPosition demonstrates requesting cursor position

fmt.Print(RequestCursorPosition())
// Note: The actual response needs to be read from stdin

func RequestTerminalParameters

func RequestTerminalParameters() string

RequestTerminalParameters requests terminal parameters

func ResetMode

func ResetMode(mode int) string

ResetMode resets various terminal modes

func ScrollDownLines

func ScrollDownLines(lines int) string

ScrollDownLines scrolls screen down by n lines

func ScrollUpLines

func ScrollUpLines(lines int) string

ScrollUpLines scrolls screen up by n lines

Example

ExampleScrollUpLines demonstrates scrolling content up

fmt.Println("Line 1")
fmt.Println("Line 2")
fmt.Print(ScrollUpLines(1)) // Scrolls content up by one line

func SetBackgroundColor

func SetBackgroundColor(color int) string

SetBackgroundColor sets the background color

func SetCharacterSet

func SetCharacterSet(g int, charset byte) string

SetCharacterSet selects character set g can be 0 or 1 for default/alternate charset can be B for US ASCII, 0 for DEC Special Graphics

Example

ExampleSetCharacterSet demonstrates setting character set

fmt.Print(SetCharacterSet(0, 'B')) // Sets default character set to US ASCII
fmt.Println("ASCII text")

func SetConformanceLevel

func SetConformanceLevel(level int) string

SetConformanceLevel sets ANSI conformance level level can be 1, 2, or 3

func SetGraphicsRendition

func SetGraphicsRendition(params ...int) string

SetGraphicsRendition sets various text attributes Multiple parameters can be combined

Example

ExampleSetGraphicsRendition demonstrates setting multiple text attributes

fmt.Print(SetGraphicsRendition(1, 4)) // Sets bold and underline
fmt.Println("Bold and underlined text")
fmt.Print(ResetAllAttributes)

func SetMode

func SetMode(mode int) string

SetMode sets various terminal modes

Example

ExampleSetMode demonstrates setting terminal mode

fmt.Print(SetMode(4)) // Enables insert mode
fmt.Println("Text in insert mode")

func SetRGBBackgroundColor

func SetRGBBackgroundColor(r, g, b int) string

SetRGBBackgroundColor sets the background color using RGB values

func SetRGBTextColor

func SetRGBTextColor(r, g, b int) string

SetRGBTextColor sets the foreground color using RGB values

Example

ExampleSetRGBTextColor demonstrates setting RGB text color

fmt.Print(SetRGBTextColor(255, 0, 0)) // Sets text color to bright red
fmt.Println("Bright red text")
fmt.Print(ResetAllAttributes)

func SetScrollingRegion

func SetScrollingRegion(top, bottom int) string

SetScrollingRegion sets top and bottom margins

Example

ExampleSetScrollingRegion demonstrates setting scroll region

fmt.Print(SetScrollingRegion(1, 10)) // Sets scrolling region from line 1 to 10
fmt.Println("Scrolling region set")

func SetTextColor

func SetTextColor(color int) string

SetTextColor sets the foreground color

Example

ExampleSetTextColor demonstrates setting text color

fmt.Print(SetTextColor(1)) // Sets text color to red
fmt.Println("Red text")
fmt.Print(ResetAllAttributes)

func SoftTerminalReset

func SoftTerminalReset() string

SoftTerminalReset performs a soft terminal reset

Example

ExampleSoftTerminalReset demonstrates soft terminal reset

fmt.Print(SoftTerminalReset())
fmt.Println("Terminal reset")

func WindowManipulation

func WindowManipulation(ps int, args ...int) string

WindowManipulation performs window manipulation ps=1: de-iconify window ps=2: iconify window ps=3: move window to x,y ps=4: resize window to height,width in pixels ps=5: raise window to top of stack ps=6: lower window to bottom of stack ps=7: refresh window ps=8: resize window to rows,cols in characters ps=9: maximize/restore window

Example

ExampleWindowManipulation demonstrates window manipulation

fmt.Print(WindowManipulation(8, 24, 80)) // Resizes window to 24 rows and 80 columns
fmt.Println("Window resized")

Types

This section is empty.

Jump to

Keyboard shortcuts

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