Skip to content

Commit

Permalink
Handle scroll events
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrelf committed Dec 9, 2023
1 parent 5730f31 commit e206350
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crossterm::event::Event;
use crossterm::event::EventStream;
use crossterm::event::KeyCode;
use crossterm::event::KeyModifiers;
use crossterm::event::MouseEventKind;
use miette::miette;
use miette::IntoDiagnostic;
use miette::WrapErr;
Expand All @@ -15,6 +16,7 @@ use ratatui::prelude::Rect;
use ratatui::widgets::Paragraph;
use ratatui::widgets::Widget;
use ratatui::widgets::Wrap;
use std::cmp::min;
use tokio::io::AsyncReadExt;
use tokio::io::DuplexStream;
use tokio_stream::StreamExt;
Expand All @@ -23,6 +25,8 @@ use tokio_stream::StreamExt;
struct Tui {
quit: bool,
scrollback: Vec<u8>,
// TODO(evan): Follow output when scrolled to bottom
scroll_offset: usize,
}

/// TODO(evan): Document
Expand Down Expand Up @@ -102,15 +106,32 @@ fn render(tui: &Tui, area: Rect, buffer: &mut Buffer) -> miette::Result<()> {

let text = tui.scrollback.into_text().into_diagnostic()?;

let scroll_offset = u16::try_from(tui.scroll_offset).unwrap();

Paragraph::new(text)
.wrap(Wrap::default())
.scroll((scroll_offset, 0))
.render(area, buffer);

Ok(())
}

const SCROLL_AMOUNT: usize = 1;

fn handle_event(tui: &mut Tui, event: Event) -> miette::Result<()> {
match event {
// TODO(evan): Scrolling is excruciatingly slow
Event::Mouse(mouse) if mouse.kind == MouseEventKind::ScrollUp => {
tui.scroll_offset = tui.scroll_offset.saturating_sub(SCROLL_AMOUNT);
}
Event::Mouse(mouse) if mouse.kind == MouseEventKind::ScrollDown => {
let last_line = tui
.scrollback
.split(|byte| *byte == b'\n')
.count()
.saturating_sub(1);
tui.scroll_offset = min(last_line, tui.scroll_offset + SCROLL_AMOUNT);
}
Event::Key(key)
if key.modifiers == KeyModifiers::CONTROL && key.code == KeyCode::Char('c') =>
{
Expand Down

0 comments on commit e206350

Please sign in to comment.