split $nu variable into scope commands and simpler $nu (#9487)

# Description

This splits off `scope` from `$nu`, creating a set of `scope` commands
for the various types of scope you might be interested in.

This also simplifies the `$nu` variable a bit.

# User-Facing Changes

This changes `$nu` to be a bit simpler and introduces a set of `scope`
subcommands.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
JT
2023-06-21 09:33:01 +12:00
committed by GitHub
parent aaa21c1619
commit fbf3f7cf1c
21 changed files with 620 additions and 387 deletions

View File

@ -1,4 +1,4 @@
use crate::{current_dir_str, get_full_help, nu_variable::NuVariable};
use crate::{current_dir_str, get_full_help};
use nu_path::expand_path_with;
use nu_protocol::{
ast::{
@ -9,8 +9,9 @@ use nu_protocol::{
DataSource, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, PipelineMetadata,
Range, ShellError, Span, Spanned, Unit, Value, VarId, ENV_VARIABLE_ID,
};
use std::collections::HashMap;
use std::time::Instant;
use std::{collections::HashMap, path::PathBuf};
use sysinfo::SystemExt;
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
match op {
@ -1202,6 +1203,217 @@ pub fn eval_subexpression(
Ok(input)
}
pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> {
fn canonicalize_path(engine_state: &EngineState, path: &PathBuf) -> PathBuf {
let cwd = engine_state.current_work_dir();
if path.exists() {
match nu_path::canonicalize_with(path, cwd) {
Ok(canon_path) => canon_path,
Err(_) => path.clone(),
}
} else {
path.clone()
}
}
let mut cols = vec![];
let mut vals = vec![];
cols.push("default-config-dir".to_string());
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not get config directory".into())),
})
}
cols.push("config-path".to_string());
if let Some(path) = engine_state.get_config_path("config-path") {
let canon_config_path = canonicalize_path(engine_state, path);
vals.push(Value::String {
val: canon_config_path.to_string_lossy().to_string(),
span,
})
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("config.nu");
vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not get config directory".into())),
})
}
cols.push("env-path".to_string());
if let Some(path) = engine_state.get_config_path("env-path") {
let canon_env_path = canonicalize_path(engine_state, path);
vals.push(Value::String {
val: canon_env_path.to_string_lossy().to_string(),
span,
})
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("env.nu");
vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
"Could not find environment path".into(),
)),
})
}
cols.push("history-path".to_string());
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
match engine_state.config.history_file_format {
nu_protocol::HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
}
nu_protocol::HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
let canon_hist_path = canonicalize_path(engine_state, &path);
vals.push(Value::String {
val: canon_hist_path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not find history path".into())),
})
}
cols.push("loginshell-path".to_string());
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("login.nu");
let canon_login_path = canonicalize_path(engine_state, &path);
vals.push(Value::String {
val: canon_login_path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
"Could not find login shell path".into(),
)),
})
}
#[cfg(feature = "plugin")]
{
cols.push("plugin-path".to_string());
if let Some(path) = &engine_state.plugin_signatures {
let canon_plugin_path = canonicalize_path(engine_state, path);
vals.push(Value::String {
val: canon_plugin_path.to_string_lossy().to_string(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
"Could not get plugin signature location".into(),
)),
})
}
}
cols.push("home-path".to_string());
if let Some(path) = nu_path::home_dir() {
let canon_home_path = canonicalize_path(engine_state, &path);
vals.push(Value::String {
val: canon_home_path.to_string_lossy().into(),
span,
})
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError("Could not get home path".into())),
})
}
cols.push("temp-path".to_string());
let canon_temp_path = canonicalize_path(engine_state, &std::env::temp_dir());
vals.push(Value::String {
val: canon_temp_path.to_string_lossy().into(),
span,
});
cols.push("pid".to_string());
vals.push(Value::int(std::process::id().into(), span));
cols.push("os-info".to_string());
let sys = sysinfo::System::new();
let ver = match sys.kernel_version() {
Some(v) => v,
None => "unknown".into(),
};
let os_record = Value::Record {
cols: vec![
"name".into(),
"arch".into(),
"family".into(),
"kernel_version".into(),
],
vals: vec![
Value::string(std::env::consts::OS, span),
Value::string(std::env::consts::ARCH, span),
Value::string(std::env::consts::FAMILY, span),
Value::string(ver, span),
],
span,
};
vals.push(os_record);
cols.push("startup-time".to_string());
vals.push(Value::Duration {
val: engine_state.get_startup_time(),
span,
});
cols.push("is-interactive".to_string());
vals.push(Value::Bool {
val: engine_state.is_interactive,
span,
});
cols.push("is-login".to_string());
vals.push(Value::Bool {
val: engine_state.is_login,
span,
});
cols.push("current-exe".to_string());
if let Ok(current_exe) = std::env::current_exe() {
vals.push(Value::String {
val: current_exe.to_string_lossy().into(),
span,
});
} else {
vals.push(Value::Error {
error: Box::new(ShellError::IOError(
"Could not get current executable path".to_string(),
)),
})
}
Ok(Value::Record { cols, vals, span })
}
pub fn eval_variable(
engine_state: &EngineState,
stack: &Stack,
@ -1210,14 +1422,7 @@ pub fn eval_variable(
) -> Result<Value, ShellError> {
match var_id {
// $nu
nu_protocol::NU_VARIABLE_ID => Ok(Value::LazyRecord {
val: Box::new(NuVariable {
engine_state: engine_state.clone(),
stack: stack.clone(),
span,
}),
span,
}),
nu_protocol::NU_VARIABLE_ID => eval_nu_variable(engine_state, span),
ENV_VARIABLE_ID => {
let env_vars = stack.get_env_vars(engine_state);
let env_columns = env_vars.keys();

View File

@ -4,7 +4,6 @@ pub mod documentation;
pub mod env;
mod eval;
mod glob_from;
mod nu_variable;
pub mod scope;
pub use call_ext::CallExt;

View File

@ -1,267 +0,0 @@
use crate::scope::create_scope;
use core::fmt;
use nu_protocol::{
engine::{EngineState, Stack},
LazyRecord, ShellError, Span, Value,
};
use std::path::PathBuf;
use sysinfo::SystemExt;
// NuVariable: a LazyRecord for the special $nu variable
// $nu used to be a plain old Record, but LazyRecord lets us load different fields/columns lazily. This is important for performance;
// collecting all the information in $nu is expensive and unnecessary if you just want a subset of the data
// Note: NuVariable is not meaningfully serializable, this #[derive] is a lie to satisfy the type checker.
// Make sure to collect() the record before serializing it
#[derive(Clone)]
pub struct NuVariable {
pub engine_state: EngineState,
pub stack: Stack,
pub span: Span,
}
impl<'a> LazyRecord<'a> for NuVariable {
fn column_names(&self) -> Vec<&'static str> {
let mut cols = vec![
"default-config-dir",
"config-path",
"env-path",
"history-path",
"loginshell-path",
];
#[cfg(feature = "plugin")]
if self.engine_state.plugin_signatures.is_some() {
cols.push("plugin-path");
}
cols.push("scope");
cols.push("home-path");
cols.push("temp-path");
cols.push("pid");
cols.push("os-info");
cols.push("startup-time");
cols.push("is-interactive");
cols.push("is-login");
cols.push("current-exe");
cols
}
fn get_column_value(&self, column: &str) -> Result<Value, ShellError> {
let err = |message: &str| -> Result<Value, ShellError> {
Err(ShellError::LazyRecordAccessFailed {
message: message.into(),
column_name: column.to_string(),
span: self.span,
})
};
fn canonicalize_path(engine_state: &EngineState, path: &PathBuf) -> PathBuf {
let cwd = engine_state.current_work_dir();
if path.exists() {
match nu_path::canonicalize_with(path, cwd) {
Ok(canon_path) => canon_path,
Err(_) => path.clone(),
}
} else {
path.clone()
}
}
match column {
"default-config-dir" => {
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: self.span,
})
} else {
err("Could not get config directory")
}
}
"config-path" => {
if let Some(path) = self.engine_state.get_config_path("config-path") {
let canon_config_path = canonicalize_path(&self.engine_state, path);
Ok(Value::String {
val: canon_config_path.to_string_lossy().to_string(),
span: self.span,
})
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("config.nu");
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: self.span,
})
} else {
err("Could not get config directory")
}
}
"env-path" => {
if let Some(path) = self.engine_state.get_config_path("env-path") {
let canon_env_path = canonicalize_path(&self.engine_state, path);
Ok(Value::String {
val: canon_env_path.to_string_lossy().to_string(),
span: self.span,
})
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("env.nu");
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: self.span,
})
} else {
err("Could not get config directory")
}
}
"history-path" => {
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
match self.engine_state.config.history_file_format {
nu_protocol::HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
}
nu_protocol::HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
let canon_hist_path = canonicalize_path(&self.engine_state, &path);
Ok(Value::String {
val: canon_hist_path.to_string_lossy().to_string(),
span: self.span,
})
} else {
err("Could not get config directory")
}
}
"loginshell-path" => {
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("login.nu");
let canon_login_path = canonicalize_path(&self.engine_state, &path);
Ok(Value::String {
val: canon_login_path.to_string_lossy().to_string(),
span: self.span,
})
} else {
err("Could not get config directory")
}
}
"plugin-path" => {
#[cfg(feature = "plugin")]
{
if let Some(path) = &self.engine_state.plugin_signatures {
let canon_plugin_path = canonicalize_path(&self.engine_state, path);
Ok(Value::String {
val: canon_plugin_path.to_string_lossy().to_string(),
span: self.span,
})
} else {
err("Could not get plugin signature location")
}
}
#[cfg(not(feature = "plugin"))]
{
err("Plugin feature not enabled")
}
}
"scope" => Ok(create_scope(&self.engine_state, &self.stack, self.span())?),
"home-path" => {
if let Some(path) = nu_path::home_dir() {
let canon_home_path = canonicalize_path(&self.engine_state, &path);
Ok(Value::String {
val: canon_home_path.to_string_lossy().into(),
span: self.span(),
})
} else {
err("Could not get home path")
}
}
"temp-path" => {
let canon_temp_path = canonicalize_path(&self.engine_state, &std::env::temp_dir());
Ok(Value::String {
val: canon_temp_path.to_string_lossy().into(),
span: self.span(),
})
}
"pid" => Ok(Value::int(std::process::id().into(), self.span())),
"os-info" => {
let sys = sysinfo::System::new();
let ver = match sys.kernel_version() {
Some(v) => v,
None => "unknown".into(),
};
let os_record = Value::Record {
cols: vec![
"name".into(),
"arch".into(),
"family".into(),
"kernel_version".into(),
],
vals: vec![
Value::string(std::env::consts::OS, self.span()),
Value::string(std::env::consts::ARCH, self.span()),
Value::string(std::env::consts::FAMILY, self.span()),
Value::string(ver, self.span()),
],
span: self.span(),
};
Ok(os_record)
}
"is-interactive" => Ok(Value::Bool {
val: self.engine_state.is_interactive,
span: self.span,
}),
"is-login" => Ok(Value::Bool {
val: self.engine_state.is_login,
span: self.span,
}),
"startup-time" => Ok(Value::Duration {
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}'")),
}
}
fn span(&self) -> Span {
self.span
}
fn clone_value(&self, span: Span) -> Value {
Value::LazyRecord {
val: Box::new((*self).clone()),
span,
}
}
}
// manually implemented so we can skip engine_state which doesn't implement Debug
// FIXME: find a better way
impl fmt::Debug for NuVariable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NuVariable").finish()
}
}

View File

@ -283,7 +283,7 @@ impl<'e, 's> ScopeData<'e, 's> {
.collect::<Vec<(String, Value)>>();
// Until we allow custom commands to have input and output types, let's just
// make them Type::Any Type::Any so they can show up in our $nu.scope.commands
// make them Type::Any Type::Any so they can show up in our `scope commands`
// a little bit better. If sigs is empty, we're pretty sure that we're dealing
// with a custom command.
if sigs.is_empty() {