Skip to main content

Minimal Python Charm stack — raw terminal TUI framework

Project description

snaptui

A zero-dependency Python TUI framework that ports the Charm stack's approach to terminal UIs: the Elm Architecture, raw terminal I/O, and ANSI escape sequences.

Why this exists

Python's TUI options are either curses-based (complex, stateful, platform-dependent) or heavy frameworks like Textual (hundreds of dependencies, CSS layout engine, async runtime). The Go ecosystem solved this with the Charm stack: a set of composable libraries that render TUIs by writing ANSI escape sequences directly to stdout. This makes rendering fast and the architecture simple -- your entire UI is a function that returns a string.

snaptui brings that approach to Python:

  • No curses. Reads raw bytes from stdin, writes ANSI sequences to stdout.
  • No dependencies. Uses only the Python standard library (termios, select, signal, fcntl).
  • Elm Architecture. Your app is init(), update(msg), view() -- nothing else.
  • Fast rendering. A line-diff renderer only redraws what changed, just like Bubble Tea.

How the Elm Architecture works

from snaptui import Model, Cmd, KeyMsg, WindowSizeMsg, quit_cmd

class Counter:
    def __init__(self):
        self.count = 0

    def init(self) -> Cmd:
        return None

    def update(self, msg) -> tuple['Counter', Cmd]:
        if isinstance(msg, KeyMsg):
            if msg.key == 'q':
                return self, quit_cmd
            elif msg.key == 'up':
                self.count += 1
            elif msg.key == 'down':
                self.count -= 1
        return self, None

    def view(self) -> str:
        return f"Count: {self.count}\n\nup/down to change, q to quit"

The program calls view() after every update(), diffs the output, and writes only the changed lines. There's no widget tree, no layout engine, no virtual DOM -- just a string.

Why escape sequences make this fast

Traditional TUI frameworks (curses, Textual) maintain an internal screen buffer and compute cell-by-cell diffs. snaptui skips all of that:

  1. Your view() returns a string with embedded ANSI codes for colors, bold, etc.
  2. The renderer splits it into lines and compares against the previous frame.
  3. Only changed lines get written using cursor-movement escape sequences.
  4. One flush() per frame -- the terminal handles the rest.

Because ANSI rendering is done by the terminal emulator (which is GPU-accelerated in modern terminals like Ghostty, Kitty, WezTerm), the Python side only needs to produce and diff strings. There's no pixel math, no redraw-the-whole-screen, no intermediate buffer.

What's ported from where

snaptui combines features from four Charm libraries into one package, porting from these exact versions:

Go module Version
charmbracelet/bubbletea v1.3.10
charmbracelet/lipgloss v1.1.0
charmbracelet/bubbles v1.0.0
charmbracelet/huh v0.8.0
charmbracelet/x/ansi v0.11.6

Here's what's implemented and what's not:

Core Framework

Feature Charm Source snaptui Status
Elm Architecture (init/update/view) bubbletea Model protocol, Program Done
Message types (Key, WindowSize, Quit) bubbletea KeyMsg, WindowSizeMsg, QuitMsg Done
Command system (async Cmd) bubbletea Cmd, batch(), quit_cmd Done
Raw terminal mode (termios) bubbletea terminal.make_raw() Done
Alternate screen buffer bubbletea Program(alt_screen=True) Done
Line-diff renderer bubbletea renderer.Renderer Done
Keyboard input parsing (70+ sequences) bubbletea keys.read_key() Done
SIGWINCH resize handling bubbletea terminal.listen_for_resize() Done
Mouse input events bubbletea -- Not yet
Bracketed paste events bubbletea -- Not yet
Focus/blur events bubbletea v2 -- Not yet
Kitty keyboard protocol bubbletea v2 -- Not yet
Clipboard (OSC 52) bubbletea v2 -- Not yet
Subscriptions (long-running listeners) bubbletea -- Not yet
Suspend/resume (Ctrl+Z) bubbletea -- Not yet

Styling

Feature Charm Source snaptui Status
Chainable style builder lipgloss Style class Done
Text attributes (bold, dim, italic, underline, reverse, strikethrough) lipgloss Style.bold(), .dim(), etc. Done
True color (24-bit RGB) lipgloss Style.fg(), .bg() Done
Padding (CSS shorthand) lipgloss Style.padding() Done
Margin (CSS shorthand) lipgloss Style.margin() Done
Width/height constraints lipgloss Style.width(), .height() Done
Max width/height lipgloss Style.max_width(), .max_height() Done
Auto word-wrap on .width() lipgloss Style._apply_wrap() Done
Borders (5 types, per-side, colored) lipgloss Style.border(), .border_fg() Done
Horizontal alignment lipgloss Style.align() Done
Immutable builder pattern lipgloss Style._copy() Done
ANSI 16/256 color fallback lipgloss -- Not yet
Adaptive colors (light/dark) lipgloss -- Not yet
Vertical alignment lipgloss -- Not yet
Tab width lipgloss -- Not yet
Table rendering lipgloss/table -- Not yet
List rendering (enumerators) lipgloss/list -- Not yet
Tree rendering lipgloss/tree -- Not yet

Layout

Feature Charm Source snaptui Status
Join blocks horizontally lipgloss join_horizontal() Done
Join blocks vertically lipgloss join_vertical() Done
Place content in canvas (2D) lipgloss place() Done
PlaceHorizontal / PlaceVertical lipgloss -- Not yet

