$ show janestreet/bonsai_term
Bonsai_term is a library for building Terminal User Interfaces (TUIs) using OCaml with a reactive programming model similar to bonsai_web. It provides a complete framework for handling terminal input/output, event processing (keyboard, mouse, resize), rendering views, and managing application state. The architecture follows a functional reactive pattern with clear separation between event handling, state management, rendering, and the main run loop. Key entry points are the start functions in loop.ml which take an application function defining the view and event handler. The system processes terminal events through an event queue, applies user-defined handlers to produce effects, and renders incremental view updates at a target frame rate.
The LLM identified 3 files worth highlighting for reuse:
Implements a clean event propagation pattern where handlers can indicate whether they've captured an event (preventing further propagation) or let it bubble up. The Let_syntax module provides ergonomic syntax for chaining handlers while respecting capture boundaries. This is a self-contained, reusable pattern with minimal dependencies (only Core) that solves a common problem in event-driven systems.
Provides a sophisticated view system with efficient memoization (Single_memo), layout operations (vcat, hcat, zcat), and a tagging system for associating metadata with views. The memoization avoids redundant computations when view inputs haven't changed, and the tagging system allows extracting layout information post-composition. Demonstrates advanced functional programming techniques for building reusable UI components.
Contains focused, isolated modules for managing different aspects of application state: dimensions (terminal size), clock (timekeeping), and exit status. Each module encapsulates its concern with a minimal, clear API. This separation makes the system easier to reason about, test, and maintain. The pattern is broadly applicable to any stateful application and could be extracted with minimal adaptation.