mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:54:57 +02:00
Lots of misc improvements
This commit is contained in:
@ -52,6 +52,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
command("size", size::size),
|
||||
command("from-json", from_json::from_json),
|
||||
command("from-toml", from_toml::from_toml),
|
||||
command("from-yaml", from_yaml::from_yaml),
|
||||
command("open", open::open),
|
||||
command("pick", pick::pick),
|
||||
command("split-column", split_column::split_column),
|
||||
|
@ -1,13 +1,15 @@
|
||||
crate mod args;
|
||||
crate mod cd;
|
||||
crate mod classified;
|
||||
crate mod pick;
|
||||
crate mod command;
|
||||
crate mod config;
|
||||
crate mod first;
|
||||
crate mod from_json;
|
||||
crate mod from_toml;
|
||||
crate mod from_yaml;
|
||||
crate mod ls;
|
||||
crate mod open;
|
||||
crate mod pick;
|
||||
crate mod ps;
|
||||
crate mod reject;
|
||||
crate mod select;
|
||||
@ -16,7 +18,6 @@ crate mod skip;
|
||||
crate mod sort_by;
|
||||
crate mod split_column;
|
||||
crate mod split_row;
|
||||
crate mod first;
|
||||
crate mod to_array;
|
||||
crate mod to_json;
|
||||
crate mod to_toml;
|
||||
|
@ -3,16 +3,19 @@ use crate::prelude::*;
|
||||
use std::env;
|
||||
|
||||
pub fn cd(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let target = match args.positional.first() {
|
||||
// TODO: This needs better infra
|
||||
None => return Err(ShellError::string(format!("cd must take one arg"))),
|
||||
Some(v) => v.as_string()?.clone(),
|
||||
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
||||
let path = match args.positional.first() {
|
||||
None => match dirs::home_dir() {
|
||||
Some(o) => o,
|
||||
_ => return Err(ShellError::string("Can not change to home directory")),
|
||||
},
|
||||
Some(v) => {
|
||||
let target = v.as_string()?.clone();
|
||||
dunce::canonicalize(cwd.join(&target).as_path())?
|
||||
}
|
||||
};
|
||||
|
||||
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
||||
|
||||
let mut stream = VecDeque::new();
|
||||
let path = dunce::canonicalize(cwd.join(&target).as_path())?;
|
||||
let _ = env::set_current_dir(&path);
|
||||
stream.push_back(ReturnValue::change_cwd(path));
|
||||
Ok(stream.boxed())
|
||||
|
@ -1,17 +1,27 @@
|
||||
use crate::object::{Primitive, Value, Dictionary, DataDescriptor};
|
||||
use crate::object::base::OF64;
|
||||
use crate::object::{DataDescriptor, Dictionary, Primitive, Value};
|
||||
use crate::prelude::*;
|
||||
|
||||
fn convert_json_value_to_nu_value(v: &serde_json::Value) -> Value {
|
||||
fn convert_json_value_to_nu_value(v: &serde_hjson::Value) -> Value {
|
||||
match v {
|
||||
serde_json::Value::Null => Value::Primitive(Primitive::String("".to_string())),
|
||||
serde_json::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)),
|
||||
serde_json::Value::Number(n) => Value::Primitive(Primitive::Int(n.as_i64().unwrap())),
|
||||
serde_json::Value::String(s) => Value::Primitive(Primitive::String(s.clone())),
|
||||
serde_json::Value::Array(a) => Value::List(a.iter().map(|x| convert_json_value_to_nu_value(x)).collect()),
|
||||
serde_json::Value::Object(o) => {
|
||||
serde_hjson::Value::Null => Value::Primitive(Primitive::String("".to_string())),
|
||||
serde_hjson::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)),
|
||||
serde_hjson::Value::F64(n) => Value::Primitive(Primitive::Float(OF64::from(*n))),
|
||||
serde_hjson::Value::U64(n) => Value::Primitive(Primitive::Int(*n as i64)),
|
||||
serde_hjson::Value::I64(n) => Value::Primitive(Primitive::Int(*n as i64)),
|
||||
serde_hjson::Value::String(s) => Value::Primitive(Primitive::String(s.clone())),
|
||||
serde_hjson::Value::Array(a) => Value::List(
|
||||
a.iter()
|
||||
.map(|x| convert_json_value_to_nu_value(x))
|
||||
.collect(),
|
||||
),
|
||||
serde_hjson::Value::Object(o) => {
|
||||
let mut collected = Dictionary::default();
|
||||
for (k, v) in o.iter() {
|
||||
collected.add(DataDescriptor::from(k.clone()), convert_json_value_to_nu_value(v));
|
||||
collected.add(
|
||||
DataDescriptor::from(k.clone()),
|
||||
convert_json_value_to_nu_value(v),
|
||||
);
|
||||
}
|
||||
Value::Object(collected)
|
||||
}
|
||||
@ -19,7 +29,7 @@ fn convert_json_value_to_nu_value(v: &serde_json::Value) -> Value {
|
||||
}
|
||||
|
||||
pub fn from_json_string_to_value(s: String) -> Value {
|
||||
let v: serde_json::Value = serde_json::from_str(&s).unwrap();
|
||||
let v: serde_hjson::Value = serde_hjson::from_str(&s).unwrap();
|
||||
convert_json_value_to_nu_value(&v)
|
||||
}
|
||||
|
||||
|
54
src/commands/from_yaml.rs
Normal file
54
src/commands/from_yaml.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use crate::object::base::OF64;
|
||||
use crate::object::{DataDescriptor, Dictionary, Primitive, Value};
|
||||
use crate::prelude::*;
|
||||
|
||||
fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value) -> Value {
|
||||
match v {
|
||||
serde_yaml::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)),
|
||||
serde_yaml::Value::Number(n) if n.is_i64() => {
|
||||
Value::Primitive(Primitive::Int(n.as_i64().unwrap()))
|
||||
}
|
||||
serde_yaml::Value::Number(n) if n.is_f64() => {
|
||||
Value::Primitive(Primitive::Float(OF64::from(n.as_f64().unwrap())))
|
||||
}
|
||||
serde_yaml::Value::String(s) => Value::Primitive(Primitive::String(s.clone())),
|
||||
serde_yaml::Value::Sequence(a) => Value::List(
|
||||
a.iter()
|
||||
.map(|x| convert_yaml_value_to_nu_value(x))
|
||||
.collect(),
|
||||
),
|
||||
serde_yaml::Value::Mapping(t) => {
|
||||
let mut collected = Dictionary::default();
|
||||
for (k, v) in t.iter() {
|
||||
match k {
|
||||
serde_yaml::Value::String(k) => {
|
||||
collected.add(
|
||||
DataDescriptor::from(k.clone()),
|
||||
convert_yaml_value_to_nu_value(v),
|
||||
);
|
||||
}
|
||||
_ => unimplemented!("Unknown key type"),
|
||||
}
|
||||
}
|
||||
Value::Object(collected)
|
||||
}
|
||||
_ => unimplemented!("Unsupported yaml case"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_yaml_string_to_value(s: String) -> Value {
|
||||
let v: serde_yaml::Value = serde_yaml::from_str(&s).unwrap();
|
||||
convert_yaml_value_to_nu_value(&v)
|
||||
}
|
||||
|
||||
pub fn from_yaml(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let out = args.input;
|
||||
Ok(out
|
||||
.map(|a| match a {
|
||||
Value::Primitive(Primitive::String(s)) => {
|
||||
ReturnValue::Value(from_yaml_string_to_value(s))
|
||||
}
|
||||
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
|
||||
})
|
||||
.boxed())
|
||||
}
|
@ -1,11 +1,18 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::{dir_entry_dict, Value};
|
||||
use crate::object::{dir_entry_dict, Primitive, Value};
|
||||
use crate::prelude::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn ls(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
||||
let mut full_path = PathBuf::from(cwd);
|
||||
match &args.positional.get(0) {
|
||||
Some(Value::Primitive(Primitive::String(s))) => full_path.push(Path::new(s)),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let entries = std::fs::read_dir(&cwd).map_err(|e| ShellError::string(format!("{:?}", e)))?;
|
||||
let entries =
|
||||
std::fs::read_dir(&full_path).map_err(|e| ShellError::string(format!("{:?}", e)))?;
|
||||
|
||||
let mut shell_entries = VecDeque::new();
|
||||
|
||||
|
@ -1,13 +1,17 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::{Primitive, Value};
|
||||
use crate::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
if args.positional.len() == 0 {
|
||||
return Err(ShellError::string("open requires a filepath"));
|
||||
}
|
||||
|
||||
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
||||
let mut full_path = PathBuf::from(cwd);
|
||||
match &args.positional[0] {
|
||||
Value::Primitive(Primitive::String(s)) => full_path.push(s),
|
||||
Value::Primitive(Primitive::String(s)) => full_path.push(Path::new(s)),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@ -22,10 +26,24 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
|
||||
match full_path.extension() {
|
||||
Some(x) if x == "toml" && !open_raw => {
|
||||
stream.push_back(ReturnValue::Value(crate::commands::from_toml::from_toml_string_to_value(contents)));
|
||||
stream.push_back(ReturnValue::Value(
|
||||
crate::commands::from_toml::from_toml_string_to_value(contents),
|
||||
));
|
||||
}
|
||||
Some(x) if x == "json" && !open_raw => {
|
||||
stream.push_back(ReturnValue::Value(crate::commands::from_json::from_json_string_to_value(contents)));
|
||||
stream.push_back(ReturnValue::Value(
|
||||
crate::commands::from_json::from_json_string_to_value(contents),
|
||||
));
|
||||
}
|
||||
Some(x) if x == "yml" && !open_raw => {
|
||||
stream.push_back(ReturnValue::Value(
|
||||
crate::commands::from_yaml::from_yaml_string_to_value(contents),
|
||||
));
|
||||
}
|
||||
Some(x) if x == "yaml" && !open_raw => {
|
||||
stream.push_back(ReturnValue::Value(
|
||||
crate::commands::from_yaml::from_yaml_string_to_value(contents),
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
stream.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
|
||||
|
Reference in New Issue
Block a user