From e6b196c141866af2b828673235c55e96302aaeff Mon Sep 17 00:00:00 2001 From: Jelle Besseling Date: Fri, 7 Apr 2023 20:51:09 +0200 Subject: [PATCH] Add $nu.current-exe variable (#8789) # Description Part solving #8752 Adds an extra variable to the `nu` table `current-exe` which is the path to the running shell executable. # User-Facing Changes Adds a variable to the `nu` table. # Tests + Formatting Tests and formatting have been run. No new test added # After Submitting I could add documentation for this if wanted Co-authored-by: Jelle Besseling --- crates/nu-cli/tests/completions.rs | 3 ++- crates/nu-engine/src/nu_variable.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs index 6e44de18f3..ca3c46d51f 100644 --- a/crates/nu-cli/tests/completions.rs +++ b/crates/nu-cli/tests/completions.rs @@ -524,10 +524,11 @@ fn variables_completions() { // Test completions for $nu let suggestions = completer.complete("$nu.", 4); - assert_eq!(12, suggestions.len()); + assert_eq!(13, suggestions.len()); let expected: Vec = vec![ "config-path".into(), + "current-exe".into(), "env-path".into(), "history-path".into(), "home-path".into(), diff --git a/crates/nu-engine/src/nu_variable.rs b/crates/nu-engine/src/nu_variable.rs index e1848dc05b..65d2d4f684 100644 --- a/crates/nu-engine/src/nu_variable.rs +++ b/crates/nu-engine/src/nu_variable.rs @@ -42,6 +42,8 @@ impl LazyRecord for NuVariable { cols.push("is-interactive"); cols.push("is-login"); + cols.push("current-exe"); + cols } @@ -213,6 +215,19 @@ impl LazyRecord for NuVariable { val: self.engine_state.get_startup_time(), span: self.span(), }), + "current-exe" => { + let exe = std::env::current_exe().map_err(|_| { + err("Could not get current executable path") + .expect_err("did not get err from err function") + })?; + + let canon_exe = canonicalize_path(&self.engine_state, &exe); + + Ok(Value::String { + val: canon_exe.to_string_lossy().into(), + span: self.span(), + }) + } _ => err(&format!("Could not find column '{column}'")), } }