weavetui_core 0.1.2

Core traits and utilities for weavetui TUI framework.
Documentation

weavetui_core

Crates.io Docs.rs

weavetui_core is the foundational library for the weavetui Text User Interface (TUI) framework. It provides the essential building blocks, traits, and utilities that enable the creation of robust and interactive terminal applications.

✨ Features

  • Component-Oriented Design: Defines the Component and ComponentAccessor traits, promoting a modular and reusable approach to TUI development.
  • Comprehensive Event Handling: Offers a standardized mechanism for processing keyboard, mouse, tick, and paste events, ensuring smooth user interactions.
  • Flexible TUI Primitives: Provides core types and functions for low-level terminal interaction, including managing terminal state, drawing elements, and handling raw input/output.
  • Action Dispatch System: Facilitates inter-component communication through a clear and efficient action dispatching system.

🚀 Getting Started

This crate is primarily designed to be a dependency for weavetui applications and other weavetui-related crates. To use it in your project, add the following to your Cargo.toml:

[dependencies]
weavetui_core = "0.1.1" # Replace with the latest version or a path/git dependency for development

📚 Examples

While weavetui_core provides the underlying traits, its usage is best demonstrated within a weavetui application. For more comprehensive and runnable examples, please refer to the examples directory in the main weavetui repository. Here's a glimpse of how components interact:

use weavetui_core::{
    Component,
    ComponentAccessor,
};
use ratatui::{
    Frame,
    layout::Rect,
    widgets::{Paragraph, Block, Borders},
};
use std::collections::BTreeMap;

// A simple component implementing the core traits
#[derive(Debug, Default)]
pub struct MyCustomComponent;

impl ComponentAccessor for MyCustomComponent {
    fn name(&self) -> String { "MyCustomComponent".to_string() }
    fn is_active(&self) -> bool { true }
    fn set_active(&mut self, _active: bool) { /* No-op for simple component */ }
    fn area(&self) -> Option<Rect> { None }
    fn set_area(&mut self, _area: Rect) { /* No-op for simple component */ }
    fn register_action_handler(&mut self, _tx: tokio::sync::mpsc::UnboundedSender<weavetui_core::event::Action>) { /* No-op for simple component */ }
    fn send(&self, _action: &str) { /* No-op for simple component */ }
    fn send_action(&self, _action: weavetui_core::event::Action) { /* No-op for simple component */ }
    fn as_active(self) -> Self { self }
    fn get_children(&mut self) -> &mut BTreeMap<String, Box<dyn Component>> {
        // For a unit struct without explicit children, return a mutable reference to a static empty map.
        static mut EMPTY_CHILDREN: BTreeMap<String, Box<dyn Component>> = BTreeMap::new();
        unsafe { &mut EMPTY_CHILDREN }
    }
}

impl Component for MyCustomComponent {
    fn draw(&mut self, f: &mut Frame<'_>, area: Rect) {
        let block = Block::default()
            .borders(Borders::ALL)
            .title("My Component");
        f.render_widget(Paragraph::new("Hello from Core!").block(block), area);
    }
}

🤝 Contributing

We welcome contributions to weavetui_core! Please refer to the main weavetui project's CONTRIBUTING.md for detailed guidelines on how to get involved, report issues, and submit pull requests.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.