Skip to content

Commit

Permalink
Horizontally scroll long values when jumping to next search match.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Jan 30, 2022
1 parent c094dfb commit 1e4ec96
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 40 deletions.
22 changes: 14 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl App {
// of the focused row changes.
let mut jumped_to_search_match = false;
let focused_row_before = self.viewer.focused_row;
let focused_row_collapsed_state_before =
let previous_collapsed_state_of_focused_row =
self.viewer.flatjson[focused_row_before].is_collapsed();

let event = event.unwrap();
Expand Down Expand Up @@ -271,13 +271,19 @@ impl App {
self.viewer.perform_action(action);
}

// Check whether we're still actively searching
if !jumped_to_search_match
&& (focused_row_before != self.viewer.focused_row
|| focused_row_collapsed_state_before
!= self.viewer.flatjson[focused_row_before].is_collapsed())
{
self.search_state.set_no_longer_actively_searching();
if jumped_to_search_match {
self.screen_writer.scroll_line_to_search_match(
&self.viewer,
self.search_state.current_match_range(),
);
} else {
// Check whether we're still actively searching
if focused_row_before != self.viewer.focused_row
|| previous_collapsed_state_of_focused_row
!= self.viewer.flatjson[focused_row_before].is_collapsed()
{
self.search_state.set_no_longer_actively_searching();
}
}

self.screen_writer
Expand Down
54 changes: 46 additions & 8 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,43 +553,81 @@ impl ScreenWriter {
let row = viewer.focused_row;
let tsv = self.truncated_row_value_views.get(&row);
if let Some(tsv) = tsv {
if tsv.range.is_none() {
return;
}

// Make tsv not a reference.
let mut tsv = *tsv;
let value_ref = self
.line_primitive_value_ref(&viewer.flatjson[row], &viewer)
.unwrap();
if tsv.range.is_none() {
return;
}
if to_right {
tsv = tsv.scroll_right(value_ref, count);
} else {
tsv = tsv.scroll_left(value_ref, count);
}
self.truncated_row_value_views
.insert(viewer.focused_row, tsv);
} else {
return;
}
}

pub fn scroll_focused_line_to_an_end(&mut self, viewer: &JsonViewer) {
let row = viewer.focused_row;
let tsv = self.truncated_row_value_views.get(&row);
if let Some(tsv) = tsv {
if tsv.range.is_none() {
return;
}

// Make tsv not a reference.
let mut tsv = *tsv;
let value_ref = self
.line_primitive_value_ref(&viewer.flatjson[row], &viewer)
.unwrap();
tsv = tsv.jump_to_an_end(value_ref);
self.truncated_row_value_views
.insert(viewer.focused_row, tsv);
}
}

pub fn scroll_line_to_search_match(
&mut self,
viewer: &JsonViewer,
focused_search_range: Range<usize>,
) {
let row = viewer.focused_row;
let tsv = self.truncated_row_value_views.get(&row);
if let Some(tsv) = tsv {
// Make tsv not a reference.
let mut tsv = *tsv;
if tsv.range.is_none() {
return;
}
tsv = tsv.jump_to_an_end(value_ref);

let json_row = &viewer.flatjson[row];
let value_ref = self.line_primitive_value_ref(json_row, &viewer).unwrap();

let no_overlap = focused_search_range.end <= json_row.range.start
|| json_row.range.end <= focused_search_range.start;
if no_overlap {
return;
}

let mut value_range_start = json_row.range.start;
if let Value::String = &json_row.value {
value_range_start += 1;
}

let offset_focused_range = Range {
start: focused_search_range.start.saturating_sub(value_range_start),
end: focused_search_range.end - value_range_start,
};

tsv = tsv.focus(value_ref, &offset_focused_range);

self.truncated_row_value_views
.insert(viewer.focused_row, tsv);
} else {
return;
}
}
}
Expand Down
Loading

0 comments on commit 1e4ec96

Please sign in to comment.