2023-06-17 19:25:43 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-26 11:33:39 +00:00
|
|
|
use ratatui::{prelude::*, symbols::scrollbar, widgets::*};
|
2023-06-17 19:25:43 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct App {
|
|
|
|
pub vertical_scroll_state: ScrollbarState,
|
|
|
|
pub horizontal_scroll_state: ScrollbarState,
|
|
|
|
pub vertical_scroll: usize,
|
|
|
|
pub horizontal_scroll: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
|
|
|
// create app and run it
|
|
|
|
let tick_rate = Duration::from_millis(250);
|
|
|
|
let app = App::default();
|
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
if let Err(err) = res {
|
|
|
|
println!("{err:?}");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
|
|
|
loop {
|
|
|
|
terminal.draw(|f| ui(f, &mut app))?;
|
|
|
|
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
2023-06-17 19:25:43 +00:00
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
2024-01-15 17:37:55 +00:00
|
|
|
KeyCode::Char('j') | KeyCode::Down => {
|
2023-06-17 19:25:43 +00:00
|
|
|
app.vertical_scroll = app.vertical_scroll.saturating_add(1);
|
2023-09-01 06:47:14 +00:00
|
|
|
app.vertical_scroll_state =
|
|
|
|
app.vertical_scroll_state.position(app.vertical_scroll);
|
2023-06-17 19:25:43 +00:00
|
|
|
}
|
2024-01-15 17:37:55 +00:00
|
|
|
KeyCode::Char('k') | KeyCode::Up => {
|
2023-06-17 19:25:43 +00:00
|
|
|
app.vertical_scroll = app.vertical_scroll.saturating_sub(1);
|
2023-09-01 06:47:14 +00:00
|
|
|
app.vertical_scroll_state =
|
|
|
|
app.vertical_scroll_state.position(app.vertical_scroll);
|
2023-06-17 19:25:43 +00:00
|
|
|
}
|
2024-01-15 17:37:55 +00:00
|
|
|
KeyCode::Char('h') | KeyCode::Left => {
|
2023-06-17 19:25:43 +00:00
|
|
|
app.horizontal_scroll = app.horizontal_scroll.saturating_sub(1);
|
2023-09-01 06:47:14 +00:00
|
|
|
app.horizontal_scroll_state =
|
|
|
|
app.horizontal_scroll_state.position(app.horizontal_scroll);
|
2023-06-17 19:25:43 +00:00
|
|
|
}
|
2024-01-15 17:37:55 +00:00
|
|
|
KeyCode::Char('l') | KeyCode::Right => {
|
2023-06-17 19:25:43 +00:00
|
|
|
app.horizontal_scroll = app.horizontal_scroll.saturating_add(1);
|
2023-09-01 06:47:14 +00:00
|
|
|
app.horizontal_scroll_state =
|
|
|
|
app.horizontal_scroll_state.position(app.horizontal_scroll);
|
2023-06-17 19:25:43 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(f: &mut Frame, app: &mut App) {
|
2023-06-17 19:25:43 +00:00
|
|
|
let size = f.size();
|
|
|
|
|
|
|
|
// Words made "loooong" to demonstrate line breaking.
|
|
|
|
let s = "Veeeeeeeeeeeeeeeery loooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiing. ";
|
|
|
|
let mut long_line = s.repeat(usize::from(size.width) / s.len() + 4);
|
|
|
|
long_line.push('\n');
|
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::vertical([
|
|
|
|
Constraint::Min(1),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
])
|
|
|
|
.split(size);
|
2023-06-17 19:25:43 +00:00
|
|
|
|
|
|
|
let text = vec![
|
|
|
|
Line::from("This is a line "),
|
2023-07-01 09:14:16 +00:00
|
|
|
Line::from("This is a line ".red()),
|
|
|
|
Line::from("This is a line".on_dark_gray()),
|
|
|
|
Line::from("This is a longer line".crossed_out()),
|
2023-09-10 00:05:36 +00:00
|
|
|
Line::from(long_line.clone()),
|
2023-07-01 09:14:16 +00:00
|
|
|
Line::from("This is a line".reset()),
|
2023-06-17 19:25:43 +00:00
|
|
|
Line::from(vec![
|
|
|
|
Span::raw("Masked text: "),
|
|
|
|
Span::styled(
|
|
|
|
Masked::new("password", '*'),
|
|
|
|
Style::default().fg(Color::Red),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
Line::from("This is a line "),
|
2023-07-01 09:14:16 +00:00
|
|
|
Line::from("This is a line ".red()),
|
|
|
|
Line::from("This is a line".on_dark_gray()),
|
|
|
|
Line::from("This is a longer line".crossed_out()),
|
2023-09-10 00:05:36 +00:00
|
|
|
Line::from(long_line.clone()),
|
2023-07-01 09:14:16 +00:00
|
|
|
Line::from("This is a line".reset()),
|
2023-06-17 19:25:43 +00:00
|
|
|
Line::from(vec![
|
|
|
|
Span::raw("Masked text: "),
|
|
|
|
Span::styled(
|
|
|
|
Masked::new("password", '*'),
|
|
|
|
Style::default().fg(Color::Red),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
];
|
2023-09-01 06:47:14 +00:00
|
|
|
app.vertical_scroll_state = app.vertical_scroll_state.content_length(text.len());
|
|
|
|
app.horizontal_scroll_state = app.horizontal_scroll_state.content_length(long_line.len());
|
2023-06-17 19:25:43 +00:00
|
|
|
|
2024-01-12 16:53:35 +00:00
|
|
|
let create_block = |title: &'static str| Block::bordered().gray().title(title.bold());
|
2023-06-17 19:25:43 +00:00
|
|
|
|
|
|
|
let title = Block::default()
|
2024-01-12 16:53:35 +00:00
|
|
|
.title("Use h j k l or ◄ ▲ ▼ ► to scroll ".bold())
|
2023-06-17 19:25:43 +00:00
|
|
|
.title_alignment(Alignment::Center);
|
|
|
|
f.render_widget(title, chunks[0]);
|
|
|
|
|
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-07-01 09:14:16 +00:00
|
|
|
.gray()
|
2023-06-17 19:25:43 +00:00
|
|
|
.block(create_block("Vertical scrollbar with arrows"))
|
|
|
|
.scroll((app.vertical_scroll as u16, 0));
|
|
|
|
f.render_widget(paragraph, chunks[1]);
|
|
|
|
f.render_stateful_widget(
|
|
|
|
Scrollbar::default()
|
|
|
|
.orientation(ScrollbarOrientation::VerticalRight)
|
|
|
|
.begin_symbol(Some("↑"))
|
|
|
|
.end_symbol(Some("↓")),
|
|
|
|
chunks[1],
|
|
|
|
&mut app.vertical_scroll_state,
|
|
|
|
);
|
|
|
|
|
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-07-01 09:14:16 +00:00
|
|
|
.gray()
|
2023-06-17 19:25:43 +00:00
|
|
|
.block(create_block(
|
2023-08-03 15:42:54 +00:00
|
|
|
"Vertical scrollbar without arrows, without track symbol and mirrored",
|
2023-06-17 19:25:43 +00:00
|
|
|
))
|
|
|
|
.scroll((app.vertical_scroll as u16, 0));
|
|
|
|
f.render_widget(paragraph, chunks[2]);
|
|
|
|
f.render_stateful_widget(
|
|
|
|
Scrollbar::default()
|
|
|
|
.orientation(ScrollbarOrientation::VerticalLeft)
|
|
|
|
.symbols(scrollbar::VERTICAL)
|
|
|
|
.begin_symbol(None)
|
2023-08-03 15:42:54 +00:00
|
|
|
.track_symbol(None)
|
2023-06-17 19:25:43 +00:00
|
|
|
.end_symbol(None),
|
|
|
|
chunks[2].inner(&Margin {
|
|
|
|
vertical: 1,
|
|
|
|
horizontal: 0,
|
|
|
|
}),
|
|
|
|
&mut app.vertical_scroll_state,
|
|
|
|
);
|
|
|
|
|
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-07-01 09:14:16 +00:00
|
|
|
.gray()
|
2023-06-17 19:25:43 +00:00
|
|
|
.block(create_block(
|
|
|
|
"Horizontal scrollbar with only begin arrow & custom thumb symbol",
|
|
|
|
))
|
|
|
|
.scroll((0, app.horizontal_scroll as u16));
|
|
|
|
f.render_widget(paragraph, chunks[3]);
|
|
|
|
f.render_stateful_widget(
|
|
|
|
Scrollbar::default()
|
|
|
|
.orientation(ScrollbarOrientation::HorizontalBottom)
|
|
|
|
.thumb_symbol("🬋")
|
|
|
|
.end_symbol(None),
|
|
|
|
chunks[3].inner(&Margin {
|
|
|
|
vertical: 0,
|
|
|
|
horizontal: 1,
|
|
|
|
}),
|
|
|
|
&mut app.horizontal_scroll_state,
|
|
|
|
);
|
|
|
|
|
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-07-01 09:14:16 +00:00
|
|
|
.gray()
|
2023-06-17 19:25:43 +00:00
|
|
|
.block(create_block(
|
|
|
|
"Horizontal scrollbar without arrows & custom thumb and track symbol",
|
|
|
|
))
|
|
|
|
.scroll((0, app.horizontal_scroll as u16));
|
|
|
|
f.render_widget(paragraph, chunks[4]);
|
|
|
|
f.render_stateful_widget(
|
|
|
|
Scrollbar::default()
|
|
|
|
.orientation(ScrollbarOrientation::HorizontalBottom)
|
|
|
|
.thumb_symbol("░")
|
2023-08-03 15:42:54 +00:00
|
|
|
.track_symbol(Some("─")),
|
2023-06-17 19:25:43 +00:00
|
|
|
chunks[4].inner(&Margin {
|
|
|
|
vertical: 0,
|
|
|
|
horizontal: 1,
|
|
|
|
}),
|
|
|
|
&mut app.horizontal_scroll_state,
|
|
|
|
);
|
|
|
|
}
|