Skip to content

Commit

Permalink
Highlight matches in delimiters around labels and values, ': ' separa…
Browse files Browse the repository at this point in the history
…tor after object keys, and in trailing commas.
  • Loading branch information
PaulJuliusMartinez committed Jan 29, 2022
1 parent 05d3c26 commit 6fe0c11
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 41 deletions.
23 changes: 19 additions & 4 deletions src/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub fn highlight_truncated_str_view<'a, W: Write, TUI: TUIControl>(
out: &mut ColorPrinter<TUI, W>,
mut s: &str,
str_view: &TruncatedStrView,
mut start_index: usize,
mut str_range_start: Option<usize>,
style: &PrintStyle,
highlight_style: &PrintStyle,
matches_iter: &mut Option<&mut Peekable<MatchRangeIter<'a>>>,
Expand All @@ -207,7 +207,7 @@ pub fn highlight_truncated_str_view<'a, W: Write, TUI: TUIControl>(
replacement_character = tr.showing_replacement_character;
trailing_ellipsis = tr.print_trailing_ellipsis(s);
s = &s[tr.start..tr.end];
start_index += tr.start;
str_range_start = str_range_start.map(|start| start + tr.start);
}

if leading_ellipsis {
Expand All @@ -225,7 +225,14 @@ pub fn highlight_truncated_str_view<'a, W: Write, TUI: TUIControl>(
}

// Print actual string itself
highlight_matches(out, s, start_index, style, highlight_style, matches_iter)?;
highlight_matches(
out,
s,
str_range_start,
style,
highlight_style,
matches_iter,
)?;

