Skip to content

Commit

Permalink
Merge pull request #170 from mihaigalos/feat/defaults
Browse files Browse the repository at this point in the history
feat: Defaults on no regex or colors
  • Loading branch information
mihaigalos authored May 22, 2024
2 parents f2ed782 + 8ca5d9e commit 94a81b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ impl WrappedBar {

fn compute_position(&mut self) -> f64 {
let previous_pos = self.output.position();
let pos = std::io::stdin()

std::io::stdin()
.lines()
.next()
.unwrap_or_else(|| Ok(previous_pos.to_string()))
.unwrap()
.parse::<f64>()
.unwrap_or(previous_pos as f64);
pos
.unwrap_or(previous_pos as f64)
}

fn set_minmax(&mut self, pos: f64) {
Expand Down
76 changes: 74 additions & 2 deletions src/colorizer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
use colored::*;
use regex::Regex;

pub fn colorize<'a>(
pub fn run<'a>(
input: &'a str,
regex: &'a str,
colors: &'a str,
) -> Result<Vec<ColoredString>, &'static str> {
if !regex.is_empty() && !colors.is_empty() {
colorize(input, regex, colors)
} else {
let num_commas = input.chars().filter(|&c| c == ',').count();
let num_spaces = input.chars().filter(|&c| c == ' ').count();
let upper_range = if num_commas > 0 {
num_commas
} else {
num_spaces
};

let mut regex: String = "(.*)".to_string();
let possible_colors: Vec<String> = vec![
"green".to_string(),
"cyan".to_string(),
"red".to_string(),
"white".to_string(),
"yellow".to_string(),
];
let mut colors: String = possible_colors[0].clone();
for i in 0..upper_range {
if num_commas > 0 {
regex.push_str(",\\s*(.*)");
} else {
regex.push_str(" \\s*(.*)");
}

colors.push(' ');
colors.push_str(&possible_colors[(i + 1) % possible_colors.len()]);
}
colorize(input, &regex, &colors)
}
}

fn colorize<'a>(
input: &'a str,
regex: &'a str,
colors: &'a str,
Expand All @@ -20,7 +59,7 @@ pub fn colorize<'a>(

if colors.len() != caps.len() {
panic!(
"Length of input: {} != length of regex match patterns: {}",
"Length of colors: {} != length of regex match patterns: {}",
colors.len(),
caps.len()
);
Expand Down Expand Up @@ -88,4 +127,37 @@ mod tests {
let result = all_captures_except_first(&caps).unwrap();
assert_eq!(result, expected);
}
#[test]
fn test_run_works_when_empty_regex_and_colors_and_input_containing_space() {
let input = "abc de";
let expected1 = ColoredString::from("abc").green();
let expected2 = ColoredString::from("de").cyan();

let result: Vec<ColoredString> = run(input, "", "").unwrap();

assert_eq!(result[0], expected1);
assert_eq!(result[1], expected2);
}
#[test]
fn test_run_works_when_empty_regex_and_colors() {
let input = "abc,de";
let expected1 = ColoredString::from("abc").green();
let expected2 = ColoredString::from("de").cyan();

let result: Vec<ColoredString> = run(input, "", "").unwrap();

assert_eq!(result[0], expected1);
assert_eq!(result[1], expected2);
}
#[test]
fn test_run_works_when_empty_regex_and_colors_and_input_containing_commaspace() {
let input = "abc, de";
let expected1 = ColoredString::from("abc").green();
let expected2 = ColoredString::from("de").cyan();

let result: Vec<ColoredString> = run(input, "", "").unwrap();

assert_eq!(result[0], expected1);
assert_eq!(result[1], expected2);
}
}
6 changes: 4 additions & 2 deletions src/formats/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const DEFAULT_CONFIG_IN_HOME_PATH: &str = ".config/pipeview.toml";

pub struct Custom;

fn extract_key<'a>(config: &'a HashMap<String, HashMap<String, String>>, key: &'a str) -> (bool, &'a str) {
fn extract_key<'a>(
config: &'a HashMap<String, HashMap<String, String>>,
key: &'a str,
) -> (bool, &'a str) {
if config.contains_key(key) {
return (true, key);
}
Expand Down Expand Up @@ -51,7 +54,6 @@ fn read_toml(path: &str) -> HashMap<String, HashMap<String, String>> {

impl FormatterFromToml for Custom {
fn get_config(custom_config_name: &str) -> (String, String) {

fn extract_config_from_toml(path: &str, custom_config_name: &str) -> (String, String) {
let config = read_toml(path);
let (found, key) = extract_key(&config, custom_config_name);
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ async fn main() -> Result<()> {
let config_name: String = config.parse().unwrap();
pipeview::formats::custom::Custom::get_config(&config_name)
} else {
let ids = args.ids()
.map(|id| id.as_str())
.collect::<Vec<_>>();
let ids = args.ids().map(|id| id.as_str()).collect::<Vec<_>>();

if ids.contains(&"regex") && ids.contains(&"colors") {
(
Expand All @@ -77,7 +75,7 @@ async fn main() -> Result<()> {
match line {
Err(_) => break,
Ok(s) => {
let _ = pipeview::colorizer::colorize(&s, &regex, &colors).unwrap();
let _ = pipeview::colorizer::run(&s, &regex, &colors).unwrap();
println!();
}
}
Expand Down

0 comments on commit 94a81b2

Please sign in to comment.