1
1
use serde:: { Deserialize , Serialize } ;
2
2
use std:: { fs, path:: { Path , PathBuf } } ;
3
+ use ratatui:: style:: Color ;
3
4
4
5
#[ derive( Deserialize , Serialize , Debug , Clone , PartialEq ) ]
5
6
pub struct ConnectionProfile {
6
7
pub name : String ,
7
8
pub url : String ,
8
9
pub db : Option < u8 > ,
9
10
pub dev : Option < bool > ,
11
+ pub color : Option < String > ,
12
+ }
13
+
14
+ impl ConnectionProfile {
15
+ pub fn resolved_color ( & self ) -> Color {
16
+ self . color
17
+ . as_deref ( )
18
+ . map ( parse_color)
19
+ . unwrap_or ( Color :: White )
20
+ }
21
+ }
22
+
23
+ fn parse_color ( spec : & str ) -> Color {
24
+ match spec. trim ( ) . to_lowercase ( ) . as_str ( ) {
25
+ "black" => Color :: Black ,
26
+ "red" => Color :: Red ,
27
+ "green" => Color :: Green ,
28
+ "yellow" => Color :: Yellow ,
29
+ "blue" => Color :: Blue ,
30
+ "magenta" => Color :: Magenta ,
31
+ "cyan" => Color :: Cyan ,
32
+ "gray" | "grey" => Color :: Gray ,
33
+ "darkgray" | "darkgrey" => Color :: DarkGray ,
34
+ "lightred" => Color :: LightRed ,
35
+ "lightgreen" => Color :: LightGreen ,
36
+ "lightyellow" => Color :: LightYellow ,
37
+ "lightblue" => Color :: LightBlue ,
38
+ "lightmagenta" => Color :: LightMagenta ,
39
+ "lightcyan" => Color :: LightCyan ,
40
+ "white" => Color :: White ,
41
+ other => {
42
+ if let Some ( hex) = other. strip_prefix ( '#' ) {
43
+ if hex. len ( ) == 6 {
44
+ if let ( Ok ( r) , Ok ( g) , Ok ( b) ) = (
45
+ u8:: from_str_radix ( & hex[ 0 ..2 ] , 16 ) ,
46
+ u8:: from_str_radix ( & hex[ 2 ..4 ] , 16 ) ,
47
+ u8:: from_str_radix ( & hex[ 4 ..6 ] , 16 ) ,
48
+ ) {
49
+ return Color :: Rgb ( r, g, b) ;
50
+ }
51
+ }
52
+ }
53
+ Color :: White
54
+ }
55
+ }
10
56
}
11
57
12
58
#[ derive( Deserialize , Serialize , Debug , Default , PartialEq ) ]
@@ -24,6 +70,7 @@ impl Config {
24
70
url: "redis://127.0.0.1:6379" . to_string( ) ,
25
71
db: Some ( 0 ) ,
26
72
dev: Some ( true ) ,
73
+ color: Some ( "green" . to_string( ) ) ,
27
74
}
28
75
]
29
76
}
@@ -112,6 +159,7 @@ mod tests {
112
159
assert_eq ! ( cfg, loaded) ;
113
160
assert_eq ! ( cfg. profiles. len( ) , 1 ) ;
114
161
assert_eq ! ( cfg. profiles[ 0 ] . name, "Default" ) ;
162
+ assert_eq ! ( cfg. profiles[ 0 ] . color. as_deref( ) , Some ( "green" ) ) ;
115
163
}
116
164
117
165
#[ test]
@@ -128,6 +176,7 @@ mod tests {
128
176
url: "redis://localhost:6379" . to_string( ) ,
129
177
db: Some ( 1 ) ,
130
178
dev: Some ( false ) ,
179
+ color: Some ( "red" . to_string( ) ) ,
131
180
} ] ,
132
181
} ;
133
182
fs:: write ( & cfg_file, toml:: to_string ( & custom_cfg) . unwrap ( ) ) . unwrap ( ) ;
0 commit comments