Skip to content

Commit

Permalink
Add highlight with awareness trait
Browse files Browse the repository at this point in the history
  • Loading branch information
bensadeh committed Jan 14, 2024
1 parent 2e7e7fc commit cec0bec
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/highlighters/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Highlight for DateHighlighter {
line_info.dashes < 2
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
highlight_utils::highlight_with_awareness_replace_all_with_new_style(&self.style, input, &DATE_REGEX, false)
}
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Highlight for IpHighlighter {
line_info.dots < 3
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
let segment = &self.segment;
let separator = &self.separator;
Expand Down
8 changes: 4 additions & 4 deletions src/highlighters/key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ impl KeyValueHighlighter {

impl Highlight for KeyValueHighlighter {
fn should_short_circuit(&self, line_info: &LineInfo) -> bool {
if line_info.equals < 1 {
return true;
}
line_info.equals < 1
}

false
fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl Highlight for KeywordHighlighter {
false
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
highlight_utils::highlight_with_awareness_replace_all_with_new_style(
&self.style,
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl Highlight for NumberHighlighter {
false
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
let color = &self.style;
highlight_utils::highlight_with_awareness_replace_all_with_new_style(color, input, &NUMBER_REGEX, false)
Expand Down
8 changes: 4 additions & 4 deletions src/highlighters/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub struct PathHighlighter {

impl Highlight for PathHighlighter {
fn should_short_circuit(&self, line_info: &LineInfo) -> bool {
if line_info.slashes == 0 {
return true;
}
line_info.slashes == 0
}

false
fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl Highlight for ProcessHighlighter {
line_info.left_bracket < 1 || line_info.right_bracket < 1
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
PROCESS_REGEX
.replace_all(input, |captures: &regex::Captures| {
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl Highlight for QuoteHighlighter {
line_info.double_quotes == 0 || line_info.double_quotes % 2 != 0
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
false
}

fn apply(&self, input: &str) -> String {
self.highlight_inside_quotes(input)
}
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Highlight for RegexpHighlighter {
false
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
let regex = &self.keyword_regex;
let color = &self.style;
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Highlight for TimeHighlighter {
line_info.colons < 2
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
self.highlight_time(input)
}
Expand Down
4 changes: 4 additions & 0 deletions src/highlighters/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl Highlight for UrlHighlighter {
line_info.slashes < 1 || line_info.colons == 0
}

fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
let highlighted = URL_REGEX.replace_all(input, |caps: &regex::Captures<'_>| {
let mut output = String::new();
Expand Down
8 changes: 4 additions & 4 deletions src/highlighters/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl UuidHighlighter {

impl Highlight for UuidHighlighter {
fn should_short_circuit(&self, line_info: &LineInfo) -> bool {
if line_info.dashes < 4 {
return true;
}
line_info.dashes < 4
}

false
fn only_apply_to_segments_not_already_highlighted(&self) -> bool {
true
}

fn apply(&self, input: &str) -> String {
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub const MISUSE_SHELL_BUILTIN: i32 = 2;

pub trait Highlight {
fn should_short_circuit(&self, line_info: &LineInfo) -> bool;
fn only_apply_to_segments_not_already_highlighted(&self) -> bool;
fn apply(&self, input: &str) -> String;
}

Expand Down

0 comments on commit cec0bec

Please sign in to comment.