forked from drewwalton19216801/rush-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_env.rs
More file actions
82 lines (74 loc) · 2.39 KB
/
builtin_env.rs
File metadata and controls
82 lines (74 loc) · 2.39 KB
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
use std::env;
use std::io::Write;
use crate::parser::ShellCommand;
use crate::state::ShellState;
pub struct EnvBuiltin;
impl super::Builtin for EnvBuiltin {
fn name(&self) -> &'static str {
"env"
}
fn names(&self) -> Vec<&'static str> {
vec![self.name()]
}
fn description(&self) -> &'static str {
"Print environment variables"
}
fn run(
&self,
_cmd: &ShellCommand,
shell_state: &mut ShellState,
output_writer: &mut dyn Write,
) -> i32 {
// Show exported shell variables first
for var_name in &shell_state.exported {
if let Some(value) = shell_state.variables.get(var_name) {
if shell_state.colors_enabled {
let _ = writeln!(
output_writer,
"{}{}\x1b[0m={}",
shell_state.color_scheme.success, var_name, value
);
} else {
let _ = writeln!(output_writer, "{}={}", var_name, value);
}
}
}
// Then show environment variables (excluding those already shown)
for (key, value) in env::vars() {
if !shell_state.exported.contains(&key) {
if shell_state.colors_enabled {
let _ = writeln!(
output_writer,
"{}{}\x1b[0m={}",
shell_state.color_scheme.builtin, key, value
);
} else {
let _ = writeln!(output_writer, "{}={}", key, value);
}
}
}
0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::builtins::Builtin;
#[test]
fn test_env_builtin_run() {
let cmd = ShellCommand {
args: vec!["env".to_string()],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
shell_state.colors_enabled = false;
shell_state.set_exported_var("TEST_VAR", "test_value".to_string());
let builtin = EnvBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
let output_str = String::from_utf8(output).unwrap();
assert!(output_str.contains("TEST_VAR=test_value"));
}
}