GoVTE is a Go implementation of a VTE (Virtual Terminal Emulator) parser, providing robust ANSI escape sequence parsing and complete terminal emulation capabilities.
- π ANSI Escape Sequence Parsing - Complete VT100/xterm compatibility
- π¨ Full Color Support - Named colors, RGB, and 256-color palette
- πΊ Terminal Emulation - Complete terminal buffer with cursor management
- π Unicode Support - Full UTF-8 character handling
- π₯οΈ TUI Program Capture - Capture and render real TUI applications
- β‘ High Performance - Optimized state machine implementation
# Install the latest version
go get github.com/cliofy/govte@latest
# Or install a specific version
go get github.com/cliofy/govte@v0.2.0- Go 1.21 or higher
- No external runtime dependencies
package main
import (
"fmt"
"github.com/cliofy/govte"
"github.com/cliofy/govte/terminal"
)
func main() {
parser := govte.NewParser()
terminalBuffer := terminal.NewTerminalBuffer(80, 24)
// Parse ANSI colored text
input := []byte("Hello \x1b[31mRed\x1b[0m World!")
for _, b := range input {
parser.Advance(terminalBuffer, []byte{b})
}
fmt.Println(terminalBuffer.GetDisplayWithColors())
// Or use convenience functions
output := terminal.ParseBytesWithColors([]byte("\x1b[32mGreen\x1b[0m"), 80, 24)
fmt.Println(output)
}Capture and render real TUI applications:
import (
"fmt"
"os/exec"
"time"
"github.com/cliofy/govte"
"github.com/cliofy/govte/terminal"
"github.com/creack/pty"
)
func main() {
// Start a TUI program in a PTY
cmd := exec.Command("htop")
ptmx, err := pty.Start(cmd)
if err != nil {
panic(err)
}
time.Sleep(500 * time.Millisecond)
// Capture output
var output []byte
buffer := make([]byte, 4096)
n, _ := ptmx.Read(buffer)
output = append(output, buffer[:n]...)
// Parse and render
parser := govte.NewParser()
terminal := terminal.NewTerminalBuffer(80, 24)
for _, b := range output {
parser.Advance(terminal, []byte{b})
}
fmt.Println(terminal.GetDisplayWithColors())
}The Parser implements a state machine for processing ANSI escape sequences:
parser := govte.NewParser()
// Process bytes through the parser
parser.Advance(performer, inputBytes)The Performer interface handles parsed actions. Implement it for custom behavior:
type MyPerformer struct {
govte.NoopPerformer // Embed for default implementations
}
func (p *MyPerformer) Print(c rune) {
fmt.Printf("Character: %c\n", c)
}
func (p *MyPerformer) CsiDispatch(params *govte.Params, intermediates []byte, ignore bool, action rune) {
fmt.Printf("CSI sequence: %c with params %v\n", action, params)
}The TerminalBuffer provides complete terminal emulation:
terminal := terminal.NewTerminalBuffer(width, height)
// Get plain text output
text := terminal.GetDisplay()
// Get output with ANSI color codes preserved
colored := terminal.GetDisplayWithColors()
// Access cursor position
x, y := terminal.CursorPosition()type LoggingPerformer struct {
govte.NoopPerformer
}
func (l *LoggingPerformer) Execute(b byte) {
switch b {
case 0x0A: // Line Feed
fmt.Println("[LF] New line")
case 0x0D: // Carriage Return
fmt.Println("[CR] Carriage return")
}
}
func (l *LoggingPerformer) CsiDispatch(params *govte.Params, intermediates []byte, ignore bool, action rune) {
fmt.Printf("[CSI] Action: %c, Params: %v\n", action, params)
}// Supports named colors (\x1b[31m), RGB (\x1b[38;2;255;0;0m), and 256-color (\x1b[38;5;196m)
input := "\x1b[38;2;255;0;0mRGB Red\x1b[0m \x1b[38;5;21mBlue\x1b[0m"
output := terminal.ParseBytesWithColors([]byte(input), 80, 24)The repository includes several example programs:
examples/parselog/- Debug tool that logs all parsed ANSI actionsexamples/capture_tui/- Complete TUI program capture and renderingexamples/animated_progress/- Animated progress bar demonstrationexamples/vte_animation/- VTE animation examples
cd examples/parselog && go run main.go
cd examples/capture_tui && go build && ./capture_tui- β CSI (Control Sequence Introducer) sequences
- β OSC (Operating System Command) sequences
- β SGR (Select Graphic Rendition) parameters
- β Cursor movement and positioning
- β Text styling (bold, italic, underline, etc.)
- β Color support (3/4-bit, 8-bit, 24-bit)
- β Character set handling
- β UTF-8 unicode support
- β Terminal title and icon sequences
Contributions are welcome! Please ensure:
- Code follows Go conventions and is well-documented
- All tests pass:
go test ./... - Add tests for new functionality
- Performance-critical code includes benchmarks
| Avatar | Name | GitHub | Social Media |
|---|---|---|---|
![]() |
Karl | github.com/youzhonghui | x.com/gofindkarl |
MIT License - see LICENSE file for details.
This project is inspired by:
- alacritty/vte - A state machine based VTE parser in Rust, which provides the foundation for terminal emulation parsing
- zellij-org/zellij - A terminal workspace with innovative approaches to terminal multiplexing and rendering
Built by cliofy.ai
