Convert Figma logo to code with AI

nsf logotermbox-go

Pure Go termbox implementation

4,667
373
4,667
50

Top Related Projects

4,521

Tcell is an alternate terminal package, similar in some ways to termbox, but better in others.

10,669

Terminal UI library with rich, interactive widgets — written in Golang

9,827

Minimalist Go package aimed at creating Console User Interfaces.

13,132

Golang terminal dashboard

2,091

A UI library for terminal applications.

Quick Overview

Termbox-go is a pure Go library for creating cross-platform text-based user interfaces. It provides a minimalistic API for handling keyboard events and drawing text-based UI elements, making it suitable for building terminal applications and games.

Pros

  • Simple and lightweight API for terminal manipulation
  • Cross-platform compatibility (works on Unix-like systems and Windows)
  • Pure Go implementation with no external dependencies
  • Supports 256 colors and Unicode characters

Cons

  • Limited built-in UI components compared to more comprehensive libraries
  • Lacks advanced features like mouse support or window management
  • Not actively maintained (last commit was in 2020)
  • May have compatibility issues with some terminal emulators

Code Examples

  1. Initializing and closing Termbox:
import "github.com/nsf/termbox-go"

func main() {
    err := termbox.Init()
    if err != nil {
        panic(err)
    }
    defer termbox.Close()

    // Your application code here
}
  1. Drawing text on the screen:
termbox.SetCell(x, y, '█', termbox.ColorRed, termbox.ColorDefault)
termbox.SetCell(x+1, y, '█', termbox.ColorGreen, termbox.ColorDefault)
termbox.SetCell(x+2, y, '█', termbox.ColorBlue, termbox.ColorDefault)
termbox.Flush()
  1. Handling keyboard events:
for {
    switch ev := termbox.PollEvent(); ev.Type {
    case termbox.EventKey:
        if ev.Key == termbox.KeyEsc {
            return
        }
    case termbox.EventError:
        panic(ev.Err)
    }
}

Getting Started

To use termbox-go in your project, follow these steps:

  1. Install the library:

    go get -u github.com/nsf/termbox-go
    
  2. Import the library in your Go file:

    import "github.com/nsf/termbox-go"
    
  3. Initialize termbox at the start of your application:

    err := termbox.Init()
    if err != nil {
        panic(err)
    }
    defer termbox.Close()
    
  4. Use termbox functions to draw on the screen and handle events:

    termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
    termbox.SetCell(0, 0, 'H', termbox.ColorWhite, termbox.ColorDefault)
    termbox.Flush()
    
  5. Run your application and interact with the terminal-based UI.

Competitor Comparisons

4,521

Tcell is an alternate terminal package, similar in some ways to termbox, but better in others.

Pros of tcell

  • More comprehensive and feature-rich, supporting a wider range of terminal capabilities
  • Better handling of color and style attributes, including RGB color support
  • More active development and maintenance

Cons of tcell

  • Slightly more complex API, which may require a steeper learning curve
  • Potentially higher memory usage due to its more extensive feature set

Code Comparison

termbox-go:

tb.Init()
defer tb.Close()

tb.SetCell(0, 0, 'H', tb.ColorDefault, tb.ColorDefault)
tb.Flush()

tcell:

s, _ := tcell.NewScreen()
s.Init()
defer s.Fini()

s.SetContent(0, 0, 'H', nil, tcell.StyleDefault)
s.Show()

Both libraries provide similar basic functionality for terminal manipulation, but tcell offers more advanced features and greater flexibility. While termbox-go has a simpler API, tcell's more comprehensive approach may be beneficial for complex terminal applications. The choice between the two depends on the specific requirements of your project and the level of control you need over the terminal environment.

10,669

Terminal UI library with rich, interactive widgets — written in Golang

Pros of tview

  • Higher-level abstraction with ready-to-use widgets and layouts
  • Built-in support for mouse events and flexible input handling
  • More actively maintained with frequent updates and improvements

Cons of tview

  • Steeper learning curve due to more complex API
  • Potentially higher resource usage for complex applications
  • Less flexibility for low-level terminal manipulation

Code Comparison

termbox-go:

tb.Init()
defer tb.Close()

tb.SetCell(0, 0, 'H', tb.ColorDefault, tb.ColorDefault)
tb.SetCell(1, 0, 'i', tb.ColorDefault, tb.ColorDefault)
tb.Flush()

tview:

app := tview.NewApplication()
box := tview.NewBox().SetBorder(true).SetTitle("Hello, World!")
app.SetRoot(box, true).Run()

Summary

termbox-go provides a low-level, lightweight interface for terminal manipulation, while tview offers a higher-level abstraction with pre-built widgets and layouts. termbox-go is simpler to use for basic terminal operations but requires more manual work for complex UIs. tview simplifies creating rich terminal UIs but may have a steeper learning curve and higher resource usage. Choose based on your project's complexity and requirements.

9,827

Minimalist Go package aimed at creating Console User Interfaces.

Pros of gocui

  • Higher-level abstraction for creating text-based user interfaces
  • Built-in support for multiple views and layouts
  • Easier to create complex TUI applications with less code

Cons of gocui

  • Less flexible for low-level terminal manipulation
  • Steeper learning curve for simple applications
  • More opinionated design, which may not suit all use cases

Code comparison

termbox-go:

tb.Init()
defer tb.Close()

tb.SetCell(0, 0, 'H', tb.ColorDefault, tb.ColorDefault)
tb.SetCell(1, 0, 'i', tb.ColorDefault, tb.ColorDefault)
tb.Flush()

gocui:

g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
    // handle error
}
defer g.Close()

g.SetManagerFunc(layout)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
    // handle error
}

