diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 2a568b7901..396eab3839 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1,7 +1,7 @@ use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement}; use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::{ - IntoPipelineData, PipelineData, Range, ShellError, Span, Spanned, Type, Unit, Value, + IntoPipelineData, PipelineData, Range, ShellError, Span, Spanned, Type, Unit, Value, VarId, }; use crate::get_full_help; @@ -210,9 +210,7 @@ pub fn eval_expression( span: expr.span, }) } - Expr::Var(var_id) => stack - .get_var(*var_id) - .map_err(move |_| ShellError::VariableNotFoundAtRuntime(expr.span)), + Expr::Var(var_id) => eval_variable(engine_state, stack, *var_id, expr.span), Expr::VarDecl(_) => Ok(Value::Nothing { span: expr.span }), Expr::CellPath(cell_path) => Ok(Value::CellPath { val: cell_path.clone(), @@ -372,6 +370,73 @@ pub fn eval_block( Ok(input) } +pub fn eval_variable( + _engine_state: &EngineState, + stack: &Stack, + var_id: VarId, + span: Span, +) -> Result { + if var_id == 0 { + // $nu + let mut output_cols = vec![]; + let mut output_vals = vec![]; + + let env_columns: Vec<_> = stack.get_env_vars().keys().map(|x| x.to_string()).collect(); + let env_values: Vec<_> = stack + .get_env_vars() + .values() + .map(|x| Value::String { + val: x.to_string(), + span, + }) + .collect(); + + output_cols.push("env".into()); + output_vals.push(Value::Record { + cols: env_columns, + vals: env_values, + span, + }); + + if let Some(mut config_path) = nu_path::config_dir() { + config_path.push("nushell"); + + let mut history_path = config_path.clone(); + + history_path.push("history.txt"); + + output_cols.push("history-path".into()); + output_vals.push(Value::String { + val: history_path.to_string_lossy().to_string(), + span, + }); + + 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, + }); + } + + if let Ok(cwd) = std::env::var("PWD") { + output_cols.push("pwd".into()); + output_vals.push(Value::String { val: cwd, span }) + } + + Ok(Value::Record { + cols: output_cols, + vals: output_vals, + span, + }) + } else { + stack + .get_var(var_id) + .map_err(move |_| ShellError::VariableNotFoundAtRuntime(span)) + } +} + pub fn compute(size: i64, unit: Unit, span: Span) -> Value { match unit { Unit::Byte => Value::Filesize { val: size, span }, diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 893c5f492e..14a437d8e0 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -1182,6 +1182,16 @@ pub fn parse_variable_expr( }, None, ); + } else if contents == b"$nu" { + return ( + Expression { + expr: Expr::Var(0), + span, + ty: Type::Unknown, + custom_completion: None, + }, + None, + ); } let (id, err) = parse_variable(working_set, span); diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index c9333ec7d7..36d1fe5227 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -102,7 +102,7 @@ impl EngineState { Self { files: im::vector![], file_contents: im::vector![], - vars: im::vector![], + vars: im::vector![Type::Unknown], decls: im::vector![], blocks: im::vector![], scope: im::vector![ScopeFrame::new()], diff --git a/src/main.rs b/src/main.rs index 5fff81f558..aecb0041aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,6 +122,10 @@ fn main() -> Result<()> { let mut stack = nu_protocol::engine::Stack::new(); + for (k, v) in std::env::vars() { + stack.env_vars.insert(k, v); + } + match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) { Ok(pipeline_data) => { println!("{}", pipeline_data.collect_string()); @@ -146,6 +150,10 @@ fn main() -> Result<()> { let mut nu_prompt = NushellPrompt::new(); let mut stack = nu_protocol::engine::Stack::new(); + for (k, v) in std::env::vars() { + stack.env_vars.insert(k, v); + } + // Load config startup file if let Some(mut config_path) = nu_path::config_dir() { config_path.push("nushell");