| Crate | Version | Docs |
|---|---|---|
weavetui |
||
weavetui_core |
||
weavetui_derive |
weavetui is a modern, robust, and modular Text User Interface (TUI) framework for Rust, 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.
β¨ Features
- Component-Driven Architecture: Leverage a declarative, component-based model for building complex UIs with ease.
- Automatic Trait Implementation: Utilize the
weavetui_deriveprocedural macro for automatic implementation ofComponentandComponentAccessortraits, significantly reducing boilerplate. - Modular Design: Built with
weavetui_coreas its foundation, ensuring high modularity, maintainability, and extensibility. - Event Handling System: A flexible event system for managing keyboard, mouse, and custom events within your TUI applications.
- Real-world Application Example: The
weavetuiapplication itself serves as a comprehensive example, demonstrating best practices for component composition, state management, and event handling. - Interactive User Experience: Designed to deliver responsive and engaging user experiences directly within the terminal environment.
π 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
weavetui is designed to simplify the development of complex Text User Interface (TUI) applications in Rust. Its essence lies in its component-based architecture and the use of procedural macros to reduce boilerplate. Here's a detailed overview of how it works:
graph TD
subgraph " Phase 1: Compile Time"
A[Developer writes 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] --> G[Terminal Initialization & `ComponentManager`];
G --> H{Main Event Loop};
subgraph "Event & Render Cycle"
H --"Waiting for input..."--> I[User provides input (Keyboard/Mouse)];
I --> J[`EventHandler` from `weavetui_core`];
J --"Event distributed"--> K[`ComponentManager`];
K --"Finds active component & calls `handle_key_events`"--> L[Active Component];
L --"1. Internal state is modified"--> L;
L --"2. Returns 'Action' (optional)"--> M{Action Handling};
M --"Action processed (e.g., 'quit', 'submit')"--> H;
K --"Triggers render process after event"--> N[Render Engine];
N --"Calls `draw()` on each visible component"--> O[Each Component];
O --"Renders view to buffer"--> P[Terminal Buffer];
P --> Q[Terminal Display updated];
Q --> 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
-
Developer Writes Component: You define your UI as a
structin Rust and add the#[component]attribute above it. This attribute is the main key to simplifying the process. -
Compile Time (Macro Magic):
- When you compile your project (
cargo build),weavetui_derive(a procedural macro) becomes active. - This macro automatically writes boilerplate code for you. It implements two important traits from
weavetui_core:Component: Handles core logic such ashandle_key_events,handle_mouse_events, anddraw.ComponentAccessor: Provides methods to access common properties like component name and active status.
- When you compile your project (
-
Runtime (Application Running):
- Initialization: Your main application starts, setting up the terminal for TUI rendering and creating a
ComponentManager. ThisComponentManageris responsible for managing the entire state and lifecycle of all components. - Event Loop: The application enters an infinite loop that continuously waits for three things: user input, internal events, and signals to re-render the UI.
- Initialization: Your main application starts, setting up the terminal for TUI rendering and creating a
-
Event Cycle:
- User Input: When you press a key (e.g.,
jfor down), theEventHandlercaptures it. - Event Distribution: This event is passed to the
ComponentManager. - Event Handling: The
ComponentManagerdetermines which component is currently active, then calls thehandle_key_eventsmethod on that component. This is where your logic (withinimpl Component) is executed. The component can modify its internal state (e.g., change the selected item in a list). - Actions: After handling an event, a component can return an
Action(e.g., as aStringlike"quit"or"save"). ThisActionis how components communicate back to theComponentManageror other components to trigger larger changes.
- User Input: When you press a key (e.g.,
-
Render Cycle:
- After an event is handled, the
ComponentManagerwill trigger the render process to update the display. - The render engine will traverse your component tree and call the
draw()method on each visible component. - Each component then "draws" itself to a buffer in memory.
- Once all components have been drawn to the buffer, the buffer is displayed to the terminal, and you will see the changes on the screen.
- After an event is handled, the
-
Back to Loop: The process returns to the beginning, waiting for the next user input. This cycle repeats rapidly, giving the illusion of an interactive and responsive 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.