Skip to main content

scrollbar_styled/
scrollbar_styled.rs

1//! Styled scrollbar showcase.
2//!
3//! Run with `cargo run -p tui-scrollbar --example scrollbar_styled`.
4//!
5//! This example renders a static layout with independently styled track, thumb, and arrow glyphs.
6
7use std::time::Duration;
8
9use color_eyre::Result;
10use ratatui::layout::{Margin, Rect};
11use ratatui::style::{Color, Modifier};
12use ratatui::widgets::{Block, Borders, Paragraph};
13use tui_scrollbar::{GlyphSet, ScrollBar, ScrollBarArrows, ScrollLengths};
14
15fn main() -> Result<()> {
16    color_eyre::install()?;
17    ratatui::run(|terminal| {
18        terminal.draw(|frame| render(frame.area(), frame))?;
19        std::thread::sleep(Duration::from_secs(3));
20        Ok(())
21    })
22}
23
24fn render(area: Rect, frame: &mut ratatui::Frame) {
25    if area.width < 2 || area.height < 2 {
26        return;
27    }
28
29    let horizontal_bar = area
30        .rows()
31        .next_back()
32        .unwrap_or(area)
33        .inner(Margin::new(1, 0));
34    let vertical_bar = area
35        .columns()
36        .next_back()
37        .unwrap_or(area)
38        .inner(Margin::new(0, 1));
39
40    let block = Block::new()
41        .borders(Borders::ALL)
42        .title("styled scrollbars")
43        .border_style((Color::LightBlue, Color::Black))
44        .style((Color::Gray, Color::Black));
45    let content = block.inner(area).inner(Margin::new(2, 1));
46    frame.render_widget(block, area);
47
48    frame.render_widget(
49        Paragraph::new("track_style, thumb_style, and arrow_style can each use distinct colors")
50            .style((Color::Gray, Color::Black)),
51        content,
52    );
53
54    let lengths = ScrollLengths {
55        content_len: 160,
56        viewport_len: 40,
57    };
58    let horizontal = ScrollBar::horizontal(lengths)
59        .offset(48)
60        .arrows(ScrollBarArrows::Both)
61        .glyph_set(GlyphSet::box_drawing())
62        .track_style((Color::Blue, Color::Black).into())
63        .thumb_style((Color::Yellow, Modifier::BOLD).into())
64        .arrow_style((Color::LightGreen, Color::Black, Modifier::BOLD).into());
65    let vertical = ScrollBar::vertical(lengths)
66        .offset(80)
67        .arrows(ScrollBarArrows::Both)
68        .glyph_set(GlyphSet::box_drawing())
69        .track_style((Color::Magenta, Color::Black).into())
70        .thumb_style((Color::Cyan, Modifier::BOLD).into())
71        .arrow_style((Color::LightRed, Color::Black, Modifier::BOLD).into());
72
73    frame.render_widget(&horizontal, horizontal_bar);
74    frame.render_widget(&vertical, vertical_bar);
75}