Skip to content

Commit

Permalink
Add scrollbar showcase example (#798)
Browse files Browse the repository at this point in the history
Signed-off-by: n4n5 <[email protected]>
Co-authored-by: Josh McKinney <[email protected]>
  • Loading branch information
Its-Just-Nans and joshka authored Jan 6, 2025
1 parent ed03381 commit e7e468a
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 16 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions code/showcase/widget-showcase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ clap = { version = "4.5.23", features = ["derive"] }
color-eyre = "0.6.3"
ratatui = { version = "0.29.0", features = ["all-widgets"] }
time = "0.3.37"
lipsum = "0.9.1"
textwrap = "0.16.1"
2 changes: 1 addition & 1 deletion code/showcase/widget-showcase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To add each widget:
- Create a VHS tape (copy from the existing tapes) and edit the output and screenshot
- Run `~/go/bin/vhs <tapefile>.tape` (Install VHS from main to get the Aardvark Blue theme and the
Screenshot command).
- Add the image to the [widgets showcase](../../src/content/docs/showcase/widgets/index.mdx)
- Add the image to the [widgets showcase](../../../src/content/docs/showcase/widgets.mdx)

## Design guidelines

Expand Down
9 changes: 9 additions & 0 deletions code/showcase/widget-showcase/scrollbar.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A VHS tape. See https://github.com/charmbracelet/vhs
Output "../../../src/content/docs/showcase/widgets/scrollbar.gif"
Set Theme "Aardvark Blue"
Set Width 800
Set Height 450
Type "cargo run -- -w scrollbar" Enter
Sleep 2s
Screenshot "../../../src/content/docs/showcase/widgets/scrollbar.png"
Sleep 1s
1 change: 1 addition & 0 deletions code/showcase/widget-showcase/src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod gauge;
pub mod line_gauge;
pub mod list;
pub mod paragraph;
pub mod scrollbar;
pub mod sparkline;
pub mod table;
pub mod tabs;
85 changes: 85 additions & 0 deletions code/showcase/widget-showcase/src/examples/scrollbar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use lipsum::lipsum;
use ratatui::{
layout::{Margin, Rect},
style::{Color, Stylize},
text::Text,
widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState},
Frame,
};

pub fn render(frame: &mut Frame) {
let word_count = frame.area().width * frame.area().height / 3;
let text = lipsum(word_count as usize);

let Rect {
x,
y,
width,
height,
} = frame.area();
let main_area = Rect::new(x, y, width - 4, height - 2);

// wrap (using textwrap) a bit wider than the frame to make sure we have a scrollbar
let wrapped = textwrap::wrap(&text, (width as f64 * 1.5) as usize);
let text = Text::from_iter(wrapped).dim();

let block = Block::bordered().title("Scrollbar").borders(Borders::ALL);

let text_height = text.height();
let text_width = text.width();
let vertical_scroll = text_height as f64 / 3.0;
let horizontal_scroll = text_width as f64 / 3.0;

let para = Paragraph::new(text)
.block(block)
.scroll((vertical_scroll as u16, horizontal_scroll as u16));
frame.render_widget(para, main_area);

let scrollbar = Scrollbar::new(ScrollbarOrientation::HorizontalBottom);
let mut scrollbar_state = ScrollbarState::new(text_width).position(horizontal_scroll as usize);
let area = main_area.inner(Margin::new(1, 0));
frame.render_stateful_widget(scrollbar, area, &mut scrollbar_state);

let scrollbar = Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("▩")
.end_style(Color::Blue)
.begin_style(Color::Blue)
.thumb_style(Color::Yellow)
.track_style(Color::Green);
let mut scrollbar_state = ScrollbarState::new(text_width).position(0);
let area = Rect::new(x + 1, y, width - 6, height - 1);
frame.render_stateful_widget(scrollbar, area, &mut scrollbar_state);

let scrollbar = Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("▣")
.track_symbol(Some("-"))
.begin_symbol(Some("⭅"))
.end_symbol(Some("⭆"));
let mut scrollbar_state = ScrollbarState::new(text_width).position(text_width as usize);
let area = Rect::new(x + 1, y, width - 6, height);
frame.render_stateful_widget(scrollbar, area, &mut scrollbar_state);

let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight);
let mut scrollbar_state = ScrollbarState::new(text_height).position(vertical_scroll as usize);
let area = main_area.inner(Margin::new(0, 1));
frame.render_stateful_widget(scrollbar, area, &mut scrollbar_state);

let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
.thumb_symbol("▯")
.end_style(Color::Red)
.begin_style(Color::Red)
.thumb_style(Color::Cyan)
.track_style(Color::Magenta);
let mut scrollbar_state = ScrollbarState::new(text_height).position(0);
let area = Rect::new(x, y + 1, width - 2, height-4);
frame.render_stateful_widget(scrollbar, area, &mut scrollbar_state);

