-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scrollbar showcase example (#798)
Signed-off-by: n4n5 <[email protected]> Co-authored-by: Josh McKinney <[email protected]>
- Loading branch information
1 parent
ed03381
commit e7e468a
Showing
10 changed files
with
148 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.