π termplot-rs
High-performance terminal graphics engine (TUI).
termplot-rs allows you to render mathematical plots, 3D visualizations, games, and complex interfaces directly in the console using Unicode Braille characters (2Γ4 dot matrix per character) and ANSI colors.
Unlike other TUI plotting libraries, termplot-rs is designed for critical speed: it uses flat memory buffers (Vec<u8>), bitwise operations, mathematical clipping, and a true zero-allocation rendering loop to achieve thousands of FPS in real-time applications.
π New in v0.9.0: True Zero-Allocation rendering (
render_to), Cohen-Sutherland Line Clipping, Filled Primitives (rect_filled,circle_filled), Pixel Erasing (unset_pixel), and Color Blending modes!
β¨ Key Features
- High Resolution: 8 sub-pixels per character (Braille 2x4). A 100x50 terminal yields a 200x200 effective pixel canvas.
- Extreme Performance:
- Flat buffers for maximum CPU cache locality.
- True Zero-Allocation Loop: Render directly to
std::fmt::Writeorstdout.lock()without allocating a singleStringper frame. - Cohen-Sutherland Clipping: Mathematically discards off-screen geometry before rasterization, saving massive CPU cycles during zoom or out-of-bounds drawing.
- Advanced Pixel & Color Control:
- Erase and toggle individual Braille dots (
unset_pixel,toggle_pixel). - Color Blending Modes: Control how sub-pixels sharing the same terminal cell interact (
OverwritevsKeepFirst).
- Erase and toggle individual Braille dots (
- Drawing Primitives:
- Lines (Bresenham), Circles, Polygons.
- Filled Shapes:
rect_filledandcircle_filled. - Text Layer (overlay).
- Ready-to-use Charts:
scatter(),line_chart(),bar_chart(),pie_chart(),plot_function().
- Auto-Range & Smart Axes: Automatic axis scaling and tick generation based on your dataset.
π¦ Installation
Add this to your Cargo.toml:
[]
= "0.9.0"
= "2.0"
# Optional, for generating test data
= "0.8"
π Quick Start
use ChartContext;
use Color;
ποΈ Zero-Allocation Render Loop (For Games/Animations)
If you are building a real-time app at 60 FPS, avoid render() (which creates a new String every frame) and use render_to():
use Write;
// Inside your game loop:
let mut buffer = Stringwith_capacity;
chart.canvas.render_to?;
print!;
buffer.clear; // Reuse memory!
π Coordinate System & Pixel API
To avoid mathematical confusion, termplot-rs offers two coordinate modes and multiple pixel operators:
| Coordinate Mode | Origin (0,0) | Y Direction | Best For |
|---|---|---|---|
| Cartesian | Bottom-Left | Grows Up | Math plots, functions, charts. |
| Screen | Top-Left | Grows Down | UI, Games, Sprites, 3D Projections. |
Pixel Manipulation Methods:
set_pixel / set_pixel_screen: Turns a dot ON.unset_pixel / unset_pixel_screen: Turns a dot OFF (Erases).toggle_pixel_screen: Flips the current state of a dot.
π§ͺ Examples & Demos
The repository includes advanced examples to showcase the library's power.
1. Primitive Shapes & Blending (NEW)
Interactive screensaver showcasing Cohen-Sutherland clipping, filled shapes, pixel erasing (dynamic holes), and real-time color blending mode switching.
2. Solar System Kepler 3D
Full physics simulation of the Solar System using true orbital mechanics, 3D rotations, and a custom software Z-Buffer.
3. Sprite Engine
A retro space-invaders style demo showcasing how to load and render custom ASCII art as fast Braille sprites.
4. Interactive Fractals
Mandelbrot and Julia explorer with infinite Zoom and rotation.
5. Chart Gallery
Shows all available static chart types (Bars, Scatter, Pie, Auto-Ticks).
β‘ Performance
termplot-rs is rigorously optimized.
In a benchmark with a 236x104 sub-pixel canvas (full fill with trigonometric noise and particles), on a modern machine:
- Debug Mode: ~60 FPS
- Release Mode: ~1600+ FPS
This makes it viable for audio visualization, high-frequency server monitoring, retro terminal games, or lightweight physics simulations.
πΊοΈ Roadmap
- Memory optimization (Flat
Vec<u8>buffers). - Explicit coordinate APIs (
screenvscartesian). - Mathematical Cohen-Sutherland Line Clipping.
- True zero-allocation rendering (
render_to). - Filled Primitives (
rect_filled,circle_filled) & Erasers. - Color Blending Policies (
Overwrite,KeepFirst). - Logarithmic scaling support.
- Automatic Legend Box.
- Trait-based pluggable terminal renderers (
CellRendererfor HalfBlocks/Quadrants).
π License
This project is licensed under the MIT license. Feel free to use it in your CLI tools, dashboards, or graphical experiments.