@@ -8,6 +8,7 @@ use crate::config::StarshipConfig;
8
8
use std:: fs:: File ;
9
9
use std:: io:: Write ;
10
10
use toml:: map:: Map ;
11
+ use toml:: value:: Table ;
11
12
use toml:: Value ;
12
13
13
14
#[ cfg( not( windows) ) ]
@@ -16,20 +17,13 @@ const STD_EDITOR: &str = "vi";
16
17
const STD_EDITOR : & str = "notepad.exe" ;
17
18
18
19
pub fn update_configuration ( name : & str , value : & str ) {
19
- let config_path = get_config_path ( ) ;
20
-
21
20
let keys: Vec < & str > = name. split ( '.' ) . collect ( ) ;
22
21
if keys. len ( ) != 2 {
23
22
log:: error!( "Please pass in a config key with a '.'" ) ;
24
23
process:: exit ( 1 ) ;
25
24
}
26
25
27
- let starship_config = StarshipConfig :: initialize ( ) ;
28
- let mut config = starship_config
29
- . config
30
- . expect ( "Failed to load starship config" ) ;
31
-
32
- if let Some ( table) = config. as_table_mut ( ) {
26
+ if let Some ( table) = get_configuration ( ) . as_table_mut ( ) {
33
27
if !table. contains_key ( keys[ 0 ] ) {
34
28
table. insert ( keys[ 0 ] . to_string ( ) , Value :: Table ( Map :: new ( ) ) ) ;
35
29
}
@@ -54,14 +48,68 @@ pub fn update_configuration(name: &str, value: &str) {
54
48
table. insert ( keys[ 0 ] . to_string ( ) , Value :: Table ( updated_values) ) ;
55
49
}
56
50
57
- let config_str =
58
- toml:: to_string_pretty ( & table) . expect ( "Failed to serialize the config to string" ) ;
59
- File :: create ( & config_path)
60
- . and_then ( |mut file| file. write_all ( config_str. as_ref ( ) ) )
61
- . expect ( "Error writing starship config" ) ;
51
+ write_configuration ( table) ;
62
52
}
63
53
}
64
54
55
+ pub fn toggle_configuration ( name : & str , key : & str ) {
56
+ if let Some ( table) = get_configuration ( ) . as_table_mut ( ) {
57
+ match table. get ( name) {
58
+ Some ( v) => {
59
+ if let Some ( values) = v. as_table ( ) {
60
+ let mut updated_values = values. clone ( ) ;
61
+
62
+ let current: bool = match updated_values. get ( key) {
63
+ Some ( v) => match v. as_bool ( ) {
64
+ Some ( b) => b,
65
+ _ => {
66
+ log:: error!(
67
+ "Given config key '{}' must be in 'boolean' format" ,
68
+ key
69
+ ) ;
70
+ process:: exit ( 1 ) ;
71
+ }
72
+ } ,
73
+ _ => {
74
+ log:: error!( "Given config key '{}' must be exist in config file" , key) ;
75
+ process:: exit ( 1 ) ;
76
+ }
77
+ } ;
78
+
79
+ updated_values. insert ( key. to_string ( ) , Value :: Boolean ( !current) ) ;
80
+
81
+ table. insert ( name. to_string ( ) , Value :: Table ( updated_values) ) ;
82
+
83
+ write_configuration ( table) ;
84
+ }
85
+ }
86
+ _ => {
87
+ log:: error!( "Given module '{}' not found in config file" , name) ;
88
+ process:: exit ( 1 ) ;
89
+ }
90
+ } ;
91
+ }
92
+ }
93
+
94
+ pub fn get_configuration ( ) -> Value {
95
+ let starship_config = StarshipConfig :: initialize ( ) ;
96
+
97
+ starship_config
98
+ . config
99
+ . expect ( "Failed to load starship config" )
100
+ }
101
+
102
+ pub fn write_configuration ( table : & mut Table ) {
103
+ let config_path = get_config_path ( ) ;
104
+
105
+ let config_str =
106
+ toml:: to_string_pretty ( & table) . expect ( "Failed to serialize the config to string" ) ;
107
+
108
+ File :: create ( & config_path)
109
+ . and_then ( |mut file| file. write_all ( config_str. as_ref ( ) ) )
110
+ . expect ( "Error writing starship config" ) ;
111
+ }
112
+
65
113
pub fn edit_configuration ( ) {
66
114
let config_path = get_config_path ( ) ;
67
115
let editor_cmd = shell_words:: split ( & get_editor ( ) ) . expect ( "Unmatched quotes found in $EDITOR." ) ;
@@ -119,7 +167,6 @@ mod tests {
119
167
use super :: * ;
120
168
121
169
// This is every possible permutation, 3² = 9.
122
-
123
170
#[ test]
124
171
fn visual_set_editor_set ( ) {
125
172
let actual = get_editor_internal ( Some ( "foo" . into ( ) ) , Some ( "bar" . into ( ) ) ) ;
0 commit comments