diff --git a/crates/nu-command/src/shells/enter.rs b/crates/nu-command/src/shells/enter.rs index 88bd09bf3..79dec79ef 100644 --- a/crates/nu-command/src/shells/enter.rs +++ b/crates/nu-command/src/shells/enter.rs @@ -1,3 +1,4 @@ +use super::{get_current_shell, get_shells}; use nu_engine::{current_dir, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -62,21 +63,8 @@ impl Command for Enter { span: call.head, }; - let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); - let mut shells = if let Some(v) = shells { - v.as_list() - .map(|x| x.to_vec()) - .unwrap_or_else(|_| vec![cwd]) - } else { - vec![cwd] - }; - - let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL"); - let mut current_shell = if let Some(v) = current_shell { - v.as_integer().unwrap_or_default() as usize - } else { - 0 - }; + let mut shells = get_shells(engine_state, stack, cwd); + let mut current_shell = get_current_shell(engine_state, stack); if current_shell + 1 > shells.len() { shells.push(new_path.clone()); diff --git a/crates/nu-command/src/shells/exit.rs b/crates/nu-command/src/shells/exit.rs index 9cd8943d8..dd429d466 100644 --- a/crates/nu-command/src/shells/exit.rs +++ b/crates/nu-command/src/shells/exit.rs @@ -1,3 +1,4 @@ +use super::{get_current_shell, get_shells}; use nu_engine::{current_dir, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -57,21 +58,8 @@ impl Command for Exit { span: call.head, }; - let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); - let mut shells = if let Some(v) = shells { - v.as_list() - .map(|x| x.to_vec()) - .unwrap_or_else(|_| vec![cwd]) - } else { - vec![cwd] - }; - - let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL"); - let mut current_shell = if let Some(v) = current_shell { - v.as_integer().unwrap_or_default() as usize - } else { - 0 - }; + let mut shells = get_shells(engine_state, stack, cwd); + let mut current_shell = get_current_shell(engine_state, stack); shells.remove(current_shell); diff --git a/crates/nu-command/src/shells/g.rs b/crates/nu-command/src/shells/g.rs index 763a7ed52..bf704a40b 100644 --- a/crates/nu-command/src/shells/g.rs +++ b/crates/nu-command/src/shells/g.rs @@ -1,3 +1,4 @@ +use super::get_shells; use nu_engine::{current_dir, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -43,14 +44,7 @@ impl Command for GotoShell { span: call.head, }; - let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); - let shells = if let Some(v) = shells { - v.as_list() - .map(|x| x.to_vec()) - .unwrap_or_else(|_| vec![cwd]) - } else { - vec![cwd] - }; + let shells = get_shells(engine_state, stack, cwd); let new_path = if let Some(v) = shells.get(new_shell.item as usize) { v.clone() diff --git a/crates/nu-command/src/shells/mod.rs b/crates/nu-command/src/shells/mod.rs index 12d4f15fe..25076ef17 100644 --- a/crates/nu-command/src/shells/mod.rs +++ b/crates/nu-command/src/shells/mod.rs @@ -9,5 +9,28 @@ pub use enter::Enter; pub use exit::Exit; pub use g::GotoShell; pub use n::NextShell; +use nu_protocol::engine::{EngineState, Stack}; +use nu_protocol::Value; pub use p::PrevShell; pub use shells_::Shells; + +fn get_shells(engine_state: &EngineState, stack: &mut Stack, cwd: Value) -> Vec { + let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); + let shells = if let Some(v) = shells { + v.as_list() + .map(|x| x.to_vec()) + .unwrap_or_else(|_| vec![cwd]) + } else { + vec![cwd] + }; + shells +} + +fn get_current_shell(engine_state: &EngineState, stack: &mut Stack) -> usize { + let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL"); + if let Some(v) = current_shell { + v.as_integer().unwrap_or_default() as usize + } else { + 0 + } +} diff --git a/crates/nu-command/src/shells/n.rs b/crates/nu-command/src/shells/n.rs index 94e8f70f3..56916c306 100644 --- a/crates/nu-command/src/shells/n.rs +++ b/crates/nu-command/src/shells/n.rs @@ -1,3 +1,4 @@ +use super::{get_current_shell, get_shells}; use nu_engine::current_dir; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -33,21 +34,8 @@ impl Command for NextShell { span: call.head, }; - let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); - let shells = if let Some(v) = shells { - v.as_list() - .map(|x| x.to_vec()) - .unwrap_or_else(|_| vec![cwd]) - } else { - vec![cwd] - }; - - let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL"); - let mut current_shell = if let Some(v) = current_shell { - v.as_integer().unwrap_or_default() as usize - } else { - 0 - }; + let shells = get_shells(engine_state, stack, cwd); + let mut current_shell = get_current_shell(engine_state, stack); current_shell += 1; diff --git a/crates/nu-command/src/shells/p.rs b/crates/nu-command/src/shells/p.rs index 696bb91fe..579507afc 100644 --- a/crates/nu-command/src/shells/p.rs +++ b/crates/nu-command/src/shells/p.rs @@ -1,3 +1,4 @@ +use super::{get_current_shell, get_shells}; use nu_engine::current_dir; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -33,21 +34,8 @@ impl Command for PrevShell { span: call.head, }; - let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); - let shells = if let Some(v) = shells { - v.as_list() - .map(|x| x.to_vec()) - .unwrap_or_else(|_| vec![cwd]) - } else { - vec![cwd] - }; - - let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL"); - let mut current_shell = if let Some(v) = current_shell { - v.as_integer().unwrap_or_default() as usize - } else { - 0 - }; + let shells = get_shells(engine_state, stack, cwd); + let mut current_shell = get_current_shell(engine_state, stack); if current_shell == 0 { current_shell = shells.len() - 1; diff --git a/crates/nu-command/src/shells/shells_.rs b/crates/nu-command/src/shells/shells_.rs index c1fceded4..5b7f55100 100644 --- a/crates/nu-command/src/shells/shells_.rs +++ b/crates/nu-command/src/shells/shells_.rs @@ -1,3 +1,4 @@ +use super::{get_current_shell, get_shells}; use nu_engine::current_dir; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -36,21 +37,8 @@ impl Command for Shells { span, }; - let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS"); - let shells = if let Some(v) = shells { - v.as_list() - .map(|x| x.to_vec()) - .unwrap_or_else(|_| vec![cwd]) - } else { - vec![cwd] - }; - - let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL"); - let current_shell = if let Some(v) = current_shell { - v.as_integer().unwrap_or_default() as usize - } else { - 0 - }; + let shells = get_shells(engine_state, stack, cwd); + let current_shell = get_current_shell(engine_state, stack); let output = shells .into_iter()