let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
.thumb_symbol("▒")
.track_symbol(Some("|"))
.begin_symbol(Some("↑"))
.end_symbol(Some("↓"));
let mut scrollbar_state = ScrollbarState::new(text_height).position(text_height as usize);
let area = Rect::new(x, y + 1, width, height - 4);
frame.render_stateful_widget(scrollbar, area, &mut scrollbar_state);
}
8 changes: 5 additions & 3 deletions code/showcase/widget-showcase/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mod examples;
mod tui;

use examples::{
bar_chart, block, calendar, canvas, chart, gauge, line_gauge, list, paragraph, sparkline,
table, tabs,
bar_chart, block, calendar, canvas, chart, gauge, line_gauge, list, paragraph, scrollbar,
sparkline, table, tabs,
};

#[derive(Debug, Parser)]
Expand All @@ -38,6 +38,7 @@ enum Widget {
LineGauge,
List,
Paragraph,
Scrollbar,
Sparkline,
Table,
Tabs,
Expand Down Expand Up @@ -103,13 +104,14 @@ impl App {
match widget {
Widget::Block => block::render(frame),
Widget::BarChart => bar_chart::render(frame),
Widget::Calendar => crate::calendar::render(frame)?,
Widget::Calendar => calendar::render(frame)?,
Widget::Canvas => canvas::render(frame),
Widget::Chart => chart::render(frame),
Widget::Gauge => gauge::render(frame),
Widget::LineGauge => line_gauge::render(frame),
Widget::List => list::render(frame),
Widget::Paragraph => paragraph::render(frame),
Widget::Scrollbar => scrollbar::render(frame),
Widget::Sparkline => sparkline::render(frame),
Widget::Table => table::render(frame),
Widget::Tabs => tabs::render(frame),
Expand Down
26 changes: 14 additions & 12 deletions src/content/docs/showcase/widgets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,55 @@ https://github.com/ratatui/ratatui-website/issues/249 for information about how
[widget-showcase]: https://github.com/ratatui/ratatui-website/blob/main/code/widget-showcase
[VHS]: https://github.com/charmbracelet/vhs

## Block <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/block/struct.Block.html" text="Docs" />
## Block <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/block/struct.Block.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/block/" text="Example Code" />

![Block](./widgets/block.png)

## BarChart <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.BarChart.html" text="Docs" />
## BarChart <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.BarChart.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/barchart/" text="Example Code" />

![BarChart](./widgets/bar_chart.png)

## Calendar <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/calendar/struct.Monthly.html" text="Docs" />
## Calendar <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/calendar/struct.Monthly.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/calendar/" text="Example Code" />

![Calendar](./widgets/calendar.png)

## Canvas <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/canvas/struct.Canvas.html" text="Docs" />
## Canvas <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/canvas/struct.Canvas.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/canvas/" text="Example Code" />

![Canvas](./widgets/canvas.png)

## Chart <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Chart.html" text="Docs" />
## Chart <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Chart.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/chart/" text="Example Code" />

![BarChart](./widgets/chart.png)

## Gauge <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Gauge.html" text="Docs" />
## Gauge <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Gauge.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/gauge/" text="Example Code" />

![Gauge](./widgets/gauge.png)

## LineGauge <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.LineGauge.html" text="Docs" />

![LineGauge](./widgets/line_gauge.png)

## List <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html" text="Docs" />
## List <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/list/" text="Example Code" />

![List](./widgets/list.png)

## Paragraph <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Paragraph.html" text="Docs" />
## Paragraph <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Paragraph.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/paragraph/" text="Example Code" />

![Paragraph](./widgets/paragraph.png)

## Scrollbar <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Scrollbar.html" text="Docs" /> <LinkBadge text="Help Wanted" href="https://github.com/ratatui/ratatui-website/issues/249" variant="caution" />
## Scrollbar <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Scrollbar.html" text="Docs" /> <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Scrollbar.html" text="Example Code" />

## Sparkline <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Sparkline.html" text="Docs" />
![Scrollbar](./widgets/scrollbar.png)

## Sparkline <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Sparkline.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/sparkline/" text="Example Code" />

![Sparkline](./widgets/sparkline.png)

## Table <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Table.html" text="Docs" />
## Table <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Table.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/table/" text="Example Code" />

![Table](./widgets/table.png)

## Tabs <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Tabs.html" text="Docs" />
## Tabs <LinkBadge href="https://docs.rs/ratatui/latest/ratatui/widgets/struct.Tabs.html" text="Docs" /> <LinkBadge href="https://ratatui.rs/examples/widgets/tabs/" text="Example Code" />

![Tabs](./widgets/tabs.png)

Expand Down
3 changes: 3 additions & 0 deletions src/content/docs/showcase/widgets/scrollbar.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/content/docs/showcase/widgets/scrollbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e7e468a

Please sign in to comment.