Get $nu.config-path and $nu.env-path from EngineState (#6366)

* Get `$nu.config-path` and `$nu.env-path` from `EngineState`

Signed-off-by: nibon7 <nibon7@163.com>

* replace tuple with hashmap

Signed-off-by: nibon7 <nibon7@163.com>

* refactor set_config_path

Signed-off-by: nibon7 <nibon7@163.com>

Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
nibon7
2022-08-23 00:30:09 +08:00
committed by GitHub
parent 9c4bbe3c63
commit 772ad896c8
3 changed files with 83 additions and 13 deletions

View File

@ -1298,6 +1298,22 @@ pub fn eval_variable(
let mut output_cols = vec![];
let mut output_vals = vec![];
if let Some(path) = engine_state.get_config_path("config-path") {
output_cols.push("config-path".into());
output_vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
});
}
if let Some(path) = engine_state.get_config_path("env-path") {
output_cols.push("env-path".into());
output_vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
});
}
if let Some(mut config_path) = nu_path::config_dir() {
config_path.push("nushell");
let mut env_config_path = config_path.clone();
@ -1321,21 +1337,25 @@ pub fn eval_variable(
span,
});
config_path.push("config.nu");
if engine_state.get_config_path("config-path").is_none() {
config_path.push("config.nu");
output_cols.push("config-path".into());
output_vals.push(Value::String {
val: config_path.to_string_lossy().to_string(),
span,
});
output_cols.push("config-path".into());
output_vals.push(Value::String {
val: config_path.to_string_lossy().to_string(),
span,
});
}
env_config_path.push("env.nu");
if engine_state.get_config_path("env-path").is_none() {
env_config_path.push("env.nu");
output_cols.push("env-path".into());
output_vals.push(Value::String {
val: env_config_path.to_string_lossy().to_string(),
span,
});
output_cols.push("env-path".into());
output_vals.push(Value::String {
val: env_config_path.to_string_lossy().to_string(),
span,
});
}
loginshell_path.push("login.nu");

View File

@ -76,6 +76,7 @@ pub struct EngineState {
pub plugin_signatures: Option<PathBuf>,
#[cfg(not(windows))]
sig_quit: Option<Arc<AtomicBool>>,
config_path: HashMap<String, PathBuf>,
}
pub const NU_VARIABLE_ID: usize = 0;
@ -113,6 +114,7 @@ impl EngineState {
plugin_signatures: None,
#[cfg(not(windows))]
sig_quit: None,
config_path: HashMap::new(),
}
}
@ -753,6 +755,14 @@ impl EngineState {
pub fn set_sig_quit(&mut self, sig_quit: Arc<AtomicBool>) {
self.sig_quit = Some(sig_quit)
}
pub fn set_config_path(&mut self, key: &str, val: PathBuf) {
self.config_path.insert(key.to_string(), val);
}
pub fn get_config_path(&self, key: &str) -> Option<&PathBuf> {
self.config_path.get(key)
}
}
/// A temporary extension to the global state. This handles bridging between the global state and the