// Print trailing ellipsis
if trailing_ellipsis {
Expand All @@ -239,11 +246,19 @@ pub fn highlight_truncated_str_view<'a, W: Write, TUI: TUIControl>(
pub fn highlight_matches<'a, W: Write, TUI: TUIControl>(
out: &mut ColorPrinter<TUI, W>,
mut s: &str,
mut start_index: usize,
str_range_start: Option<usize>,
style: &PrintStyle,
highlight_style: &PrintStyle,
matches_iter: &mut Option<&mut Peekable<MatchRangeIter<'a>>>,
) -> fmt::Result {
if str_range_start.is_none() {
out.set_style(style)?;
write!(out.buf, "{}", s)?;
return Ok(());
}

let mut start_index = str_range_start.unwrap();

while !s.is_empty() {
// Initialize the next match to be a fake match past the end of the string.
let string_end = start_index + s.len();
Expand Down
120 changes: 87 additions & 33 deletions src/lineprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ pub struct LinePrinter<'a, 'b, TUI: TUIControl> {

// Stuff to actually print out
pub label: Option<LineLabel<'a>>,
pub label_start_index: usize,
pub label_range: &'a Option<Range<usize>>,
pub value: LineValue<'a>,
pub value_start_index: usize,
pub value_range: &'a Range<usize>,

pub search_matches: Option<&'a mut Peekable<MatchRangeIter<'b>>>,

Expand Down Expand Up @@ -308,8 +308,8 @@ impl<'a, 'b, TUI: TUIControl> LinePrinter<'a, 'b, TUI> {

let mut used_space = 0;

let style: PrintStyle;
let highlighted_style: PrintStyle;
let style: &PrintStyle;
let highlighted_style: &PrintStyle;
let mut dummy_search_matches = None;
let matches_iter;

Expand All @@ -327,11 +327,11 @@ impl<'a, 'b, TUI: TUIControl> LinePrinter<'a, 'b, TUI> {
label_ref = key;

if self.focused {
style = highlighting::INVERTED_BOLD_BLUE_STYLE;
highlighted_style = highlighting::BOLD_INVERTED_STYLE;
style = &highlighting::INVERTED_BOLD_BLUE_STYLE;
highlighted_style = &highlighting::BOLD_INVERTED_STYLE;
} else {
style = highlighting::BLUE_STYLE;
highlighted_style = highlighting::SEARCH_MATCH_HIGHLIGHTED;
style = &highlighting::BLUE_STYLE;
highlighted_style = &highlighting::SEARCH_MATCH_HIGHLIGHTED;
}
matches_iter = &mut self.search_matches;
}
Expand All @@ -340,14 +340,14 @@ impl<'a, 'b, TUI: TUIControl> LinePrinter<'a, 'b, TUI> {
label_ref = index;

if self.focused {
style = highlighting::BOLD_STYLE;
style = &highlighting::BOLD_STYLE;
} else {
style = highlighting::GRAY_STYLE;
style = &highlighting::GRAY_STYLE;
}

// No match highlighting for index labels.
matches_iter = &mut dummy_search_matches;
highlighted_style = highlighting::DEFAULT_STYLE;
highlighted_style = &highlighting::DEFAULT_STYLE;
}
}

Expand All @@ -372,26 +372,58 @@ impl<'a, 'b, TUI: TUIControl> LinePrinter<'a, 'b, TUI> {

let mut out = ColorPrinter::new(self.tui, buf);

// Print out start of label;
out.set_style(&style)?;
write!(out.buf, "{}", label_style.left())?;
let mut label_open_delimiter_range_start = None;
let mut label_range_start = None;
let mut label_close_delimiter_range_start = None;
let mut object_separator_range_start = None;

if let Some(range) = self.label_range {
label_open_delimiter_range_start = Some(range.start);
label_range_start = Some(range.start + 1);
label_close_delimiter_range_start = Some(range.end - 1);
object_separator_range_start = Some(range.end);
}

// Print out start of label
highlighting::highlight_matches(
&mut out,
label_style.left(),
label_open_delimiter_range_start,
style,
highlighted_style,
matches_iter,
)?;

// Print out the label itself
highlighting::highlight_truncated_str_view(
&mut out,
label_ref,
&truncated_view,
self.label_start_index,
&style,
&highlighted_style,
label_range_start,
style,
highlighted_style,
matches_iter,
)?;

// Print out end of label;
out.set_style(&style)?;
write!(out.buf, "{}", label_style.right())?;
// Print out end of label
highlighting::highlight_matches(
&mut out,
label_style.right(),
label_close_delimiter_range_start,
style,
highlighted_style,
matches_iter,
)?;

self.tui.reset_style(buf)?;
write!(buf, ": ")?;
// Print out separator between label and value
highlighting::highlight_matches(
&mut out,
": ",
object_separator_range_start,
&highlighting::DEFAULT_STYLE,
&highlighting::SEARCH_MATCH_HIGHLIGHTED,
matches_iter,
)?;

used_space += label_style.width();
used_space += 2;
Expand Down Expand Up @@ -474,34 +506,56 @@ impl<'a, 'b, TUI: TUIControl> LinePrinter<'a, 'b, TUI> {
..PrintStyle::default()
};

let value_open_quote_range_start = self.value_range.start;
let mut value_range_start = self.value_range.start;
let value_close_quote_range_start = self.value_range.end - 1;
let trailing_comma_range_start = self.value_range.end;

if quoted {
out.set_style(&style)?;
out.buf.write_char('"')?;
used_space += 1;
value_range_start += 1;
highlighting::highlight_matches(
&mut out,
"\"",
Some(value_open_quote_range_start),
&style,
&highlighting::SEARCH_MATCH_HIGHLIGHTED,
&mut self.search_matches,
)?;
}

highlighting::highlight_truncated_str_view(
&mut out,
value_ref,
&truncated_view,
self.value_start_index,
Some(value_range_start),
&style,
&highlighting::SEARCH_MATCH_HIGHLIGHTED,
&mut self.search_matches,
)?;

if quoted {
out.set_style(&style)?;
out.buf.write_char('"')?;
}

if quoted {
used_space += 2;
used_space += 1;
highlighting::highlight_matches(
&mut out,
"\"",
Some(value_close_quote_range_start),
&style,
&highlighting::SEARCH_MATCH_HIGHLIGHTED,
&mut self.search_matches,
)?;
}

if self.trailing_comma {
used_space += 1;
self.tui.reset_style(buf)?;
buf.write_char(',')?;
highlighting::highlight_matches(
&mut out,
",",
Some(trailing_comma_range_start),
&highlighting::DEFAULT_STYLE,
&highlighting::SEARCH_MATCH_HIGHLIGHTED,
&mut self.search_matches,
)?;
}

Ok(used_space)
Expand Down
8 changes: 4 additions & 4 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ impl ScreenWriter {

let mut label = None;
let index_label: String;
let mut label_start_index = 0;
let mut label_range = &None;

// Set up key label.
if let Some(key_range) = &row.key_range {
label_start_index = key_range.start + 1;
let key = &viewer.flatjson.1[key_range.start + 1..key_range.end - 1];
label = Some(lp::LineLabel::Key { key });
label_range = &row.key_range;
}

// Set up index label.
Expand Down Expand Up @@ -290,9 +290,9 @@ impl ScreenWriter {
trailing_comma,

label,
label_start_index,
label_range,
value,
value_start_index,
value_range: &row.range,

search_matches: Some(search_matches),

Expand Down

0 comments on commit 6fe0c11

Please sign in to comment.