mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 06:51:28 +02:00
Tests pass
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crossterm::{cursor, terminal, Attribute, RawScreen};
|
||||
use indexmap::IndexMap;
|
||||
use nu::{serve_plugin, Args, CommandConfig, Plugin, ShellError, Value};
|
||||
use nu::{serve_plugin, Args, CommandConfig, Plugin, ShellError, Spanned, Value};
|
||||
use pretty_hex::*;
|
||||
|
||||
struct BinaryView;
|
||||
@@ -15,8 +15,7 @@ impl Plugin for BinaryView {
|
||||
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
||||
Ok(CommandConfig {
|
||||
name: "binaryview".to_string(),
|
||||
mandatory_positional: vec![],
|
||||
optional_positional: vec![],
|
||||
positional: vec![],
|
||||
can_load: vec![],
|
||||
can_save: vec![],
|
||||
is_filter: false,
|
||||
@@ -26,10 +25,13 @@ impl Plugin for BinaryView {
|
||||
})
|
||||
}
|
||||
|
||||
fn sink(&mut self, _args: Args, input: Vec<Value>) {
|
||||
fn sink(&mut self, _args: Args, input: Vec<Spanned<Value>>) {
|
||||
for v in input {
|
||||
match v {
|
||||
Value::Binary(b) => {
|
||||
Spanned {
|
||||
item: Value::Binary(b),
|
||||
..
|
||||
} => {
|
||||
let _ = view_binary(&b);
|
||||
}
|
||||
_ => {}
|
||||
|
@@ -1,11 +1,8 @@
|
||||
use indexmap::IndexMap;
|
||||
use nu::{
|
||||
serve_plugin, Args, CommandConfig, Plugin, PositionalType, Primitive, ReturnValue, ShellError,
|
||||
Spanned, Value,
|
||||
serve_plugin, Args, CommandConfig, Plugin, PositionalType, Primitive, ReturnSuccess,
|
||||
ReturnValue, ShellError, Spanned, SpannedItem, Value,
|
||||
};
|
||||
use nu::{Primitive, ReturnSuccess, ReturnValue, ShellError, Spanned, SpannedItem, Value};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
|
||||
struct Inc {
|
||||
inc_by: i64,
|
||||
@@ -16,87 +13,47 @@ impl Inc {
|
||||
}
|
||||
}
|
||||
|
||||
fn send_response<T: Serialize>(result: Vec<T>) {
|
||||
let response = JsonRpc::new("response", result);
|
||||
let response_raw = serde_json::to_string(&response).unwrap();
|
||||
println!("{}", response_raw);
|
||||
}
|
||||
|
||||
fn send_error(error: ShellError) {
|
||||
let message: ReturnValue = Err(error);
|
||||
send_response(vec![message])
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "method")]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum NuCommand {
|
||||
init { params: Vec<Spanned<Value>> },
|
||||
filter { params: Spanned<Value> },
|
||||
quit,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut inc_by = 1;
|
||||
|
||||
loop {
|
||||
let mut input = String::new();
|
||||
match io::stdin().read_line(&mut input) {
|
||||
Ok(_) => {
|
||||
let command = serde_json::from_str::<NuCommand>(&input);
|
||||
|
||||
match command {
|
||||
Ok(NuCommand::init { params }) => {
|
||||
for param in params {
|
||||
match param {
|
||||
Spanned {
|
||||
item: Value::Primitive(Primitive::Int(i)),
|
||||
..
|
||||
} => {
|
||||
inc_by = i;
|
||||
}
|
||||
_ => {
|
||||
send_error(ShellError::string("Unrecognized type in params"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(NuCommand::filter { params }) => match params {
|
||||
Spanned {
|
||||
item: Value::Primitive(Primitive::Int(i)),
|
||||
span,
|
||||
} => {
|
||||
send_response(vec![ReturnSuccess::value(
|
||||
Value::int(i + inc_by).spanned(span),
|
||||
)]);
|
||||
}
|
||||
Spanned {
|
||||
item: Value::Primitive(Primitive::Bytes(b)),
|
||||
span,
|
||||
} => {
|
||||
send_response(vec![ReturnSuccess::value(
|
||||
Value::bytes(b + inc_by as u128).spanned(span),
|
||||
)]);
|
||||
}
|
||||
_ => {
|
||||
send_error(ShellError::string("Unrecognized type in stream"));
|
||||
}
|
||||
},
|
||||
Ok(NuCommand::quit) => {
|
||||
break;
|
||||
}
|
||||
Err(_) => {
|
||||
send_error(ShellError::string("Unrecognized type in stream"));
|
||||
impl Plugin for Inc {
|
||||
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
||||
Ok(CommandConfig {
|
||||
name: "inc".to_string(),
|
||||
positional: vec![PositionalType::mandatory("Increment")],
|
||||
can_load: vec![],
|
||||
can_save: vec![],
|
||||
is_filter: true,
|
||||
is_sink: false,
|
||||
named: IndexMap::new(),
|
||||
rest_positional: true,
|
||||
})
|
||||
}
|
||||
fn begin_filter(&mut self, args: Args) -> Result<(), ShellError> {
|
||||
if let Some(args) = args.positional {
|
||||
for arg in args {
|
||||
match arg {
|
||||
Spanned {
|
||||
item: Value::Primitive(Primitive::Int(i)),
|
||||
..
|
||||
} => {
|
||||
self.inc_by = i;
|
||||
}
|
||||
_ => return Err(ShellError::string("Unrecognized type in params")),
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
send_error(ShellError::string("Unrecognized type in stream"));
|
||||
}
|
||||
Value::Primitive(Primitive::Bytes(b)) => Ok(vec![ReturnValue::Value(Value::bytes(
|
||||
b + self.inc_by as u64,
|
||||
))]),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let span = input.span;
|
||||
|
||||
match input.item {
|
||||
Value::Primitive(Primitive::Int(i)) => Ok(vec![ReturnSuccess::value(
|
||||
Value::int(i + self.inc_by).spanned(span),
|
||||
)]),
|
||||
Value::Primitive(Primitive::Bytes(b)) => Ok(vec![ReturnSuccess::value(
|
||||
Value::bytes(b + self.inc_by as u64).spanned(span),
|
||||
)]),
|
||||
x => Err(ShellError::string(format!(
|
||||
"Unrecognized type in stream: {:?}",
|
||||
x
|
||||
|
@@ -1,6 +1,7 @@
|
||||
use indexmap::IndexMap;
|
||||
use nu::{
|
||||
serve_plugin, Args, CommandConfig, Plugin, Primitive, ReturnValue, ShellError, Spanned, Value,
|
||||
serve_plugin, Args, CommandConfig, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError,
|
||||
Spanned, Value,
|
||||
};
|
||||
|
||||
struct NewSkip {
|
||||
@@ -16,8 +17,7 @@ impl Plugin for NewSkip {
|
||||
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
||||
Ok(CommandConfig {
|
||||
name: "skip".to_string(),
|
||||
mandatory_positional: vec![],
|
||||
optional_positional: vec![],
|
||||
positional: vec![],
|
||||
can_load: vec![],
|
||||
can_save: vec![],
|
||||
is_filter: true,
|
||||
@@ -44,9 +44,9 @@ impl Plugin for NewSkip {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if self.skip_amount == 0 {
|
||||
Ok(vec![ReturnValue::Value(input)])
|
||||
Ok(vec![ReturnSuccess::value(input)])
|
||||
} else {
|
||||
self.skip_amount -= 1;
|
||||
Ok(vec![])
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use derive_new::new;
|
||||
use indexmap::IndexMap;
|
||||
use nu::{serve_plugin, Args, CommandConfig, Plugin, ShellError, Value};
|
||||
use nu::{serve_plugin, Args, CommandConfig, Plugin, ShellError, Spanned, Value};
|
||||
use ptree::item::StringItem;
|
||||
use ptree::output::print_tree_with;
|
||||
use ptree::print_config::PrintConfig;
|
||||
@@ -31,7 +31,6 @@ impl TreeView {
|
||||
}
|
||||
}
|
||||
Value::Block(_) => {}
|
||||
Value::Error(_) => {}
|
||||
Value::Filesystem => {}
|
||||
Value::Binary(_) => {}
|
||||
}
|
||||
@@ -85,8 +84,7 @@ impl Plugin for TreeViewer {
|
||||
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
||||
Ok(CommandConfig {
|
||||
name: "tree".to_string(),
|
||||
mandatory_positional: vec![],
|
||||
optional_positional: vec![],
|
||||
positional: vec![],
|
||||
can_load: vec![],
|
||||
can_save: vec![],
|
||||
is_filter: false,
|
||||
@@ -96,12 +94,11 @@ impl Plugin for TreeViewer {
|
||||
})
|
||||
}
|
||||
|
||||
fn sink(&mut self, _args: Args, input: Vec<Value>) {
|
||||
fn sink(&mut self, _args: Args, input: Vec<Spanned<Value>>) {
|
||||
if input.len() > 0 {
|
||||
for i in input.iter() {
|
||||
let view = TreeView::from_value(&i);
|
||||
let _ = view.render_view();
|
||||
//handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user