Skip to main content

tui_dispatch/
lib.rs

1//! tui-dispatch: Centralized state management for Rust TUI apps
2//!
3//! Like Redux/Elm, but for terminals. Components are pure functions of state,
4//! and all state mutations happen through dispatched actions.
5//!
6//! # Quick Start
7//!
8//! ```
9//! use tui_dispatch::prelude::*;
10//!
11//! // Define your actions
12//! #[derive(Action, Clone, Debug)]
13//! enum MyAction {
14//!     Increment,
15//!     Decrement,
16//!     Quit,
17//! }
18//!
19//! // Define your state
20//! #[derive(Default)]
21//! struct MyState {
22//!     count: i32,
23//! }
24//!
25//! // Write a reducer
26//! fn reducer(state: &mut MyState, action: MyAction) -> bool {
27//!     match action {
28//!         MyAction::Increment => { state.count += 1; true }
29//!         MyAction::Decrement => { state.count -= 1; true }
30//!         MyAction::Quit => false,
31//!     }
32//! }
33//!
34//! // Create a store
35//! let mut store = Store::new(MyState::default(), reducer);
36//! store.dispatch(MyAction::Increment);
37//! assert_eq!(store.state().count, 1);
38//! ```
39//!
40//! # With Effects
41//!
42//! For async operations, use `EffectStore` with `ReducerResult`:
43//!
44//! ```
45//! use tui_dispatch::prelude::*;
46//!
47//! #[derive(Action, Clone, Debug)]
48//! enum Action {
49//!     Fetch,
50//!     DidLoad(String),
51//! }
52//!
53//! enum Effect {
54//!     FetchData,
55//! }
56//!
57//! #[derive(Default)]
58//! struct State {
59//!     data: Option<String>,
60//!     loading: bool,
61//! }
62//!
63//! fn reducer(state: &mut State, action: Action) -> ReducerResult<Effect> {
64//!     match action {
65//!         Action::Fetch => {
66//!             state.loading = true;
67//!             ReducerResult::changed_with(Effect::FetchData)
68//!         }
69//!         Action::DidLoad(data) => {
70//!             state.data = Some(data);
71//!             state.loading = false;
72//!             ReducerResult::changed()
73//!         }
74//!     }
75//! }
76//!
77//! let mut store = EffectStore::new(State::default(), reducer);
78//! let result = store.dispatch(Action::Fetch);
79//! assert!(result.changed);
80//! assert_eq!(result.effects.len(), 1);
81//! ```
82//!
83//! See the [documentation](https://docs.rs/tui-dispatch) for full guides.
84
85// Re-export everything from core
86pub use tui_dispatch_core::reducer_compose;
87pub use tui_dispatch_core::*;
88
89// Debug utilities
90pub use tui_dispatch_debug::debug;
91
92// Re-export derive macros
93pub use tui_dispatch_macros::{Action, BindingContext, ComponentId, DebugState, FeatureFlags};
94
95/// Prelude for convenient imports
96pub mod prelude {
97    // Traits
98    pub use tui_dispatch_core::{
99        Action, ActionCategory, ActionParams, BindingContext, Component, ComponentId,
100    };
101
102    // Event system
103    pub use tui_dispatch_core::{
104        process_raw_event, spawn_event_poller, DefaultBindingContext, Event, EventBus,
105        EventContext, EventHandler, EventKind, EventRoutingState, EventType, GlobalKeyPolicy,
106        HandlerResponse, NumericComponentId, RawEvent, RouteTarget, RoutedEvent, SimpleEventBus,
107    };
108
109    // Keybindings
110    pub use tui_dispatch_core::{format_key_for_display, parse_key_string, Keybindings};
111
112    // Store
113    pub use tui_dispatch_core::reducer_compose;
114    #[cfg(feature = "tracing")]
115    pub use tui_dispatch_core::LoggingMiddleware;
116    pub use tui_dispatch_core::{
117        ComposedMiddleware, Middleware, NoopMiddleware, Reducer, Store, StoreWithMiddleware,
118    };
119
120    // Effects and state types
121    pub use tui_dispatch_core::{
122        DataResource, EffectReducer, EffectStore, EffectStoreWithMiddleware, ReducerResult,
123    };
124
125    // Runtime helpers
126    pub use tui_dispatch_core::{
127        DispatchRuntime, DispatchStore, EffectContext, EffectRuntime, EffectStoreLike,
128        EventOutcome, PollerConfig, RenderContext,
129    };
130
131    // Tasks (requires "tasks" feature)
132    #[cfg(feature = "tasks")]
133    pub use tui_dispatch_core::{TaskKey, TaskManager, TaskPauseHandle};
134
135    // Subscriptions (requires "subscriptions" feature)
136    #[cfg(feature = "subscriptions")]
137    pub use tui_dispatch_core::{SubKey, SubPauseHandle, Subscriptions};
138
139    // Debug
140    pub use tui_dispatch_debug::debug::{
141        ActionLoggerConfig, ActionLoggerMiddleware, DebugFreeze, DebugOverlay, DebugTableBuilder,
142    };
143
144    // Derive macros
145    pub use tui_dispatch_macros::{Action, BindingContext, ComponentId, DebugState, FeatureFlags};
146
147    // Ratatui re-exports
148    pub use tui_dispatch_core::{Color, Frame, Line, Modifier, Rect, Span, Style, Text};
149}