Skip to content

Commit

Permalink
Add mouse support!
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Aug 20, 2021
1 parent 8be5cfd commit 0ee52c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/jless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::io::Write;

use rustyline::Editor;
use termion::event::Key;
use termion::event::MouseButton::{Left, WheelDown, WheelUp};
use termion::event::MouseEvent::Press;

use crate::flatjson;
use crate::input::TuiEvent;
use crate::input::TuiEvent::{KeyEvent, WinChEvent};
use crate::input::TuiEvent::{KeyEvent, MouseEvent, WinChEvent};
use crate::screenwriter::{AnsiTTYWriter, ScreenWriter};
use crate::types::TTYDimensions;
use crate::viewer::{Action, JsonViewer, Mode};
Expand Down Expand Up @@ -131,6 +133,20 @@ impl JLess {

action
}
MouseEvent(me) => {
self.input_buffer.clear();

match me {
Press(Left, _, h) => {
print!("Clicked on line {}", h);
None
}
Press(WheelUp, _, _) => Some(Action::MoveUp(3)),
Press(WheelDown, _, _) => Some(Action::MoveDown(3)),
/* Ignore other mouse events. */
_ => None,
}
}
WinChEvent => {
let dimensions = TTYDimensions::from_size(termion::terminal_size().unwrap());
self.screen_writer.dimensions = dimensions;
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::Read;
use std::path::PathBuf;
use structopt::StructOpt;
use termion::cursor::HideCursor;
use termion::input::MouseTerminal;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;

Expand Down Expand Up @@ -36,7 +37,9 @@ fn main() {
}
};

let stdout = HideCursor::from(AlternateScreen::from(io::stdout().into_raw_mode().unwrap()));
let stdout = MouseTerminal::from(HideCursor::from(AlternateScreen::from(
io::stdout().into_raw_mode().unwrap(),
)));
let mut app = match jless::new(json_string, Box::new(stdout)) {
Ok(jl) => jl,
Err(err) => {
Expand Down

0 comments on commit 0ee52c8

Please sign in to comment.