diff --git a/crates/nu-command/src/shells/enter.rs b/crates/nu-command/src/shells/enter.rs index 88bd09bf31..79dec79ef2 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 9cd8943d8a..dd429d4662 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 763a7ed524..bf704a40b1 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 12d4f15fee..25076ef174 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 94e8f70f34..56916c306f 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 696bb91fe4..579507afc6 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 c1fceded47..5b7f551008 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()