Skip to content

Commit

Permalink
printer.rs: De-duplicate code to highlight long lines
Browse files Browse the repository at this point in the history
We do this to only have one invocation of `highlighter.highlight(...)`
so we don't need to change to `highlighter.highlight_line(...)` in two
places in sharkdp#2181.
  • Loading branch information
Enselic committed May 5, 2022
1 parent adea895 commit f63b3fc
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,21 @@ impl<'a> Printer for InteractivePrinter<'a> {
return Ok(());
}
};

// skip syntax highlighting on long lines
if line.len() > 1024 * 16 {
let mut empty = highlighter_from_set
.highlighter
.highlight(&"\n", highlighter_from_set.syntax_set);
empty[0].1 = line.as_ref();
empty
} else {
highlighter_from_set
.highlighter
.highlight(&line, highlighter_from_set.syntax_set)
let too_long = line.len() > 1024 * 16;

let for_highlighting: &str = if too_long { "\n" } else { &line };

let mut highlighted_line = highlighter_from_set
.highlighter
.highlight(for_highlighting, highlighter_from_set.syntax_set);

if too_long {
highlighted_line[0].1 = &line;
}

highlighted_line
};

if out_of_range {
Expand Down

0 comments on commit f63b3fc

Please sign in to comment.