Skip to content

About

Headless, embeddable ANSI terminal emulator in Go with a Bubble Tea-compatible rendering layer for building TUIs

Topics

Resources

Stars

31 stars

Watchers

2 watching

Forks

Latest commit

Β 

History

58 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Bubbleterm: A Headless Terminal Emulator in Go

A fully-functional, headless, embeddable terminal emulator written in Golang. This library focuses on terminal emulation - parsing ANSI escape sequences, maintaining screen state, and rendering frames. It's designed to work with PTY libraries like creack/pty for complete terminal functionality. Finally, we provide a Bubbletea-compatible output format for building terminal user interfaces (TUIs).

πŸ’‘ Goals

This library provides the terminal emulation layer and Bubble components that sits between PTY I/O and your application. It can:

  • Parse and interpret ANSI escape sequences (CSI, OSC, ESC, DCS)
  • Maintain terminal screen state (cursor position, colors, attributes)
  • Handle 256-color and true color (24-bit RGB) rendering
  • Support alternate screen buffers and scrollback
  • Process keyboard and mouse input events
  • Render frames as ANSI-preserved strings for TUI frameworks
  • Emulate $TERM = xterm-256color behavior accurately

πŸ“¦ Features

Feature Status
ANSI parser (CSI, OSC) βœ… Core complete
UTF-8 support βœ…
Text attributes (bold, underline, etc) βœ…
256-color + true color βœ…
Cursor & scrollback βœ…
Keyboard input support βœ…
Resize support βœ…
$TERM compatibility βœ… xterm-256color
Damage tracking βœ… Line-level
Cursor position βœ…
Bubbletea-compatible output βœ…
Adjustable frame rate βœ…
Process termination API βœ…

πŸš€ Getting Started

go get github.com/taigrr/bubbleterm

πŸ“‹ Usage Examples

This library provides three ways to use the terminal emulator:

1. Bubbletea Integration (cmd/bubbleterm)

Run a terminal application within a Bubbletea TUI:

go run cmd/bubbleterm/main.go

This example shows how to:

  • Create a terminal bubble that runs htop
  • Handle keyboard input (Ctrl+C/q to quit)
  • Forward all messages to the terminal bubble
  • Display the terminal output in a TUI
// import bubbleterm "github.com/taigrr/bubbleterm"
// Create a new terminal bubble and start htop
cmd := exec.Command("htop")
terminal, err := bubbleterm.NewWithCommand(80, 24, cmd)

// Use in your Bubbletea model
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    var cmd tea.Cmd
    terminalModel, cmd := m.terminal.Update(msg)
    m.terminal = terminalModel.(*bubbleterm.Model)
    return m, cmd
}

2. Headless Emulator (cmd/staticprint)

Use the terminal emulator without a TUI for programmatic access:

go run cmd/staticprint/main.go

This example demonstrates:

  • Creating a headless terminal emulator
  • Starting a command (htop)
  • Capturing terminal output as frames
  • Resizing the terminal dynamically
// Create a new emulator
emu, err := emulator.New(80, 24)
defer emu.Close()

// Start a command
cmd := exec.Command("htop")
err = emu.StartCommand(cmd)

// Get the screen output
frame := emu.GetScreen()
for i, row := range frame.Rows {
    fmt.Printf("%2d: %s\n", i, row)
}

// Resize the terminal
emu.Resize(100, 40)

3. Multi-Window Terminal Manager (cmd/multiwindow)

A complete windowing system with multiple terminal instances:

multiWindow.mp4
go run cmd/multiwindow/main.go

Features:

  • Right-click: Create new terminal window
  • Left-click: Select and drag windows
  • 'i': Enter insert mode (input goes to focused terminal)
  • ESC: Exit insert mode
  • +/-: Resize focused window
  • Ctrl+C/q: Quit application

This example shows advanced usage:

  • Multiple terminal instances running simultaneously
  • Window management with focus and z-ordering
  • Mouse event translation between screen and window coordinates
  • Centralized terminal updates with proper cleanup

πŸ”§ Core API

Basic Terminal Emulator

// Create emulator
emu, err := emulator.New(width, height)

// Start a command
cmd := exec.Command("your-command")
emu.StartCommand(cmd)

// Get rendered output
frame := emu.GetScreen()
for _, row := range frame.Rows {
    fmt.Println(row)
}

// Resize
emu.Resize(newWidth, newHeight)

// Cleanup
emu.Close()

Bubbletea Integration

// Create terminal bubble
terminal, err := bubbleterm.NewWithCommand(width, height, cmd)
terminal.SetAutoPoll(false) // Disable auto-polling for updates

// In your Bubbletea model
func (m *model) Init() tea.Cmd {
    return m.terminal.Init()
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    terminalModel, cmd := m.terminal.Update(msg)
    m.terminal = terminalModel.(*bubbleterm.Model)
    return m, cmd
}

func (m *model) View() string {
    return m.terminal.View()
}

Advanced Features

// Focus management
terminal.Focus()
terminal.Blur()
focused := terminal.Focused()

// Manual input sending
terminal.SendInput("ls\n")

// Process monitoring
if terminal.GetEmulator().IsProcessExited() {
    // Handle process exit
}

// Auto-polling control (for custom update loops)
terminal.SetAutoPoll(false)
cmd := terminal.UpdateTerminal() // Manual poll

Limitations and Known Issues

  • We may decide to use a different emulator library in the future if it provides better performance or features

πŸ“š Resources

πŸ“œ License

0BSD

βš™οΈ Roadmap

This library focuses on terminal emulation. For complete terminal functionality:

  • PTY Management: Use creack/pty for process and PTY handling
  • Terminal Emulation: This library handles ANSI parsing and screen rendering
  • TUI Integration: Output works seamlessly with Bubbletea and other TUI frameworks

Contributions welcome!

About

Headless, embeddable ANSI terminal emulator in Go with a Bubble Tea-compatible rendering layer for building TUIs

Topics

Resources

Stars

31 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages