Skip to content

Commit 89ac0fe

Browse files
committed
docs: demo horizontal scrolling and mark TODO as done
1 parent 2994cce commit 89ac0fe

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

tui-scrollview/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ combination with any other widget.
8383
## TODO
8484

8585
- [ ] Conditionally show scrollbar
86-
- [ ] Implement horizontal scrolling and bar
86+
- [x] Implement horizontal scrolling and bar
8787

8888
## License
8989

tui-scrollview/examples/horizontal.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use std::io;
2+
3+
use color_eyre::Result;
4+
use ratatui::{
5+
crossterm::event::{self, Event, KeyCode, KeyEventKind},
6+
layout::Size,
7+
widgets::{Paragraph, Wrap},
8+
DefaultTerminal,
9+
};
10+
use tui_scrollview::{ScrollView, ScrollViewState};
11+
12+
fn main() -> Result<()> {
13+
color_eyre::install()?;
14+
let terminal = ratatui::init();
15+
let result = App::new().run(terminal);
16+
ratatui::restore();
17+
result
18+
}
19+
20+
#[derive(Debug, Default, Clone)]
21+
struct App {
22+
text: String,
23+
scroll_view_state: ScrollViewState,
24+
state: AppState,
25+
}
26+
27+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
28+
enum AppState {
29+
#[default]
30+
Running,
31+
Quit,
32+
}
33+
34+
impl App {
35+
fn new() -> App {
36+
App {
37+
text: lipsum::lipsum(10_000),
38+
..Default::default()
39+
}
40+
}
41+
42+
fn run(&mut self, mut terminal: DefaultTerminal) -> Result<()> {
43+
while self.is_running() {
44+
self.draw(&mut terminal)?;
45+
self.handle_events()?;
46+
}
47+
Ok(())
48+
}
49+
50+
fn is_running(&self) -> bool {
51+
self.state == AppState::Running
52+
}
53+
54+
fn quit(&mut self) {
55+
self.state = AppState::Quit;
56+
}
57+
58+
fn draw(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
59+
terminal.draw(|frame| {
60+
let area = frame.area();
61+
let size = Size::new(area.width * 2, area.width);
62+
let mut scroll_view = ScrollView::new(size);
63+
let paragraph = Paragraph::new(self.text.clone()).wrap(Wrap::default());
64+
scroll_view.render_widget(paragraph, scroll_view.area());
65+
frame.render_stateful_widget(scroll_view, area, &mut self.scroll_view_state);
66+
})?;
67+
Ok(())
68+
}
69+
70+
fn handle_events(&mut self) -> Result<()> {
71+
use KeyCode::*;
72+
match event::read()? {
73+
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
74+
Char('q') | Esc => self.quit(),
75+
Char('h') | Left => self.scroll_view_state.scroll_left(),
76+
Char('l') | Right => self.scroll_view_state.scroll_right(),
77+
_ => (),
78+
},
79+
_ => {}
80+
}
81+
Ok(())
82+
}
83+
}

0 commit comments

Comments
 (0)