Rand Stats

Termbox2

zef:arne

Termbox2

Raku NativeCall bindings to termbox2, a small C library for writing text-based user interfaces.

termbox2 is a single-header, dependency-free C library, vendored in this distribution as resources/termbox2.h (MIT licensed, see the header for the full license text). It is compiled into a shared library at install time by Build.rakumod, using LibraryMake for cross-platform compiler/linker flags — no external build system, and no dependency beyond a C compiler.

This distribution's eg/ examples are adapted from Sanko Robinson's Termbox Perl module, which binds the same termbox2 library. This module's own design — in particular the tagged exports described under "Import tags" below — was inspired by José Joaquín Atria's Termbox, the original Raku binding to termbox2's predecessor, the now-unmaintained termbox v1 library.

resources/termbox2.h carries two local patches on top of upstream:

Reapply both patches if termbox2.h is ever updated from upstream.

Usage

use Termbox2 :ALL;

tb-init();
tb-set-cell(0, 0, 'X'.ord, TB_WHITE, TB_BLACK);
tb-present();

my $event = Event.new;
tb-poll-event($event);

tb-shutdown();

API

Every public, non-deprecated function in the vendored termbox2.h is bound except for a handful documented under "Not bound" below. All tb-* functions return TB_OK (0) on success, or a negative TB_ERR_* code (tb-strerror/tb-version/tb-wcwidth/tb-iswprint return something else; noted below).

Import tags

use Termbox2; (bare, or with :ALL) imports everything, as in the example above. Everything is also available under a narrower tag, to avoid pulling the full symbol set into scope:

TagContents
:errorsTB_OK, TB_ERR*
:keysTB_KEY_*, TB_MOD_*
:stylesColors (TB_DEFAULT..TB_WHITE) and attributes (TB_BOLD..TB_DIM)
:eventsTB_EVENT_*, Event
:modesTB_INPUT_*, TB_OUTPUT_*
:cellsCell
:subsEvery tb-* function

For example, use Termbox2 :keys, :subs; imports just the key constants and functions, without the color/style/event/mode constants. t/03-exports.t asserts that each tag (and bare use/:ALL) imports exactly its documented symbols — no more, no less.

Not bound

Not bound behavior

This distribution's shared library is built with termbox2's defaults: 16-bit fg/bg attributes (TB_OPT_ATTR_W unset) and extended grapheme cluster support disabled (TB_OPT_EGC unset) — confirmed at runtime by tb-attr-width() returning 16 and tb-has-egc() returning 0. Practical effects:

Migrating from Termbox (v1)

José Joaquín Atria's Termbox binds the older, now-unmaintained termbox v1 C library. termbox2 is a distinct, actively developed successor — not a drop-in .so swap — so moving code from Termbox to Termbox2 means updating the Raku call sites, not just the use line. The two APIs are similar enough that most of that is mechanical:

Development

zef install . --force-test

requires only a C compiler (cc) and make — no Python, no git submodules, unlike the original Termbox binding to the (now unmaintained) termbox v1 library.

Source

Source code, issues, and pull requests: github.com/arnesom/raku-termbox2

Author

Arne Sommer arne@sommer.pm

Parts of this distribution were developed with AI assistance (Claude and Qwen).

License

This Raku binding is licensed under the Artistic License 2.0, the same license as Raku itself. See LICENSE for the full text.

The vendored resources/termbox2.h (and the termbox2-impl.c wrapper compiled from it) is termbox2, MIT licensed — see the header for the full license text.