forked from starship/starship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.rs
217 lines (189 loc) · 6.52 KB
/
configure.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::env;
use std::ffi::OsString;
use std::io::ErrorKind;
use std::process;
use std::process::Command;
use crate::config::StarshipConfig;
use std::fs::File;
use std::io::Write;
use toml::map::Map;
use toml::value::Table;
use toml::Value;
#[cfg(not(windows))]
const STD_EDITOR: &str = "vi";
#[cfg(windows)]
const STD_EDITOR: &str = "notepad.exe";
pub fn update_configuration(name: &str, value: &str) {
let keys: Vec<&str> = name.split('.').collect();
if keys.len() != 2 {
log::error!("Please pass in a config key with a '.'");
process::exit(1);
}
if let Some(table) = get_configuration().as_table_mut() {
if !table.contains_key(keys[0]) {
table.insert(keys[0].to_string(), Value::Table(Map::new()));
}
if let Some(values) = table.get(keys[0]).unwrap().as_table() {
let mut updated_values = values.clone();
if value.parse::<bool>().is_ok() {
updated_values.insert(
keys[1].to_string(),
Value::Boolean(value.parse::<bool>().unwrap()),
);
} else if value.parse::<i64>().is_ok() {
updated_values.insert(
keys[1].to_string(),
Value::Integer(value.parse::<i64>().unwrap()),
);
} else {
updated_values.insert(keys[1].to_string(), Value::String(value.to_string()));
}
table.insert(keys[0].to_string(), Value::Table(updated_values));
}
write_configuration(table);
}
}
pub fn toggle_configuration(name: &str, key: &str) {
if let Some(table) = get_configuration().as_table_mut() {
match table.get(name) {
Some(v) => {
if let Some(values) = v.as_table() {
let mut updated_values = values.clone();
let current: bool = match updated_values.get(key) {
Some(v) => match v.as_bool() {
Some(b) => b,
_ => {
log::error!(
"Given config key '{}' must be in 'boolean' format",
key
);
process::exit(1);
}
},
_ => {
log::error!("Given config key '{}' must be exist in config file", key);
process::exit(1);
}
};
updated_values.insert(key.to_string(), Value::Boolean(!current));
table.insert(name.to_string(), Value::Table(updated_values));
write_configuration(table);
}
}
_ => {
log::error!("Given module '{}' not found in config file", name);
process::exit(1);
}
};
}
}
pub fn get_configuration() -> Value {
let starship_config = StarshipConfig::initialize();
starship_config
.config
.expect("Failed to load starship config")
}
pub fn write_configuration(table: &mut Table) {
let config_path = get_config_path();
let config_str =
toml::to_string_pretty(&table).expect("Failed to serialize the config to string");
File::create(&config_path)
.and_then(|mut file| file.write_all(config_str.as_ref()))
.expect("Error writing starship config");
}
pub fn edit_configuration() {
let config_path = get_config_path();
let editor_cmd = shell_words::split(&get_editor()).expect("Unmatched quotes found in $EDITOR.");
let command = Command::new(&editor_cmd[0])
.args(&editor_cmd[1..])
.arg(config_path)
.status();
match command {
Ok(_) => (),
Err(error) => match error.kind() {
ErrorKind::NotFound => {
eprintln!(
"Error: editor {:?} was not found. Did you set your $EDITOR or $VISUAL \
environment variables correctly?",
editor_cmd
);
std::process::exit(1)
}
other_error => panic!("failed to open file: {:?}", other_error),
},
};
}
fn get_editor() -> String {
get_editor_internal(env::var("VISUAL").ok(), env::var("EDITOR").ok())
}
fn get_editor_internal(visual: Option<String>, editor: Option<String>) -> String {
let editor_name = visual.unwrap_or_else(|| "".into());
if !editor_name.is_empty() {
return editor_name;
}
let editor_name = editor.unwrap_or_else(|| "".into());
if !editor_name.is_empty() {
return editor_name;
}
STD_EDITOR.into()
}
fn get_config_path() -> OsString {
if let Some(config_path) = env::var_os("STARSHIP_CONFIG") {
return config_path;
}
dirs_next::home_dir()
.expect("couldn't find home directory")
.join(".config")
.join("starship.toml")
.into()
}
#[cfg(test)]
mod tests {
use super::*;
// This is every possible permutation, 3² = 9.
#[test]
fn visual_set_editor_set() {
let actual = get_editor_internal(Some("foo".into()), Some("bar".into()));
assert_eq!("foo", actual);
}
#[test]
fn visual_set_editor_empty() {
let actual = get_editor_internal(Some("foo".into()), None);
assert_eq!("foo", actual);
}
#[test]
fn visual_set_editor_not_set() {
let actual = get_editor_internal(Some("foo".into()), None);
assert_eq!("foo", actual);
}
#[test]
fn visual_empty_editor_set() {
let actual = get_editor_internal(Some("".into()), Some("bar".into()));
assert_eq!("bar", actual);
}
#[test]
fn visual_empty_editor_empty() {
let actual = get_editor_internal(Some("".into()), Some("".into()));
assert_eq!(STD_EDITOR, actual);
}
#[test]
fn visual_empty_editor_not_set() {
let actual = get_editor_internal(Some("".into()), None);
assert_eq!(STD_EDITOR, actual);
}
#[test]
fn visual_not_set_editor_set() {
let actual = get_editor_internal(None, Some("bar".into()));
assert_eq!("bar", actual);
}
#[test]
fn visual_not_set_editor_empty() {
let actual = get_editor_internal(None, Some("".into()));
assert_eq!(STD_EDITOR, actual);
}
#[test]
fn visual_not_set_editor_not_set() {
let actual = get_editor_internal(None, None);
assert_eq!(STD_EDITOR, actual);
}
}