Skip to content

Commit

Permalink
Allow config of token separator
Browse files Browse the repository at this point in the history
  • Loading branch information
bensadeh committed Jan 17, 2024
1 parent 1e8524d commit 6e09afe
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 41 deletions.
9 changes: 8 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
[pointer]
number = { fg = "green" }
letter = { fg = "blue" }
separator = { fg = "red" }
separator_token = "a"
x = { fg = "red" }

[[regexps]]
regular_expression = 'Started (.*)\.'
style = { fg = "redd" }
style = { fg = "red" }

[[regexps]]
regular_expression = 'Stopped .*'
Expand Down
1 change: 1 addition & 0 deletions src/highlighters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl Highlighters {
theme.pointer.number,
theme.pointer.letter,
theme.pointer.separator,
theme.pointer.separator_token,
theme.pointer.x,
)));
}
Expand Down
86 changes: 46 additions & 40 deletions src/highlighters/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,20 @@ pub struct PointerHighlighter {
number: Style,
letter: Style,
separator: Style,
separator_token: char,
x: Style,
}

impl PointerHighlighter {
pub fn new(number: Style, letter: Style, separator: Style, x: Style) -> Self {
pub fn new(number: Style, letter: Style, separator: Style, separator_token: char, x: Style) -> Self {
Self {
number,
letter,
separator,
separator_token,
x,
}
}

fn highlight_char(&self, c: char) -> String {
match c {
'0'..='9' => format!("{}", self.number.paint(c.to_string())),
'x' | 'X' => format!("{}", self.x.paint(c.to_string())),
'a'..='f' | 'A'..='F' => format!("{}", self.letter.paint(c.to_string())),
_ => c.to_string(),
}
}

fn format_pointer_match(&self, caps: &Captures<'_>) -> String {
let prefix = caps.name("prefix").or_else(|| caps.name("prefix64")).unwrap().as_str();
let first_half = caps
.name("first_half")
.or_else(|| caps.name("first_half64"))
.unwrap()
.as_str();
let formatted_prefix = prefix.chars().map(|c| self.highlight_char(c)).collect::<String>();
let formatted_first_half = first_half.chars().map(|c| self.highlight_char(c)).collect::<String>();

caps.name("second_half").map_or_else(
|| format!("{}{}", formatted_prefix, formatted_first_half),
|second_half| {
let formatted_second_half = second_half
.as_str()
.chars()
.map(|c| self.highlight_char(c))
.collect::<String>();
format!(
"{}{}{}{}",
formatted_prefix,
formatted_first_half,
self.separator.paint("•"),
formatted_second_half
)
},
)
}
}

impl Highlight for PointerHighlighter {
Expand All @@ -82,7 +46,49 @@ impl Highlight for PointerHighlighter {

fn apply(&self, input: &str) -> String {
POINTER_REGEX
.replace_all(input, |caps: &Captures<'_>| self.format_pointer_match(caps))
.replace_all(input, |caps: &Captures<'_>| {
let prefix = caps.name("prefix").or_else(|| caps.name("prefix64")).unwrap().as_str();
let first_half = caps
.name("first_half")
.or_else(|| caps.name("first_half64"))
.unwrap()
.as_str();
let formatted_prefix = prefix
.chars()
.map(|c| highlight_char(c, self.number, self.x, self.letter))
.collect::<String>();
let formatted_first_half = first_half
.chars()
.map(|c| highlight_char(c, self.number, self.x, self.letter))
.collect::<String>();

caps.name("second_half").map_or_else(
|| format!("{}{}", formatted_prefix, formatted_first_half),
|second_half| {
let formatted_second_half = second_half
.as_str()
.chars()
.map(|c| highlight_char(c, self.number, self.x, self.letter))
.collect::<String>();
format!(
"{}{}{}{}",
formatted_prefix,
formatted_first_half,
self.separator.paint(self.separator_token.to_string()),
formatted_second_half
)
},
)
})
.to_string()
}
}

fn highlight_char(c: char, number: Style, x: Style, letter: Style) -> String {
match c {
'0'..='9' => format!("{}", number.paint(c.to_string())),
'x' | 'X' => format!("{}", x.paint(c.to_string())),
'a'..='f' | 'A'..='F' => format!("{}", letter.paint(c.to_string())),
_ => c.to_string(),
}
}
1 change: 1 addition & 0 deletions src/theme/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Default for Pointer {
number: Style::new().fg(Color::Blue).italic(),
letter: Style::new().fg(Color::Magenta).italic().dimmed(),
separator: Style::new().dimmed(),
separator_token: '•',
x: Style::new().fg(Color::Red),
disabled: false,
}
Expand Down
4 changes: 4 additions & 0 deletions src/theme/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ pub fn map_or_exit_early(raw: theme::raw::Theme) -> theme::processed::Theme {
number: raw.pointer.number.map_or(Pointer::default().number, to_style),
letter: raw.pointer.letter.map_or(Pointer::default().letter, to_style),
separator: raw.pointer.separator.map_or(Pointer::default().separator, to_style),
separator_token: raw
.pointer
.separator_token
.map_or(Pointer::default().separator_token, |c| c),
x: raw.pointer.x.map_or(Pointer::default().x, to_style),
disabled: raw.pointer.disabled,
},
Expand Down
1 change: 1 addition & 0 deletions src/theme/processed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Pointer {
pub number: Style,
pub letter: Style,
pub separator: Style,
pub separator_token: char,
pub x: Style,
pub disabled: bool,
}
Expand Down
1 change: 1 addition & 0 deletions src/theme/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Pointer {
pub number: Option<Style>,
pub letter: Option<Style>,
pub separator: Option<Style>,
pub separator_token: Option<char>,
pub x: Option<Style>,
pub disabled: bool,
}
Expand Down

0 comments on commit 6e09afe

Please sign in to comment.