Skip to content

Commit 6df1f15

Browse files
committed
Add feature: add a specific color for each template
1 parent 871ead4 commit 6df1f15

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/metassr-fs-analyzer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ edition = "2021"
88

99
[dependencies]
1010
anyhow = "1.0.89"
11-
metassr-utils = { version = "0.0.1-alpha", path = "../metassr-utils" }
11+
metassr-utils = { version = "1.0.0-alpha", path = "../metassr-utils" }
1212
serde = "1.0.210"
1313
walkdir = "2.5.0"

metassr-cli/src/cli/creator.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use clap::ValueEnum;
22
use metassr_create::Creator as MetassrCreator;
3-
use std::{fmt::Display, str::FromStr};
3+
use std::{collections::HashMap, fmt::Display, str::FromStr};
44
use tracing::{error, info};
55

66
use super::traits::Exec;
77

8+
// ANSI color codes
9+
pub const RESET: &str = "\x1b[0m";
10+
pub const YELLOW: &str = "\x1b[93m";
11+
pub const BLUE: &str = "\x1b[94m";
812
pub struct Creator {
913
project_name: String,
1014
version: String,
@@ -68,7 +72,7 @@ impl Exec for Creator {
6872
&self.project_name,
6973
&self.version,
7074
&self.description,
71-
&self.template.to_string(),
75+
&self.template.as_str(),
7276
)
7377
.generate()
7478
{
@@ -79,18 +83,39 @@ impl Exec for Creator {
7983
}
8084
}
8185

82-
#[derive(Debug, ValueEnum, PartialEq, Eq, Clone, Copy)]
86+
#[derive(Debug, ValueEnum, PartialEq, Eq, Clone, Copy , Hash)]
8387
pub enum Template {
8488
Javascript,
8589
Typescript,
8690
}
91+
impl Template {
92+
fn templates_info() -> HashMap<Template, (&'static str, &'static str)> {
93+
let mut map = HashMap::new();
94+
map.insert(Template::Javascript, ("javascript", YELLOW));
95+
map.insert(Template::Typescript, ("typescript", BLUE));
96+
map
97+
}
98+
99+
pub fn as_str(&self) -> &'static str {
100+
Self::templates_info()
101+
.get(self)
102+
.map(|(name, _)| *name)
103+
.unwrap_or("unknown")
104+
}
105+
106+
pub fn fmt_colored(&self) -> String {
107+
let templates = Self::templates_info();
108+
if let Some((name, color)) = templates.get(self) {
109+
format!("{}{}{RESET}", color, name)
110+
} else {
111+
String::from("unknown")
112+
}
113+
}
114+
}
87115

88116
impl Display for Template {
89117
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90-
f.write_str(match *self {
91-
Self::Javascript => "javascript",
92-
Self::Typescript => "typescript",
93-
})
118+
write!(f, "{}", self.fmt_colored())
94119
}
95120
}
96121

0 commit comments

Comments
 (0)