-
Notifications
You must be signed in to change notification settings - Fork 8
/
nu.rs
270 lines (234 loc) · 8.2 KB
/
nu.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use super::Shell;
use crate::helpers::{get_config_dir, get_env_var_regex, normalize_newlines};
use crate::hooks::*;
use std::collections::HashSet;
use std::env::consts;
use std::fmt;
use std::path::{Path, PathBuf};
#[derive(Clone, Copy, Debug)]
pub struct Nu;
impl Nu {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self
}
}
fn join_path(value: impl AsRef<str>) -> String {
let parts = value
.as_ref()
.split(['/', '\\'])
.filter(|part| !part.is_empty())
.collect::<Vec<_>>();
format!("path join {}", parts.join(" "))
}
impl Shell for Nu {
// https://www.nushell.sh/book/configuration.html#environment
fn format(&self, statement: Statement<'_>) -> String {
let path_name = if consts::OS == "windows" {
"Path"
} else {
"PATH"
};
match statement {
Statement::PrependPath {
paths,
key,
orig_key,
} => {
let env_regex = get_env_var_regex();
let key = key.unwrap_or(path_name);
let orig_key = orig_key.unwrap_or(key);
let mut value = format!("$env.{key} = ($env.{orig_key} | split row (char esep)\n");
// https://www.nushell.sh/book/configuration.html#path-configuration
for path in paths.iter().rev() {
value.push_str(" | prepend ");
if let Some(cap) = env_regex.captures(path) {
let path_without_env = path.replace(cap.get(0).unwrap().as_str(), "");
value.push('(');
value.push_str(&format!("$env.{}", cap.name("name").unwrap().as_str()));
value.push_str(" | ");
value.push_str(&join_path(path_without_env));
value.push(')');
} else {
value.push_str(path);
}
value.push('\n');
}
value.push_str(" | uniq)");
normalize_newlines(value)
}
Statement::SetEnv { key, value } => {
if value.starts_with("$HOME/") {
let path = value.trim_start_matches("$HOME/");
format!("$env.{} = ($env.HOME | path join '{}')", key, path)
} else {
format!("$env.{} = {}", key, self.quote(value))
}
}
Statement::UnsetEnv { key } => {
format!("hide-env {key}")
}
}
}
fn format_hook(&self, hook: Hook) -> Result<String, crate::ShellError> {
let path_name = if consts::OS == "windows" {
"Path"
} else {
"PATH"
};
// https://www.nushell.sh/book/hooks.html#adding-a-single-hook-to-existing-config
Ok(normalize_newlines(match hook {
Hook::OnChangeDir { command, function } => {
format!(
r#"
$env.__ORIG_PATH = $env.{path_name}
def {function} [] {{
let data = {command} | from json
$data | get env | items {{ |k, v|
if $v == null {{
hide_env $k
}} else {{
load-env {{ ($k): $v }}
}}
}}
let path_list = $env.__ORIG_PATH | split row (char esep)
$data | get paths | reverse | each {{ |p|
let path_list = ($path_list | prepend $p)
}}
$env.{path_name} = ($path_list | uniq)
}}
$env.config = ($env.config | upsert hooks.env_change.PWD {{ |config|
let list = ($config | get -i hooks.env_change.PWD) | default []
$list | append {{ |before, after|
{function}
}}
}})"#
)
}
}))
}
fn get_config_path(&self, home_dir: &Path) -> PathBuf {
get_config_dir(home_dir).join("nushell").join("config.nu")
}
fn get_env_path(&self, home_dir: &Path) -> PathBuf {
get_config_dir(home_dir).join("nushell").join("env.nu")
}
// https://www.nushell.sh/book/configuration.html
fn get_profile_paths(&self, home_dir: &Path) -> Vec<PathBuf> {
HashSet::<PathBuf>::from_iter([
get_config_dir(home_dir).join("nushell").join("env.nu"),
home_dir.join(".config").join("nushell").join("env.nu"),
get_config_dir(home_dir).join("nushell").join("config.nu"),
home_dir.join(".config").join("nushell").join("config.nu"),
])
.into_iter()
.collect()
}
/// Quotes a string according to Nu shell quoting rules.
/// @see <https://www.nushell.sh/book/working_with_strings.html>
fn quote(&self, input: &str) -> String {
if input.contains('`') {
// Use backtick quoting for strings containing backticks
format!("`{}`", input)
} else if input.contains('\'') {
// Use double quotes with proper escaping for single-quoted strings
format!(
"\"{}\"",
input
.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
)
} else if input.contains('"') {
// Escape double quotes if present
format!(
"\"{}\"",
input
.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
)
} else {
// Use single quotes for other cases
format!("'{}'", input.replace('\n', "\\n"))
}
}
}
impl fmt::Display for Nu {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "nu")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_env_var() {
assert_eq!(
Nu.format_env_set("PROTO_HOME", "$HOME/.proto"),
r#"$env.PROTO_HOME = ($env.HOME | path join '.proto')"#
);
}
#[cfg(unix)]
#[test]
fn formats_path() {
assert_eq!(
Nu.format_path_set(&["$PROTO_HOME/shims".into(), "$PROTO_HOME/bin".into()]),
r#"$env.PATH = ($env.PATH | split row (char esep)
| prepend ($env.PROTO_HOME | path join bin)
| prepend ($env.PROTO_HOME | path join shims)
| uniq)"#
);
assert_eq!(
Nu.format_path_set(&["$HOME/with/sub/dir".into(), "/some/abs/path/bin".into()]),
r#"$env.PATH = ($env.PATH | split row (char esep)
| prepend /some/abs/path/bin
| prepend ($env.HOME | path join with sub dir)
| uniq)"#
);
}
#[cfg(windows)]
#[test]
fn formats_path() {
assert_eq!(
Nu.format_path_set(&["$PROTO_HOME/shims".into(), "$PROTO_HOME/bin".into()])
.replace("\r\n", "\n"),
r#"$env.Path = ($env.Path | split row (char esep)
| prepend ($env.PROTO_HOME | path join bin)
| prepend ($env.PROTO_HOME | path join shims)
| uniq)"#
);
assert_eq!(
Nu.format_path_set(&["$HOME/with/sub/dir".into(), "/some/abs/path/bin".into()])
.replace("\r\n", "\n"),
r#"$env.Path = ($env.Path | split row (char esep)
| prepend /some/abs/path/bin
| prepend ($env.HOME | path join with sub dir)
| uniq)"#
);
}
#[cfg(unix)]
#[test]
fn formats_cd_hook() {
use starbase_sandbox::assert_snapshot;
let hook = Hook::OnChangeDir {
command: "starbase hook nu".into(),
function: "_starbase_hook".into(),
};
assert_snapshot!(Nu.format_hook(hook).unwrap());
}
#[test]
fn test_nu_quoting() {
assert_eq!(Nu.quote("hello"), "'hello'");
assert_eq!(Nu.quote(""), "''");
assert_eq!(Nu.quote("echo 'hello'"), "\"echo 'hello'\"");
assert_eq!(Nu.quote("echo \"$HOME\""), "\"echo \\\"$HOME\\\"\"");
assert_eq!(Nu.quote("\"hello\""), "\"\\\"hello\\\"\"");
assert_eq!(Nu.quote("\"hello\nworld\""), "\"\\\"hello\\nworld\\\"\"");
assert_eq!(Nu.quote("$'hello world'"), "\"$'hello world'\"");
assert_eq!(Nu.quote("$''"), "\"$''\"");
assert_eq!(Nu.quote("$\"hello world\""), "\"$\\\"hello world\\\"\"");
assert_eq!(Nu.quote("$\"$HOME\""), "\"$\\\"$HOME\\\"\"");
assert_eq!(Nu.quote("'hello'"), "\"'hello'\"");
}
}