Skip to content

Commit

Permalink
feat: Add highligting (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
j03-dev committed Jun 11, 2024
1 parent 8665646 commit ac9d75b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/actors/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ impl ConsoleActor {
}

pub fn next(&mut self) {
self.index.clone_from(&self.order[(self.idx() + 1) % self.order.len()]);
self.index
.clone_from(&self.order[(self.idx() + 1) % self.order.len()]);
self.list_state.select(Some(self.idx()))
}

pub fn previous(&mut self) {
self.index.clone_from(&self.order[(self.idx() + self.order.len() - 1) % self.order.len()]);
self.index
.clone_from(&self.order[(self.idx() + self.order.len() - 1) % self.order.len()]);
self.list_state.select(Some(self.idx()))
}

Expand Down
41 changes: 33 additions & 8 deletions src/config/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl ColorOption {
Self { regex, color }
}

pub fn from(color_config: (&String, &String)) -> anyhow::Result<Self> {
pub fn from(color_config: (&str, &str)) -> anyhow::Result<Self> {
let (regex, color_str) = color_config;
let regex = Regex::new(regex)?;
let color = ColorOption::parse_color(color_str)?;
Expand Down Expand Up @@ -78,19 +78,44 @@ impl<'b> Colorizer<'b> {
pub fn patch_text<'a>(&self, str: &'a str) -> Vec<Line<'a>> {
let text = str.into_text().unwrap().patch_style(self.base_style);

if self.colors.is_empty() {
// We don't have color options.
// Just return base-styled lines.
return text.lines;
}
let mut color_options = vec![
ColorOption::from(("GET", "green")).unwrap(),
ColorOption::from(("POST", "#FFA500")).unwrap(),
ColorOption::from(("PUT", "#800080")).unwrap(),
ColorOption::from(("PATCH", "#800080")).unwrap(),
ColorOption::from(("DELETE", "red")).unwrap(),
ColorOption::from(("ERROR", "red")).unwrap(),
ColorOption::from(("RELOAD", "#800080")).unwrap(),
ColorOption::from((
r"(?x)
\b
\d+
(\.
\d+
)?
\b",
"cyan",
))
.unwrap(),
ColorOption::from((
r"(?x)
(?P<path>
[~/.][\w./-]*
/[\w.-]*
)",
"green",
))
.unwrap(),
ColorOption::from((r"https?://[^\s]+", "blue")).unwrap(), // https
];
color_options.extend(self.colors.clone());

// Iterate over lines and patch them one-by-one
text.lines
.iter()
.map(|line| {
let mut styled_line = line.clone();
let pure_str = Colorizer::line_as_string(line);
for opt in self.colors {
for opt in color_options.iter() {
styled_line =
self.merge_lines(&styled_line, &self.apply_color_option(&pure_str, opt));
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl RawConfig {
let task_color_options: Vec<ColorOption> = task
.color
.iter()
.filter_map(|color_config| ColorOption::from(color_config).ok())
.filter_map(|(r, c)| ColorOption::from((&r, &c)).ok())
.collect();

colors.insert(task_name.to_owned(), task_color_options);
Expand Down

0 comments on commit ac9d75b

Please sign in to comment.