11use clap:: ValueEnum ;
22use metassr_create:: Creator as MetassrCreator ;
3- use std:: { fmt:: Display , str:: FromStr } ;
3+ use std:: { collections :: HashMap , fmt:: Display , str:: FromStr } ;
44use tracing:: { error, info} ;
55
66use 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" ;
812pub 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 ) ]
8387pub 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
88116impl 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