Skip to content

Commit

Permalink
Horizontally scroll long values when jumping to next search match, ev…
Browse files Browse the repository at this point in the history
…en if the row wasn't on the screen before.
  • Loading branch information
PaulJuliusMartinez committed Jan 30, 2022
1 parent 1e4ec96 commit 606bb96
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/flatjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ impl Row {
pub fn is_container(&self) -> bool {
self.value.is_container()
}
pub fn is_string(&self) -> bool {
self.value.is_string()
}
pub fn is_opening_of_container(&self) -> bool {
self.value.is_opening_of_container()
}
Expand Down Expand Up @@ -320,6 +323,13 @@ impl Value {
}
}

pub fn is_string(&self) -> bool {
match self {
Value::String => true,
_ => false,
}
}

pub fn container_type(&self) -> Option<ContainerType> {
match self {
Value::OpenContainer { container_type, .. } => Some(*container_type),
Expand Down
30 changes: 29 additions & 1 deletion src/lineprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,35 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
.and_modify(|tsv| {
*tsv = tsv.resize(value_ref, available_space);
})
.or_insert_with(|| TruncatedStrView::init_start(value_ref, available_space))
.or_insert_with(|| {
let tsv = TruncatedStrView::init_start(value_ref, available_space);

let mut range = self.value_range.clone();
if quoted {
range.start += 1;
range.end -= 1;
}

// If we're showing a line for the first time, we might
// need to focus on a search match.
let no_overlap = self.focused_search_match.end <= range.start
|| range.end <= self.focused_search_match.start;
if no_overlap {
return tsv;
}

let value_range_start = range.start;
let offset_focused_range = Range {
start: self
.focused_search_match
.start
.saturating_sub(value_range_start),
end: (self.focused_search_match.end - value_range_start)
.min(value_ref.len()),
};

tsv.focus(value_ref, &offset_focused_range)
})
.clone()
})
.unwrap_or_else(|| TruncatedStrView::init_start(value_ref, available_space));
Expand Down
12 changes: 9 additions & 3 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,19 @@ impl ScreenWriter {
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;
let mut range = json_row.range.clone();
if json_row.is_string() {
range.start += 1;
range.end -= 1;
}

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

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

0 comments on commit 606bb96

Please sign in to comment.