terminux is a small Go library for drawing and handling input in a terminal using ANSI escape codes.
It gives you:
- Raw keyboard input
- Double buffering (only changed cells are redrawn)
- Simple drawing helpers (points, rectangles, lines, etc.)
No dependencies beyond golang.org/x/term.
go get github.com/mslaursen/terminux- Create a screen
- Put the terminal into raw mode
- Run a loop that:
- clears the back buffer
- draws things
- flushes the buffer to the terminal
- Restore the terminal on exit
Input is handled asynchronously via an event listener.
package main
import (
"time"
"github.com/mslaursen/terminux"
)
func main() {
screen := terminux.NewScreenDefault()
screen.HideCursor()
defer screen.Restore()
running := true
screen.SetEventListener(func(event string) {
if event == "q" {
running = false
}
})
go screen.ListenForEvents()
ticker := time.NewTicker(time.Second / 30)
defer ticker.Stop()
for range ticker.C {
if !running {
break
}
screen.Clear()
screen.Draw(10, 10, '@')
screen.Display()
}
}Input is read rune by rrune in raw mode.
screen.SetEventListener(func(event string) {
fmt.Println(event)
})
go screen.ListenForEvents()Each key press is passed to the listener as a string.
terminux switches the terminal into raw mode.
Always restore it before exiting:
defer screen.Restore()This clears the screen, shows the cursor again, and resets terminal settings.