Skip to content

Commit 28e8870

Browse files
mostafa630hulxv
andauthored
feat: add coloring and style to create interactive command (#56)
* Add feature: add a specific color for each template * make fmt containing the whole logic of coloring templates --------- Co-authored-by: Mohamed Emad <hulxxv@gmail.com>
1 parent 9ff887c commit 28e8870

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

metassr-cli/src/cli/creator.rs

Lines changed: 26 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,33 @@ 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+
pub fn as_str(&self) -> &'static str {
93+
match self {
94+
Template::Javascript => "javascript",
95+
Template::Typescript => "typescript",
96+
}
97+
}
98+
}
8799

88100
impl Display for Template {
89101
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-
})
102+
let templates =
103+
HashMap::from([(Template::Javascript, YELLOW), (Template::Typescript, BLUE)]);
104+
write!(
105+
f,
106+
"{}{}{RESET}",
107+
templates.get(self).unwrap(),
108+
match self {
109+
Template::Javascript => "javascript",
110+
Template::Typescript => "typescript",
111+
}
112+
)
94113
}
95114
}
96115

0 commit comments

Comments
 (0)