2021-03-15 07:26:30 +00:00
|
|
|
use crate::evaluate::scope::Scope;
|
2021-03-29 08:27:51 +00:00
|
|
|
use indexmap::IndexMap;
|
2021-03-27 05:08:03 +00:00
|
|
|
use nu_data::config::NuConfig;
|
2020-02-11 23:25:56 +00:00
|
|
|
use nu_errors::ShellError;
|
2021-03-15 07:26:30 +00:00
|
|
|
use nu_protocol::{Primitive, TaggedDictBuilder, UntaggedValue, Value};
|
2021-03-29 08:27:51 +00:00
|
|
|
use nu_source::{Spanned, Tag};
|
2020-02-11 23:25:56 +00:00
|
|
|
|
2021-03-15 07:26:30 +00:00
|
|
|
pub fn nu(scope: &Scope, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
2020-02-11 23:25:56 +00:00
|
|
|
let tag = tag.into();
|
|
|
|
|
2021-03-27 05:08:03 +00:00
|
|
|
let env = &scope.get_env_vars();
|
|
|
|
|
|
|
|
let config = if let Some(Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::FilePath(path)),
|
|
|
|
..
|
|
|
|
}) = scope.get_var("config-path")
|
|
|
|
{
|
|
|
|
NuConfig::with(Some(path).map(|p| p.into_os_string()))
|
|
|
|
} else {
|
|
|
|
NuConfig::new()
|
|
|
|
};
|
|
|
|
|
2020-02-11 23:25:56 +00:00
|
|
|
let mut nu_dict = TaggedDictBuilder::new(&tag);
|
|
|
|
|
|
|
|
let mut dict = TaggedDictBuilder::new(&tag);
|
2020-05-27 04:50:26 +00:00
|
|
|
for v in env.iter() {
|
2020-02-11 23:25:56 +00:00
|
|
|
if v.0 != "PATH" && v.0 != "Path" {
|
|
|
|
dict.insert_untagged(v.0, UntaggedValue::string(v.1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nu_dict.insert_value("env", dict.into_value());
|
|
|
|
|
2021-03-27 05:08:03 +00:00
|
|
|
nu_dict.insert_value(
|
|
|
|
"config",
|
|
|
|
UntaggedValue::row(config.vars.clone()).into_value(&tag),
|
|
|
|
);
|
2020-02-11 23:25:56 +00:00
|
|
|
|
|
|
|
let mut table = vec![];
|
2021-02-18 02:56:14 +00:00
|
|
|
for v in env.iter() {
|
|
|
|
if v.0 == "PATH" || v.0 == "Path" {
|
|
|
|
for path in std::env::split_paths(&v.1) {
|
|
|
|
table.push(UntaggedValue::filepath(path).into_value(&tag));
|
|
|
|
}
|
2020-02-11 23:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nu_dict.insert_value("path", UntaggedValue::table(&table).into_value(&tag));
|
|
|
|
|
2020-03-08 05:33:30 +00:00
|
|
|
let path = std::env::current_dir()?;
|
2021-01-08 07:30:41 +00:00
|
|
|
nu_dict.insert_value("cwd", UntaggedValue::filepath(path).into_value(&tag));
|
2020-03-08 05:33:30 +00:00
|
|
|
|
2021-01-10 02:50:49 +00:00
|
|
|
if let Some(home) = crate::filesystem::filesystem_shell::homedir_if_possible() {
|
2021-01-08 07:30:41 +00:00
|
|
|
nu_dict.insert_value("home-dir", UntaggedValue::filepath(home).into_value(&tag));
|
2020-03-08 05:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let temp = std::env::temp_dir();
|
2021-01-08 07:30:41 +00:00
|
|
|
nu_dict.insert_value("temp-dir", UntaggedValue::filepath(temp).into_value(&tag));
|
2020-03-08 05:33:30 +00:00
|
|
|
|
2021-01-08 07:30:41 +00:00
|
|
|
nu_dict.insert_value(
|
|
|
|
"config-path",
|
2021-03-27 05:08:03 +00:00
|
|
|
UntaggedValue::filepath(nu_data::config::path::source_file(&config)).into_value(&tag),
|
2021-01-08 07:30:41 +00:00
|
|
|
);
|
2020-03-08 05:33:30 +00:00
|
|
|
|
2020-09-17 06:02:30 +00:00
|
|
|
#[cfg(feature = "rustyline-support")]
|
|
|
|
{
|
2021-01-13 17:31:47 +00:00
|
|
|
let keybinding_path = nu_data::keybinding::keybinding_path()?;
|
2020-09-17 06:02:30 +00:00
|
|
|
nu_dict.insert_value(
|
|
|
|
"keybinding-path",
|
2021-01-08 07:30:41 +00:00
|
|
|
UntaggedValue::filepath(keybinding_path).into_value(&tag),
|
2020-09-17 06:02:30 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-15 07:51:59 +00:00
|
|
|
|
2020-03-08 05:33:30 +00:00
|
|
|
nu_dict.insert_value(
|
|
|
|
"history-path",
|
2021-03-27 05:08:03 +00:00
|
|
|
UntaggedValue::filepath(nu_data::config::path::history(&config)).into_value(&tag),
|
2020-03-08 05:33:30 +00:00
|
|
|
);
|
|
|
|
|
2020-02-11 23:25:56 +00:00
|
|
|
Ok(nu_dict.into_value())
|
|
|
|
}
|
2021-03-29 08:27:51 +00:00
|
|
|
|
|
|
|
pub fn scope(
|
|
|
|
aliases: &IndexMap<String, Vec<Spanned<String>>>,
|
|
|
|
tag: impl Into<Tag>,
|
|
|
|
) -> Result<Value, ShellError> {
|
|
|
|
let tag = tag.into();
|
|
|
|
|
|
|
|
let mut scope_dict = TaggedDictBuilder::new(&tag);
|
|
|
|
|
|
|
|
let mut dict = TaggedDictBuilder::new(&tag);
|
|
|
|
for v in aliases.iter() {
|
|
|
|
let values = v.1.clone();
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
|
|
|
|
for k in values.iter() {
|
|
|
|
vec.push(k.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
let alias = vec.join(" ");
|
|
|
|
dict.insert_untagged(v.0, UntaggedValue::string(alias));
|
|
|
|
}
|
|
|
|
|
|
|
|
scope_dict.insert_value("aliases", dict.into_value());
|
|
|
|
Ok(scope_dict.into_value())
|
|
|
|
}
|