| Crate | Version | Docs |
|---|---|---|
weavetui |
||
weavetui_core |
||
weavetui_derive |
weavetui is a modern, robust, and modular Text User Interface (TUI) framework for Rust, built on top of ratatui and tokio. It is designed to simplify the development of sophisticated and interactive terminal applications. This repository serves as both the primary application showcasing the framework's capabilities and the foundational crates that enable its powerful component-based architecture.
β¨ Why weavetui?
weavetui empowers developers to build complex and interactive terminal applications with ease, offering:
- Component-Driven Development: Build UIs using reusable, self-contained components that implement the
ComponentandComponentAccessortraits, making your code modular and maintainable. - Reduced Boilerplate: Leverage procedural macros (
weavetui_derive) to automatically implement common traits, allowing you to focus on your application's unique logic rather than repetitive setup. - Robust Event Handling: A flexible and comprehensive event system, powered by
tokio, handles keyboard, mouse, and customActions, ensuring a responsive user experience. - Clear Architecture: A well-defined separation of concerns between core functionalities (
weavetui_core) and macro-based development (weavetui_derive) promotes clarity and extensibility. - Interactive & Responsive: Designed from the ground up to deliver engaging and fast-responding user interfaces directly within the terminal, with configurable tick and frame rates.
π Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
- Rust programming language (stable or beta channel recommended). You can install it via
rustup:| - Cargo, Rust's package manager (comes with Rust installation).
Installation
-
Clone the repository:
-
Build the project: This command will compile all crates within the workspace (
weavetui,weavetui_core,weavetui_derive).Using
--releaseis recommended for optimized performance.
Running the Application
To launch the weavetui example application (which demonstrates the framework's capabilities):
This command compiles and runs the main weavetui application, serving as a practical demonstration of the framework's features directly in your terminal.
π Project Structure
This repository is organized as a Rust workspace, containing the following crates:
weavetui/(root): The main application crate that orchestrates the UI and application logic, serving as a practical demonstration of the framework.weavetui_core/: A foundational library defining core TUI traits (Component,ComponentAccessor), event handling mechanisms, and utility functions.weavetui_derive/: A procedural macro crate providing the#[component]attribute for automatic trait implementation, simplifying component creation.
βοΈ How weavetui Works: A Simplified Overview
weavetui simplifies TUI development in Rust through a component-based architecture and powerful procedural macros. Here's a breakdown of its core mechanics:
graph TD
subgraph " Phase 1: Compile Time"
A[Developer defines Component Struct & adds [component]] --> B{Rust Compiler};
B --> C[weavetui_derive Crate];
C --"Macro [component] active"--> D[Implementation of Component & ComponentAccessor traits is automatically generated];
D --> E[Complete Component Code];
end
subgraph "Phase 2: Runtime"
F[`weavetui` Application starts App] --> G[Terminal Initialization Tui & ComponentManager];
G --> H{Main Event Loop App run};
subgraph "Event & Render Cycle"
H --"Waiting for input/events..."--> I[User input KeyEvent, MouseEvent, Paste or internal events Tick, Render];
I --> J[Tui polls crossterm events];
J --"Emits weavetui_core::event::Event"--> K[App main application loop];
K --"Dispatches Event to ComponentManager"--> L[ComponentManager];
L --"Finds active component & calls handle_key_events, etc."--> M[Active Component];
M --"1. Internal state is modified"--> M;
M --"2. Returns Action (optional)"--> N{Action Handling};
N --"Action processed by App or other components"--> O[State Update & Re-render Trigger];
O --> P[Render Engine Tui];
P --"Calls draw() on each visible component"--> Q[Each Component];
Q --"Renders view to buffer"--> R[Terminal Buffer];
R --> S[Terminal Display updated];
S --> H;
end
end
style A fill:#f9f,stroke:#333,stroke-width:2px
style C fill:#f9f,stroke:#333,stroke-width:2px
style F fill:#ccf,stroke:#333,stroke-width:2px
style I fill:#ccf,stroke:#333,stroke-width:2px
Detailed Explanation
-
Define Your UI as Components: You start by defining your UI elements as Rust
structs. By adding the#[component]attribute fromweavetui_deriveto your struct, you tell the framework that this struct should behave as a UI component. This macro automatically injects essential fields like_ctx(containingchildren,area,active,action_tx, andtheme_manager) into your component, managing its internal state and context. -
Automatic Trait Implementation (Compile Time):
- During compilation, the
#[component]procedural macro automatically generates the necessary boilerplate code. - Specifically, it implements the
ComponentandComponentAccessortraits (defined inweavetui_core) for your struct. These traits provide the fundamental methods for handling events, drawing the UI, and managing component properties. This automation significantly reduces manual coding, allowing you to focus on the unique logic of your components.
- During compilation, the
-
Application Lifecycle (Runtime):
- Initialization: When your
weavetuiapplication starts (driven by theAppstruct), it sets up the terminal environment using theTuiutility. TheAppalso initializes aComponentManager, which is the central orchestrator responsible for holding all your UI components and managing their state and interactions. - Event Loop: The
Appthen enters a continuous asynchronous loop (withinApp.run()), constantly monitoring for user input (like key presses or mouse clicks) and internal events (likeTickfor periodic updates orRenderfor UI redraws). TheTuicomponent pollscrosstermevents and translates them intoweavetui_core::event::Eventenum variants.
- Initialization: When your
-
Event Handling Flow:
- Input Capture & Dispatch: When an
Eventoccurs (e.g., aKeyEventfrom a key press), theTuiemits it, and theApp's main loop receives it. TheAppthen dispatches thisEventto theComponentManager. - Component Action: The
ComponentManageridentifies the currently active component (and its children) and dispatches the event to the appropriate handler method (e.g.,handle_key_events,handle_mouse_events,on_event). Here, your component's logic processes the event, potentially modifying its internal state (e.g., updating a counter, changing a selected item). - Actions & Communication: After processing an event, a component can optionally return an
Action(e.g., a command likeAction::QuitorAction::AppAction("submit")). TheseActions are a primary way for components to communicate with theAppor other parts of the application, triggering broader changes or application-level responses. TheAppprocesses theseActions, which can lead to state updates or further event dispatches.
- Input Capture & Dispatch: When an
-
Rendering the UI:
- After events and actions are processed, and if a re-render is triggered (e.g., by a
Renderaction or a state change), theComponentManagerinitiates a re-render of the UI. - The
Tui's rendering engine iterates through all visible components, calling theirdraw()method. - Each component draws its visual representation onto an in-memory buffer using
ratatuiprimitives. - Once the buffer is complete, its contents are efficiently sent to the terminal, updating what the user sees on the screen.
- After events and actions are processed, and if a re-render is triggered (e.g., by a
-
Continuous Interaction: This entire cycle of event handling, action processing, and rendering repeats rapidly, driven by
tokio's asynchronous runtime, creating the illusion of a fluid, interactive, and responsive terminal application.
π§ͺ Running Tests
To ensure the stability and correctness of the framework and application, you can run the test suite:
This command will execute all unit and integration tests across all workspace crates.
π€ Contributing
We welcome and encourage contributions from the community! Whether you're looking to report a bug, suggest an enhancement, or contribute code, please refer to our CONTRIBUTING.md for detailed guidelines on how to get involved.
πΊοΈ Roadmap
- Enhanced layout management system.
- More sophisticated styling and theming capabilities.
- Expanded set of pre-built UI components.
- Improved accessibility features.
- Comprehensive documentation and tutorials.
π License
This project is licensed under the MIT License. See the LICENSE file for details.