Skip to content

Commit

Permalink
Implement zt, zz and zb for moving focus to the top/center/bottom of …
Browse files Browse the repository at this point in the history
…the screen.
  • Loading branch information
PaulJuliusMartinez committed Aug 20, 2021
1 parent 2d11dee commit 1910b36
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/jless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ impl JLess {
// These may interpret the input buffer some other way
Key::Char('t') => {
if self.input_buffer == "z".as_bytes() {
// Action::MoveFocusedLineToTop
None
Some(Action::MoveFocusedLineToTop)
} else {
None
}
}
Key::Char('b') => {
if self.input_buffer == "z".as_bytes() {
// Action::MoveFocusedLineToBottom
None
Some(Action::MoveFocusedLineToBottom)
} else {
// Action::MoveUpAtDepth
None
Expand Down Expand Up @@ -166,9 +164,7 @@ impl JLess {
fn handle_z_input(&mut self) -> Option<Action> {
if self.input_buffer == "z".as_bytes() {
self.input_buffer.clear();

// Action::MoveFocusedLineToCenter()
None
Some(Action::MoveFocusedLineToCenter)
} else {
self.input_buffer.clear();
self.buffer_input('z' as u8);
Expand Down
92 changes: 91 additions & 1 deletion src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ pub enum Action {
ScrollUp(usize),
ScrollDown(usize),

MoveFocusedLineToTop,
MoveFocusedLineToCenter,
MoveFocusedLineToBottom,

ToggleCollapsed,
ToggleMode,

Expand All @@ -88,6 +92,9 @@ impl JsonViewer {
Action::FocusMatchingPair => self.focus_matching_pair(),
Action::ScrollUp(n) => self.scroll_up(n),
Action::ScrollDown(n) => self.scroll_down(n),
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::ToggleCollapsed => self.toggle_collapsed(),
Action::ToggleMode => {
// TODO: custom window management here
Expand Down Expand Up @@ -120,6 +127,9 @@ impl JsonViewer {
Action::FocusMatchingPair => true,
Action::ScrollUp(_) => false,
Action::ScrollDown(_) => false,
Action::MoveFocusedLineToTop => false,
Action::MoveFocusedLineToCenter => false,
Action::MoveFocusedLineToBottom => false,
Action::ToggleMode => false,
Action::ResizeViewerDimensions(_) => true,
_ => false,
Expand All @@ -132,6 +142,9 @@ impl JsonViewer {
Action::FocusNextSibling => false,
Action::ScrollUp(_) => false,
Action::ScrollDown(_) => false,
Action::MoveFocusedLineToTop => false,
Action::MoveFocusedLineToCenter => false,
Action::MoveFocusedLineToBottom => false,
Action::ToggleMode => false,
Action::ResizeViewerDimensions(_) => false,
_ => true,
Expand Down Expand Up @@ -341,6 +354,21 @@ impl JsonViewer {
}
}

fn move_focused_line_to_top(&mut self) {
let padding = self.scrolloff() as usize;
self.top_row = self.count_n_lines_before(self.focused_row, padding, self.mode);
}

fn move_focused_line_to_center(&mut self) {
let padding = (self.dimensions.height / 2) as usize;
self.top_row = self.count_n_lines_before(self.focused_row, padding, self.mode);
}

fn move_focused_line_to_bottom(&mut self) {
let padding = (self.dimensions.height - self.scrolloff() - 1) as usize;
self.top_row = self.count_n_lines_before(self.focused_row, padding, 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 @@ -467,7 +495,16 @@ impl JsonViewer {
#[cfg(test)]
mod tests {
use super::*;
use crate::flatjson::parse_top_level_json;
use crate::flatjson::{parse_top_level_json, NIL};

impl OptionIndex {
pub fn to_usize(&self) -> usize {
match self {
OptionIndex::Nil => NIL,
OptionIndex::Index(i) => *i,
}
}
}

const OBJECT: &'static str = r#"{
"1": 1,
Expand Down Expand Up @@ -872,6 +909,59 @@ mod tests {
);
}

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

assert_window_tracking(
&mut viewer,
vec![
(Action::MoveFocusedLineToTop, 0, 0),
(Action::MoveFocusedLineToCenter, 0, 0),
(Action::MoveFocusedLineToBottom, 0, 0),
],
);

viewer.top_row = 10;
viewer.focused_row = 12;

assert_window_tracking(
&mut viewer,
vec![
(Action::MoveFocusedLineToTop, 11, 12),
(Action::MoveFocusedLineToCenter, 10, 12),
(Action::MoveFocusedLineToBottom, 9, 12),
],
);

viewer.top_row = 4;
viewer.focused_row = 6;
viewer.dimensions.height = 7;

assert_window_tracking(
&mut viewer,
vec![
(Action::MoveFocusedLineToTop, 5, 6),
(Action::MoveFocusedLineToCenter, 3, 6),
(Action::MoveFocusedLineToBottom, 1, 6),
],
);

viewer.dimensions.height = 8;

assert_window_tracking(
&mut viewer,
vec![
(Action::MoveFocusedLineToTop, 5, 6),
(Action::MoveFocusedLineToCenter, 2, 6),
(Action::MoveFocusedLineToBottom, 0, 6),
],
);
}

#[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 1910b36

Please sign in to comment.