Summary

termbox-go provides a lower-level interface for terminal manipulation, offering more flexibility but requiring more code for complex UIs. gocui, on the other hand, provides a higher-level abstraction, making it easier to create complex TUI applications with multiple views and layouts. However, gocui may be overkill for simple applications and has a steeper learning curve. The choice between the two depends on the specific requirements of your project and your preferred level of abstraction.

13,132

Golang terminal dashboard

Pros of termui

  • Higher-level abstraction for building terminal UIs
  • Built-in widgets and layouts for faster development
  • More visually appealing and feature-rich interfaces

Cons of termui

  • Steeper learning curve due to more complex API
  • Less flexibility for low-level terminal manipulation
  • Potentially higher resource usage for complex UIs

Code Comparison

termbox-go (low-level approach):

tb.Init()
defer tb.Close()
tb.SetCell(0, 0, 'H', tb.ColorDefault, tb.ColorDefault)
tb.SetCell(1, 0, 'i', tb.ColorDefault, tb.ColorDefault)
tb.Flush()

termui (high-level approach):

ui.Init()
defer ui.Close()
p := widgets.NewParagraph()
p.Text = "Hi"
ui.Render(p)

termbox-go provides direct control over individual cells in the terminal, while termui offers pre-built widgets and a more abstracted rendering process. termbox-go is better suited for simple, performance-critical applications or when fine-grained control is needed. termui excels in creating complex, interactive terminal interfaces with less code but at the cost of some flexibility and potential overhead.

2,091

A UI library for terminal applications.

Pros of tui-go

  • Higher-level abstraction, making it easier to create complex UIs
  • Built-in widgets and layouts for faster development
  • More actively maintained with recent updates

Cons of tui-go

  • Larger API surface area, potentially steeper learning curve
  • Less flexible for low-level terminal manipulation
  • Slightly higher memory footprint due to additional abstractions

Code Comparison

termbox-go:

termbox.Init()
defer termbox.Close()

termbox.SetCell(0, 0, 'H', termbox.ColorDefault, termbox.ColorDefault)
termbox.SetCell(1, 0, 'i', termbox.ColorDefault, termbox.ColorDefault)
termbox.Flush()

tui-go:

root := tui.NewHBox(
    tui.NewLabel("Hello, world!"),
)

ui, err := tui.New(root)
if err != nil {
    log.Fatal(err)
}
ui.Run()

Summary

termbox-go provides a lower-level API for terminal manipulation, offering more flexibility but requiring more code for complex UIs. tui-go, on the other hand, offers a higher-level abstraction with built-in widgets and layouts, making it easier to create sophisticated terminal UIs quickly. However, this comes at the cost of a larger API surface area and potentially less flexibility for low-level operations. Choose termbox-go for fine-grained control or tui-go for rapid development of feature-rich terminal applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Go Reference Build

IMPORTANT

This library is somewhat not maintained anymore. But I'm glad that it did what I wanted the most. It moved people away from "ncurses" mindset and these days we see both re-implementations of termbox API in various languages and even possibly better libs with similar API design. If you're looking for a Go lib that provides terminal-based user interface facilities, I've heard that gdamore/tcell is good (never used it myself). Also for more complicated interfaces and/or computer games I recommend you to consider using HTML-based UI. Having said that, termbox still somewhat works. In fact I'm writing this line of text right now in godit (which is a text editor written using termbox-go). So, be aware. Good luck and have a nice day.

Termbox

Termbox is a library that provides a minimalistic API which allows the programmer to write text-based user interfaces. The library is crossplatform and has both terminal-based implementations on *nix operating systems and a winapi console based implementation for windows operating systems. The basic idea is an abstraction of the greatest common subset of features available on all major terminals and other terminal-like APIs in a minimalistic fashion. Small API means it is easy to implement, test, maintain and learn it, that's what makes the termbox a distinct library in its area.

Installation

Install and update this go package with go get -u github.com/nsf/termbox-go

Examples

For examples of what can be done take a look at various examples in the _demos directory. You can try them with go run: go run _demos/keyboard.go

There are also some interesting projects using termbox-go:

  • godit is an emacsish lightweight text editor written using termbox.
  • gotetris is an implementation of Tetris.
  • sokoban-go is an implementation of sokoban game.
  • hecate is a hex editor designed by Satan.
  • httopd is top for httpd logs.
  • mop is stock market tracker for hackers.
  • termui is a terminal dashboard.
  • termloop is a terminal game engine.
  • xterm-color-chart is a XTerm 256 color chart.
  • gocui is a minimalist Go library aimed at creating console user interfaces.
  • dry is an interactive cli to manage Docker containers.
  • pxl displays images in the terminal.
  • snake-game is an implementation of the Snake game.
  • gone is a CLI pomodoro® timer.
  • Spoof.go controllable movement spoofing from the cli.
  • rat lets you compose shell commands to build terminal applications.
  • httplab An interactive web server.
  • wot Wait time during command is completed.
  • 2048-go is 2048 in Go.
  • jv helps you view JSON on the command-line.
  • pinger helps you to monitor numerous hosts using ICMP ECHO_REQUEST.
  • vixl44 lets you create pixel art inside your terminal using vim movements.
  • zterm is a typing game inspired by http://zty.pe/.
  • gotypist is a fun touch-typing tutor following Steve Yegge's method.
  • cointop is an interactive terminal based UI application for tracking cryptocurrencies.
  • pexpo is a terminal sending ping tool written in Go.
  • jid is an interactive JSON drill down tool using filtering queries like jq.
  • nonograminGo is a nonogram (aka. picross) in Go.
  • tower-of-go is a tiny maze game that runs on the terminal.