tvxterm is a terminal emulator widget for tview.
It gives you a small VT-style emulator plus a tview.Primitive, so you can embed an interactive terminal inside terminal UIs written in Go.
tviewprimitive for rendering terminal output- PTY backend for local shell and command execution
- stream backend for SSH sessions or custom transports
- UTF-8, wide rune, combining rune, and scrollback support
- terminal input handling for keyboard, paste, focus, and mouse reporting
- OSC title tracking and callback hooks
The package name is tvxterm, and it can be imported directly from the repository root.
import "github.com/blacknon/tvxterm"go get github.com/blacknon/tvxtermpackage main
import (
"log"
"os"
"os/exec"
"github.com/rivo/tview"
"github.com/blacknon/tvxterm"
)
func main() {
app := tview.NewApplication()
term := tvxterm.New(app)
cmd := exec.Command(shell())
cmd.Env = append(os.Environ(), "TERM=xterm-256color")
backend, err := tvxterm.NewPTYBackend(cmd, 80, 24)
if err != nil {
log.Fatal(err)
}
defer backend.Close()
term.Attach(backend)
if err := app.SetRoot(term, true).SetFocus(term).Run(); err != nil {
log.Fatal(err)
}
}
func shell() string {
if sh := os.Getenv("SHELL"); sh != "" {
return sh
}
return "/bin/sh"
}Use NewPTYBackend when you want to run a local shell or process inside a pseudo terminal.
cmd := exec.Command("/bin/sh")
backend, err := tvxterm.NewPTYBackend(cmd, 80, 24)Use NewStreamBackend when another library already owns the session and only gives you io.Reader / io.Writer.
backend := tvxterm.NewStreamBackend(reader, writer, resizeFunc, closeFunc)
term.Attach(backend)This is useful for:
- SSH clients
- container exec sessions
- remote agent transports
- custom REPL bridges
A runnable sample is included:
go run ./sample/localptyWhat the sample shows:
- local shell execution through
PTYBackend - window title updates from OSC sequences
- status line updates
- local scrollback control with
PageUp/PageDown - exit with
Ctrl+Q
Additional samples:
go run ./sample/muxpty
go run ./sample/go-sshlib-shellsample/muxpty: terminal mux style sample with dynamic pane splittingCtrl+A ccreates a new page,Ctrl+A %/Ctrl+A "splits the current page,Ctrl+A n/pswitches pages,Ctrl+A omoves pane focus,Ctrl+A wopens the page listsample/go-sshlib-shell:go-sshlibsession rendered inside atvxtermview viaStreamBackendCtrl+A ccreates a new page,Ctrl+A %/Ctrl+A "splits a new SSH pane in the current page,Ctrl+A n/pswitches pages,Ctrl+A omoves focus,Ctrl+A wopens the page list
For sample/go-sshlib-shell, you can use flags:
go run ./sample/go-sshlib-shell \
-host example.com \
-port 22 \
-user myuser \
-key-path $HOME/.ssh/id_ed25519 \
-key-passphrase ''Or password authentication:
go run ./sample/go-sshlib-shell \
-host example.com \
-port 22 \
-user myuser \
-password mypasswordEnvironment variables TVXTERM_SSH_HOST, TVXTERM_SSH_PORT, TVXTERM_SSH_USER, TVXTERM_SSH_PASSWORD, TVXTERM_SSH_KEY_PATH, and TVXTERM_SSH_KEY_PASSPHRASE can also be used as defaults.
The main exported pieces are:
View: thetviewwidgetBackend: transport abstraction for terminal I/OPTYBackend: local PTY-backed process launcherStreamBackend: adapter for generic streamsEmulator: exported terminal state machine for testing or advanced use
Useful hooks on View:
Attach(backend)to connect a sessionSetBackendExitHandler(...)to react when the backend closesSetTitleHandler(...)to receive title updatesScrollbackUp,ScrollbackDown,ScrollbackPageUp,ScrollbackPageDown
- This library is focused on embedding terminal sessions into
tviewapplications. - The emulator is intentionally small rather than a full xterm clone.
- Alternate screen, colors, mouse reporting, bracketed paste, and title handling are supported, but some terminal features may still be incomplete.
MIT. See LICENSE.
