Skip to content

Commit

Permalink
option to use a color file
Browse files Browse the repository at this point in the history
  • Loading branch information
droppo committed Jan 6, 2023
1 parent 8ae23b0 commit 4f5c502
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "colorer"
version = "1.0.1"
version = "1.1.0-beta.1"
authors = ["droppo"]
edition = "2021"
description = "Simple command line utility that add color to commands that do not have it by default."
Expand Down
7 changes: 6 additions & 1 deletion src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::path::PathBuf;

use structopt::StructOpt;

#[derive(StructOpt)]
#[structopt(
author = "droppo - github.com/droppo",
name = "clrr - colorer",
about = "Simple command line utility to highlight the output of commands with custom color schemas."
about = "Simple command line utility to highlight the output of commands with custom color schemas.",
rename_all = "kebab-case"
)]
pub struct CliArgs {
#[structopt(long, short)]
pub file: Option<PathBuf>,
pub command: Vec<String>,
}
51 changes: 21 additions & 30 deletions src/core/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, fs::File, io::Read, path::Path, sync::Arc};

use serde_derive::{Deserialize, Serialize};
use structopt::StructOpt;

use crate::cli_args::CliArgs;

use super::decorator::{decorate, Decoration};

Expand All @@ -11,20 +14,6 @@ pub struct Command {
command: Option<Vec<ColorerRegex>>,
}

impl Command {
pub fn _new(
disabling_flags: Option<Vec<String>>,
subcommand: Option<HashMap<String, Vec<ColorerRegex>>>,
command: Option<Vec<ColorerRegex>>,
) -> Self {
Self {
disabling_flags,
subcommand,
command,
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ColorerRegex {
regex: String,
Expand All @@ -50,24 +39,26 @@ pub fn init_parser(
command: &str,
args: &[String],
) -> Result<Option<Arc<Vec<ColorerRegex>>>, Box<dyn std::error::Error>> {
let config = Path::new("/etc").join("colorer");
if config.exists() {
let config_path = config.join(format!("{}.toml", command));
let cli = CliArgs::from_args();
let colorer_file_path = match cli.file {
Some(filepath) => filepath,
None => Path::new("/etc")
.join("colorer")
.join(format!("{}.toml", command)),
};

if config_path.exists() {
let mut config_file = File::open(config_path)?;
let mut configs = String::new();
config_file.read_to_string(&mut configs)?;
let pattern: Command = toml::from_str(&configs)?;
if colorer_file_path.exists() {
let mut config_file = File::open(colorer_file_path)?;
let mut configs = String::new();
config_file.read_to_string(&mut configs)?;
let pattern: Command = toml::from_str(&configs)?;

// TODO check if the loaded configs contains any disabling flag
if let Some(values) = pattern.command {
return Ok(Some(Arc::new(values)));
} else if let Some(values) = pattern.subcommand {
if let Some(subcommand) = args.get(1) {
if let Some(colors) = values.get(subcommand) {
return Ok(Some(Arc::new(colors.to_vec())));
}
if let Some(values) = pattern.command {
return Ok(Some(Arc::new(values)));
} else if let Some(values) = pattern.subcommand {
if let Some(subcommand) = args.get(1) {
if let Some(colors) = values.get(subcommand) {
return Ok(Some(Arc::new(colors.to_vec())));
}
}
}
Expand Down

0 comments on commit 4f5c502

Please sign in to comment.