String Utilities

Feature Charm Source snaptui Status
ANSI-aware visible width x/ansi strutil.visible_width() Done
ANSI stripping x/ansi strutil.strip_ansi() Done
ANSI-aware truncation x/ansi strutil.truncate() Done
ANSI-aware word wrap x/ansi strutil.word_wrap() Done
ANSI-aware padding x/ansi strutil.pad_right() Done
CJK wide character support x/ansi strutil._char_width() Done
Grapheme cluster segmentation x/ansi -- Not yet
Hard wrap (non-word-boundary) x/ansi -- Not yet (hard-break exists within word_wrap)
Truncate with tail ("...") x/ansi -- Not yet

Components

Component Charm Source snaptui Status
Single-line text input bubbles/textinput TextInput Done
Multi-line text area bubbles/textarea TextArea Done
Scrollable viewport bubbles/viewport Viewport Done
Paginated list (delegate pattern) bubbles/list + bubbles/paginator List Done
Option picker (select) bubbles/list Select Done
Yes/No confirmation huh Confirm Done
Multi-field form huh Form Done
Table bubbles/table -- Not yet
Spinner bubbles/spinner -- Not yet
Progress bar bubbles/progress -- Not yet
File picker bubbles/filepicker -- Not yet
Timer / Stopwatch bubbles/timer, bubbles/stopwatch -- Not yet
Help (auto-generated keybinds) bubbles/help -- Not yet

Legend

  • Done -- Implemented and tested
  • Not yet -- Not implemented; listed here so future apps know what to add when they need it

Architecture

your_app.py
    |
    +-- snaptui (framework + styling + components in one package)
            |
            +-- model.py        Elm Architecture: Model protocol, Msg, Cmd
            +-- keys.py         Keyboard input: escape sequence parser
            +-- program.py      Event loop: raw mode, resize, render cycle
            +-- terminal.py     Low-level: termios, ioctl, ANSI sequences
            +-- renderer.py     Line-diff screen updates
            +-- style.py        Lip Gloss equivalent: chainable styling
            +-- strutil.py      ANSI-aware string ops: width, wrap, truncate
            +-- layout.py       join_horizontal, join_vertical, place
            +-- components/
                    +-- textinput.py   Single-line input
                    +-- textarea.py    Multi-line editor
                    +-- viewport.py    Scrollable content
                    +-- list.py        Paginated list with delegates
                    +-- select.py      Option picker
                    +-- confirm.py     Yes/No prompt
                    +-- form.py        Multi-field form

In Go, these are spread across four repos (bubbletea, lipgloss, bubbles, x/ansi). snaptui combines them because Python apps can just import what they need from one package.

File mapping

Exact Go source → snaptui file mapping for reference when porting new features or chasing bugs back to the Go implementation.

Go source repos:

  • bubbletea (v1.3.10) — event loop, renderer, key input
  • lipgloss (v1.1.0) — styling, layout, borders
  • bubbles (v1.0.0) — textinput, textarea, viewport, list
  • huh (v0.8.0) — form, select, confirm, theme
  • x/ansi (v0.11.6) — string width, wrap, truncate
snaptui file Go source(s)
model.py bubbletea/tea.go
program.py bubbletea/tea.go, bubbletea/signals_unix.go
renderer.py bubbletea/standard_renderer.go
keys.py bubbletea/key.go, bubbletea/key_sequences.go
terminal.py bubbletea/tty_unix.go, x/ansi/mode.go
style.py lipgloss/style.go, lipgloss/set.go, lipgloss/get.go, lipgloss/color.go, lipgloss/borders.go, lipgloss/whitespace.go, lipgloss/size.go
layout.py lipgloss/join.go, lipgloss/align.go, lipgloss/position.go
strutil.py x/ansi/width.go, x/ansi/wrap.go, x/ansi/truncate.go, x/ansi/style.go
theme.py huh/theme.go
components/textinput.py bubbles/textinput/textinput.go, bubbles/cursor/cursor.go
components/textarea.py bubbles/textarea/textarea.go
components/viewport.py bubbles/viewport/viewport.go, bubbles/viewport/keymap.go
components/list.py bubbles/list/list.go, bubbles/list/defaultitem.go, bubbles/paginator/paginator.go
components/select.py huh/field_select.go
components/confirm.py huh/field_confirm.go
components/form.py huh/form.go, huh/group.go

Running tests

python3 -m pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snaptui-0.1.2.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snaptui-0.1.2-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file snaptui-0.1.2.tar.gz.

File metadata

  • Download URL: snaptui-0.1.2.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for snaptui-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1e5a087f1da01ce106bc28d0bafc62e651d0d812557ce89676e5d20f20e12510
MD5 a326a4a8d1e10f95da535af3150470c7
BLAKE2b-256 683955b9d861a596cbfab782ba820bdd0b48d1812e3b1baff64ca20e12bdf1a3

See more details on using hashes here.

File details

Details for the file snaptui-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: snaptui-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for snaptui-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8656ac6168cfeb58b64c157eff9fbd9077e9726ff207f229ea6cb4e0d2554397
MD5 24d26a4afb47320ce51cc23ea8a7dc94
BLAKE2b-256 b8d6b9b173e9d48d47f95d257c641213f2faa72d303877a9ec1dd813f92739f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page