Skip to main content

scrollbar_styled/
scrollbar_styled.rs

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