From 6e723863604bcb3c027d8204ab2aae258cca1330 Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Thu, 4 Jun 2020 05:30:28 +0200 Subject: [PATCH] Refactoring, add comment --- crates/nu-cli/src/env/environment.rs | 37 ++++++++++------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/crates/nu-cli/src/env/environment.rs b/crates/nu-cli/src/env/environment.rs index f24f38b0e0..5b3c585bf8 100644 --- a/crates/nu-cli/src/env/environment.rs +++ b/crates/nu-cli/src/env/environment.rs @@ -2,12 +2,10 @@ use crate::data::config::Conf; use indexmap::{indexmap, IndexSet}; use nu_protocol::{UntaggedValue, Value}; use std::collections::{HashMap}; -use indexmap::IndexMap; use std::ffi::OsString; use std::fmt::Debug; use std::fs::File; use std::io::prelude::*; -use std::io::Write; use std::path::PathBuf; pub trait Env: Debug + Send { @@ -40,7 +38,7 @@ impl Env for Box { pub struct Environment { environment_vars: Option, path_vars: Option, - nurc_env_vars: HashMap>, + nurc_env_keys: HashMap>, //Directory -> Env key. If an environment var has been added from a .nurc in a directory, we track it here so we can remove it when the user leaves the directory. } impl Environment { @@ -48,7 +46,7 @@ impl Environment { Environment { environment_vars: None, path_vars: None, - nurc_env_vars: HashMap::new(), + nurc_env_keys: HashMap::new(), } } @@ -59,7 +57,7 @@ impl Environment { Environment { environment_vars: env, path_vars: path, - nurc_env_vars: HashMap::new(), + nurc_env_keys: HashMap::new(), } } @@ -90,7 +88,7 @@ impl Environment { self.add_env(k, v.as_str().unwrap()); }); - self.nurc_env_vars.insert( + self.nurc_env_keys.insert( std::env::current_dir()?, nurc_vars.keys().map(|k| k.clone()).collect(), ); //Maybe could do without clone here, but leave for now @@ -99,17 +97,16 @@ impl Environment { //If the user has left directories which added env vars through .nurc, we clear those vars //For each directory d in nurc_env_vars: - //if current_dir does not have d as a parent (possibly recursive), - //the vars set by d should be removed + //if current_dir does not have d as a parent (possibly recursive), the vars set by d should be removed pub fn clear_vars_from_unvisited_dirs(&mut self) -> std::io::Result<()> { let current_dir = std::env::current_dir()?; - let mut vars_to_keep = HashMap::new(); - for (d, v) in self.nurc_env_vars.iter() { + let mut new_nurc_env_vars = HashMap::new(); + for (d, v) in self.nurc_env_keys.iter() { let mut working_dir = Some(current_dir.as_path()); while working_dir.is_some() { if working_dir.unwrap() == d { - vars_to_keep.insert(d.clone(), v.clone()); + new_nurc_env_vars.insert(d.clone(), v.clone()); break; } else { working_dir = working_dir.unwrap().parent(); @@ -118,33 +115,25 @@ impl Environment { } let mut vars_to_delete = vec![]; - for (path, vals) in self.nurc_env_vars.iter() { - if !vars_to_keep.contains_key(path) { + for (path, vals) in self.nurc_env_keys.iter() { + if !new_nurc_env_vars.contains_key(path) { vars_to_delete.extend(vals.clone()); } } - vars_to_delete.iter().for_each(|var| self.remove_env(var)); + vars_to_delete.iter().for_each(|env_var| self.remove_env(env_var)); - self.nurc_env_vars = vars_to_keep; + self.nurc_env_keys = new_nurc_env_vars; Ok(()) } - // environment_vars: Option, - // pub fn remove_env(&mut self, key: &str) { if let Some(Value { value: UntaggedValue::Row(envs), tag: _, }) = &mut self.environment_vars { - - let mut file = File::create("env.txt").unwrap(); - write!(&mut file, "env: {:?}", envs.entries).unwrap(); - - let mut file = File::create("removenv.txt").unwrap(); - let removed = envs.remove_key(key); - write!(&mut file, "removed {:?}", (key, removed)).unwrap(); + envs.remove_key(key); } }