Skip to content

Commit

Permalink
Handle mouse click events to focus a row.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Aug 20, 2021
1 parent 0ee52c8 commit 2a11873
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/jless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ impl JLess {
self.input_buffer.clear();

match me {
Press(Left, _, h) => {
print!("Clicked on line {}", h);
None
}
Press(Left, _, h) => Some(Action::Click(h)),
Press(WheelUp, _, _) => Some(Action::MoveUp(3)),
Press(WheelDown, _, _) => Some(Action::MoveDown(3)),
/* Ignore other mouse events. */
Expand Down
26 changes: 26 additions & 0 deletions src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub enum Action {
MoveFocusedLineToCenter,
MoveFocusedLineToBottom,

Click(u16),

ToggleCollapsed,
ToggleMode,

Expand Down Expand Up @@ -95,6 +97,7 @@ impl JsonViewer {
Action::MoveFocusedLineToTop => self.move_focused_line_to_top(),
Action::MoveFocusedLineToCenter => self.move_focused_line_to_center(),
Action::MoveFocusedLineToBottom => self.move_focused_line_to_bottom(),
Action::Click(n) => self.focus_clicked_row(n),
Action::ToggleCollapsed => self.toggle_collapsed(),
Action::ToggleMode => {
// TODO: custom window management here
Expand Down Expand Up @@ -130,6 +133,7 @@ impl JsonViewer {
Action::MoveFocusedLineToTop => false,
Action::MoveFocusedLineToCenter => false,
Action::MoveFocusedLineToBottom => false,
Action::Click(_) => true,
Action::ToggleMode => false,
Action::ResizeViewerDimensions(_) => true,
_ => false,
Expand Down Expand Up @@ -369,6 +373,10 @@ impl JsonViewer {
self.top_row = self.count_n_lines_before(self.focused_row, padding, self.mode);
}

fn focus_clicked_row(&mut self, row: u16) {
self.focused_row = self.count_n_lines_past(self.top_row, (row - 1) as usize, self.mode);
}

fn toggle_collapsed(&mut self) {
let focused_row = &mut self.flatjson[self.focused_row];
if focused_row.is_primitive() {
Expand Down Expand Up @@ -962,6 +970,24 @@ mod tests {
);
}

#[test]
fn test_focus_clicked_row() {
let fj = parse_top_level_json(OBJECT.to_owned()).unwrap();
let mut viewer = JsonViewer::new(fj, Mode::Line);
viewer.dimensions.height = 7;
viewer.scrolloff_setting = 3;

assert_window_tracking(
&mut viewer,
vec![
(Action::Click(6), 2, 5),
(Action::Click(1), 0, 2),
(Action::Click(3), 0, 2),
(Action::Click(5), 1, 4),
],
);
}

#[test]
fn test_focus_prev_next_sibling_line_mode() {
let fj = parse_top_level_json(OBJECT.to_owned()).unwrap();
Expand Down

0 comments on commit 2a11873

Please sign in to comment.