tui_slider/lib.rs
1//! # tui-slider
2//!
3//! A simple TUI slider component library for ratatui.
4//!
5//! This library provides horizontal and vertical sliders for terminal user interfaces.
6//!
7//! ## Features
8//!
9//! - **Horizontal and Vertical sliders** - Support for both orientations
10//! - **Simple styling** - Customizable colors and symbols
11//! - **State management** - Built-in state for value tracking
12//! - **Easy to use** - Minimal configuration required
13//!
14//! ## Quick Start
15//!
16//! ```rust,no_run
17//! use ratatui::prelude::*;
18//! use tui_slider::{Slider, SliderState, SliderOrientation};
19//!
20//! // Create a slider state
21//! let mut state = SliderState::new(50.0, 0.0, 100.0);
22//!
23//! // Create a slider widget
24//! let slider = Slider::from_state(&state)
25//! .orientation(SliderOrientation::Horizontal)
26//! .label("Volume")
27//! .show_value(true);
28//!
29//! // Render it (in your terminal UI loop)
30//! // frame.render_widget(slider, area);
31//! ```
32//!
33//! ## Customization
34//!
35//! Customize the appearance with colors and symbols:
36//!
37//! ```rust
38//! use ratatui::style::Color;
39//! use tui_slider::{Slider, SliderState};
40//!
41//! let state = SliderState::new(50.0, 0.0, 100.0);
42//!
43//! let slider = Slider::from_state(&state)
44//! .filled_symbol("━")
45//! .empty_symbol("─")
46//! .handle_symbol("●")
47//! .filled_color(Color::Cyan)
48//! .empty_color(Color::DarkGray)
49//! .handle_color(Color::White);
50//! ```
51//!
52//! ## Vertical Sliders
53//!
54//! Create vertical sliders for equalizers and level meters:
55//!
56//! ```rust
57//! use tui_slider::{Slider, SliderState, SliderOrientation};
58//!
59//! let state = SliderState::new(60.0, 0.0, 100.0);
60//!
61//! let vertical_slider = Slider::from_state(&state)
62//! .orientation(SliderOrientation::Vertical)
63//! .label("Bass");
64//! ```
65
66pub mod border;
67pub mod orientation;
68pub mod position;
69pub mod slider;
70pub mod state;
71pub mod style;
72pub mod symbols;
73
74// Re-export main types
75pub use orientation::SliderOrientation;
76pub use position::{
77 HorizontalBarAlignment, VerticalLabelPosition, VerticalValueAlignment, VerticalValuePosition,
78};
79pub use slider::Slider;
80pub use state::SliderState;
81
82/// Prelude module for convenient imports
83pub mod prelude {
84 pub use crate::border;
85 pub use crate::orientation::SliderOrientation;
86 pub use crate::position::{
87 VerticalLabelPosition, VerticalValueAlignment, VerticalValuePosition,
88 };
89 pub use crate::slider::Slider;
90 pub use crate::state::SliderState;
91 pub use crate::style;
92 pub use crate::symbols;
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_slider_state() {
101 let mut state = SliderState::new(50.0, 0.0, 100.0);
102 assert_eq!(state.value(), 50.0);
103
104 state.set_value(75.0);
105 assert_eq!(state.value(), 75.0);
106
107 state.set_value(150.0); // Should clamp to max
108 assert_eq!(state.value(), 100.0);
109
110 state.set_value(-10.0); // Should clamp to min
111 assert_eq!(state.value(), 0.0);
112 }
113
114 #[test]
115 fn test_slider_percentage() {
116 let state = SliderState::new(50.0, 0.0, 100.0);
117 assert_eq!(state.percentage(), 0.5);
118
119 let state = SliderState::new(25.0, 0.0, 100.0);
120 assert_eq!(state.percentage(), 0.25);
121 }
122}