Merge pull request #265 from nushell/nu_var

Add some support for $nu
This commit is contained in:
JT 2021-10-30 08:23:49 +13:00 committed by GitHub
commit a160d480b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 5 deletions

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement}; use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{ 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; use crate::get_full_help;
@ -210,9 +210,7 @@ pub fn eval_expression(
span: expr.span, span: expr.span,
}) })
} }
Expr::Var(var_id) => stack Expr::Var(var_id) => eval_variable(engine_state, stack, *var_id, expr.span),
.get_var(*var_id)
.map_err(move |_| ShellError::VariableNotFoundAtRuntime(expr.span)),
Expr::VarDecl(_) => Ok(Value::Nothing { span: expr.span }), Expr::VarDecl(_) => Ok(Value::Nothing { span: expr.span }),
Expr::CellPath(cell_path) => Ok(Value::CellPath { Expr::CellPath(cell_path) => Ok(Value::CellPath {
val: cell_path.clone(), val: cell_path.clone(),
@ -372,6 +370,73 @@ pub fn eval_block(
Ok(input) Ok(input)
} }
pub fn eval_variable(
_engine_state: &EngineState,
stack: &Stack,
var_id: VarId,
span: Span,
) -> Result<Value, ShellError> {
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 { pub fn compute(size: i64, unit: Unit, span: Span) -> Value {
match unit { match unit {
Unit::Byte => Value::Filesize { val: size, span }, Unit::Byte => Value::Filesize { val: size, span },

View File

@ -1182,6 +1182,16 @@ pub fn parse_variable_expr(
}, },
None, 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); let (id, err) = parse_variable(working_set, span);

View File

@ -102,7 +102,7 @@ impl EngineState {
Self { Self {
files: im::vector![], files: im::vector![],
file_contents: im::vector![], file_contents: im::vector![],
vars: im::vector![], vars: im::vector![Type::Unknown],
decls: im::vector![], decls: im::vector![],
blocks: im::vector![], blocks: im::vector![],
scope: im::vector![ScopeFrame::new()], scope: im::vector![ScopeFrame::new()],

View File

@ -122,6 +122,10 @@ fn main() -> Result<()> {
let mut stack = nu_protocol::engine::Stack::new(); 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()) { match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) {
Ok(pipeline_data) => { Ok(pipeline_data) => {
println!("{}", pipeline_data.collect_string()); println!("{}", pipeline_data.collect_string());
@ -146,6 +150,10 @@ fn main() -> Result<()> {
let mut nu_prompt = NushellPrompt::new(); let mut nu_prompt = NushellPrompt::new();
let mut stack = nu_protocol::engine::Stack::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 // Load config startup file
if let Some(mut config_path) = nu_path::config_dir() { if let Some(mut config_path) = nu_path::config_dir() {
config_path.push("nushell"); config_path.push("nushell");