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::{VerticalLabelPosition, VerticalValueAlignment, VerticalValuePosition};
77pub use slider::Slider;
78pub use state::SliderState;
79
80/// Prelude module for convenient imports
81pub mod prelude {
82    pub use crate::border;
83    pub use crate::orientation::SliderOrientation;
84    pub use crate::position::{
85        VerticalLabelPosition, VerticalValueAlignment, VerticalValuePosition,
86    };
87    pub use crate::slider::Slider;
88    pub use crate::state::SliderState;
89    pub use crate::style;
90    pub use crate::symbols;
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_slider_state() {
99        let mut state = SliderState::new(50.0, 0.0, 100.0);
100        assert_eq!(state.value(), 50.0);
101
102        state.set_value(75.0);
103        assert_eq!(state.value(), 75.0);
104
105        state.set_value(150.0); // Should clamp to max
106        assert_eq!(state.value(), 100.0);
107
108        state.set_value(-10.0); // Should clamp to min
109        assert_eq!(state.value(), 0.0);
110    }
111
112    #[test]
113    fn test_slider_percentage() {
114        let state = SliderState::new(50.0, 0.0, 100.0);
115        assert_eq!(state.percentage(), 0.5);
116
117        let state = SliderState::new(25.0, 0.0, 100.0);
118        assert_eq!(state.percentage(), 0.25);
119    }
120}