mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 20:24:24 +02:00
--no-edit
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use crossterm::{cursor, terminal, Attribute, RawScreen};
|
||||
use nu::{
|
||||
serve_plugin, AnchorLocation, CallInfo, Plugin, Primitive, ShellError, Signature, Tagged, Value,
|
||||
outln, serve_plugin, AnchorLocation, CallInfo, Plugin, Primitive, ShellError, Signature,
|
||||
Tagged, Value,
|
||||
};
|
||||
use pretty_hex::*;
|
||||
|
||||
@ -47,7 +48,7 @@ fn view_binary(
|
||||
}
|
||||
#[cfg(not(feature = "rawkey"))]
|
||||
{
|
||||
println!("Interactive binary viewing currently requires the 'rawkey' feature");
|
||||
outln!("Interactive binary viewing currently requires the 'rawkey' feature");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@ -117,7 +118,7 @@ impl RenderContext {
|
||||
);
|
||||
}
|
||||
}
|
||||
println!("{}", Attribute::Reset);
|
||||
outln!("{}", Attribute::Reset);
|
||||
Ok(())
|
||||
}
|
||||
fn render_to_screen_hires(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -174,7 +175,7 @@ impl RenderContext {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
println!("{}", Attribute::Reset);
|
||||
outln!("{}", Attribute::Reset);
|
||||
Ok(())
|
||||
}
|
||||
pub fn flush(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -264,7 +265,7 @@ pub fn view_contents(
|
||||
|
||||
if raw_image_buffer.is_none() {
|
||||
//Not yet supported
|
||||
println!("{:?}", buffer.hex_dump());
|
||||
outln!("{:?}", buffer.hex_dump());
|
||||
return Ok(());
|
||||
}
|
||||
let raw_image_buffer = raw_image_buffer.unwrap();
|
||||
@ -322,7 +323,7 @@ pub fn view_contents(
|
||||
}
|
||||
_ => {
|
||||
//Not yet supported
|
||||
println!("{:?}", buffer.hex_dump());
|
||||
outln!("{:?}", buffer.hex_dump());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,10 @@
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, Plugin, ReturnSuccess, ReturnValue, ShellError, Signature, SyntaxShape,
|
||||
Tagged, Value,
|
||||
serve_plugin, CallInfo, ColumnPath, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError,
|
||||
ShellTypeName, Signature, SpannedItem, SyntaxShape, Tagged, TaggedItem, Value,
|
||||
};
|
||||
|
||||
pub type ColumnPath = Tagged<Vec<Tagged<Value>>>;
|
||||
|
||||
struct Edit {
|
||||
field: Option<ColumnPath>,
|
||||
field: Option<Tagged<ColumnPath>>,
|
||||
value: Option<Value>,
|
||||
}
|
||||
impl Edit {
|
||||
@ -21,7 +19,7 @@ impl Edit {
|
||||
let value_tag = value.tag();
|
||||
match (value.item, self.value.clone()) {
|
||||
(obj @ Value::Row(_), Some(v)) => match &self.field {
|
||||
Some(f) => match obj.replace_data_at_column_path(value_tag, &f, v) {
|
||||
Some(f) => match obj.tagged(value_tag).replace_data_at_column_path(&f, v) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::labeled_error(
|
||||
@ -65,12 +63,17 @@ impl Plugin for Edit {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
match &args[0] {
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
item: Value::Primitive(Primitive::ColumnPath(_)),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(table.as_column_path()?);
|
||||
}
|
||||
value => return Err(ShellError::type_error("table", value.tagged_type_name())),
|
||||
value => {
|
||||
return Err(ShellError::type_error(
|
||||
"table",
|
||||
value.type_name().spanned(value.span()),
|
||||
))
|
||||
}
|
||||
}
|
||||
match &args[1] {
|
||||
Tagged { item: v, .. } => {
|
||||
|
@ -2,8 +2,8 @@
|
||||
extern crate indexmap;
|
||||
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature,
|
||||
SyntaxShape, Tag, Tagged, TaggedItem, Value,
|
||||
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError,
|
||||
ShellTypeName, Signature, SpannedItem, SyntaxShape, Tag, Tagged, TaggedItem, Value,
|
||||
};
|
||||
|
||||
struct Embed {
|
||||
@ -42,7 +42,12 @@ impl Plugin for Embed {
|
||||
self.field = Some(s.clone());
|
||||
self.values = Vec::new();
|
||||
}
|
||||
value => return Err(ShellError::type_error("string", value.tagged_type_name())),
|
||||
value => {
|
||||
return Err(ShellError::type_error(
|
||||
"string",
|
||||
value.type_name().spanned(value.span()),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
use nu::{
|
||||
did_you_mean, serve_plugin, tag_for_tagged_list, CallInfo, Plugin, Primitive, ReturnSuccess,
|
||||
ReturnValue, ShellError, Signature, SyntaxShape, Tagged, TaggedItem, Value,
|
||||
did_you_mean, serve_plugin, span_for_spanned_list, CallInfo, ColumnPath, Plugin, Primitive,
|
||||
ReturnSuccess, ReturnValue, ShellError, ShellTypeName, Signature, SpannedItem, SyntaxShape,
|
||||
Tagged, TaggedItem, Value,
|
||||
};
|
||||
|
||||
enum Action {
|
||||
@ -14,10 +15,8 @@ pub enum SemVerAction {
|
||||
Patch,
|
||||
}
|
||||
|
||||
pub type ColumnPath = Tagged<Vec<Tagged<Value>>>;
|
||||
|
||||
struct Inc {
|
||||
field: Option<ColumnPath>,
|
||||
field: Option<Tagged<ColumnPath>>,
|
||||
error: Option<String>,
|
||||
action: Option<Action>,
|
||||
}
|
||||
@ -89,7 +88,7 @@ impl Inc {
|
||||
} else {
|
||||
return Err(ShellError::type_error(
|
||||
"incrementable value",
|
||||
value.tagged_type_name(),
|
||||
value.type_name().spanned(value.span()),
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -98,48 +97,32 @@ impl Inc {
|
||||
Some(ref f) => {
|
||||
let fields = f.clone();
|
||||
|
||||
let replace_for = value.item.get_data_by_column_path(
|
||||
value.tag(),
|
||||
f,
|
||||
Box::new(move |(obj_source, column_path_tried)| {
|
||||
let replace_for = value.get_data_by_column_path(
|
||||
&f,
|
||||
Box::new(move |(obj_source, column_path_tried, _)| {
|
||||
match did_you_mean(&obj_source, &column_path_tried) {
|
||||
Some(suggestions) => {
|
||||
return ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", suggestions[0].1),
|
||||
tag_for_tagged_list(fields.iter().map(|p| p.tag())),
|
||||
span_for_spanned_list(fields.iter().map(|p| p.span)),
|
||||
)
|
||||
}
|
||||
None => {
|
||||
return ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
"row does not contain this column",
|
||||
tag_for_tagged_list(fields.iter().map(|p| p.tag())),
|
||||
span_for_spanned_list(fields.iter().map(|p| p.span)),
|
||||
)
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let replacement = match replace_for {
|
||||
Ok(got) => match got {
|
||||
Some(result) => self.inc(result.map(|x| x.clone()))?,
|
||||
None => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"inc could not find field to replace",
|
||||
"column name",
|
||||
value.tag(),
|
||||
))
|
||||
}
|
||||
},
|
||||
Err(reason) => return Err(reason),
|
||||
};
|
||||
let got = replace_for?;
|
||||
let replacement = self.inc(got.map(|x| x.clone()))?;
|
||||
|
||||
match value.item.replace_data_at_column_path(
|
||||
value.tag(),
|
||||
&f,
|
||||
replacement.item.clone(),
|
||||
) {
|
||||
match value.replace_data_at_column_path(&f, replacement.item.clone()) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::labeled_error(
|
||||
@ -156,7 +139,7 @@ impl Inc {
|
||||
},
|
||||
_ => Err(ShellError::type_error(
|
||||
"incrementable value",
|
||||
value.tagged_type_name(),
|
||||
value.type_name().spanned(value.span()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@ -188,12 +171,17 @@ impl Plugin for Inc {
|
||||
for arg in args {
|
||||
match arg {
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
item: Value::Primitive(Primitive::ColumnPath(_)),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(table.as_column_path()?);
|
||||
}
|
||||
value => return Err(ShellError::type_error("table", value.tagged_type_name())),
|
||||
value => {
|
||||
return Err(ShellError::type_error(
|
||||
"table",
|
||||
value.type_name().spanned(value.span()),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -229,8 +217,8 @@ mod tests {
|
||||
use super::{Inc, SemVerAction};
|
||||
use indexmap::IndexMap;
|
||||
use nu::{
|
||||
CallInfo, EvaluatedArgs, Plugin, Primitive, ReturnSuccess, Tag, Tagged, TaggedDictBuilder,
|
||||
TaggedItem, Value,
|
||||
CallInfo, EvaluatedArgs, PathMember, Plugin, RawPathMember, ReturnSuccess, SpannedItem,
|
||||
Tag, Tagged, TaggedDictBuilder, TaggedItem, Value,
|
||||
};
|
||||
|
||||
struct CallStub {
|
||||
@ -255,13 +243,13 @@ mod tests {
|
||||
}
|
||||
|
||||
fn with_parameter(&mut self, name: &str) -> &mut Self {
|
||||
let fields: Vec<Tagged<Value>> = name
|
||||
let fields: Vec<PathMember> = name
|
||||
.split(".")
|
||||
.map(|s| Value::string(s.to_string()).tagged(Tag::unknown()))
|
||||
.map(|s| RawPathMember::String(s.to_string()).spanned_unknown())
|
||||
.collect();
|
||||
|
||||
self.positionals
|
||||
.push(Value::Table(fields).tagged(Tag::unknown()));
|
||||
.push(Value::column_path(fields).tagged_unknown());
|
||||
self
|
||||
}
|
||||
|
||||
@ -344,14 +332,13 @@ mod tests {
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(
|
||||
plugin.field.map(|f| f
|
||||
.iter()
|
||||
.map(|f| match &f.item {
|
||||
Value::Primitive(Primitive::String(s)) => s.clone(),
|
||||
_ => panic!(""),
|
||||
})
|
||||
.collect()),
|
||||
Some(vec!["package".to_string(), "version".to_string()])
|
||||
plugin
|
||||
.field
|
||||
.map(|f| f.iter().map(|f| f.item.clone()).collect()),
|
||||
Some(vec![
|
||||
RawPathMember::String("package".to_string()),
|
||||
RawPathMember::String("version".to_string())
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,11 @@
|
||||
use itertools::Itertools;
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, Plugin, ReturnSuccess, ReturnValue, ShellError, Signature, SyntaxShape,
|
||||
Tagged, TaggedItem, Value,
|
||||
serve_plugin, CallInfo, ColumnPath, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError,
|
||||
ShellTypeName, Signature, SpannedItem, SyntaxShape, Tagged, Value,
|
||||
};
|
||||
|
||||
pub type ColumnPath = Vec<Tagged<Value>>;
|
||||
|
||||
struct Insert {
|
||||
field: Option<ColumnPath>,
|
||||
value: Option<Value>,
|
||||
value: Option<Tagged<Value>>,
|
||||
}
|
||||
impl Insert {
|
||||
fn new() -> Insert {
|
||||
@ -20,38 +17,19 @@ impl Insert {
|
||||
|
||||
fn insert(&self, value: Tagged<Value>) -> Result<Tagged<Value>, ShellError> {
|
||||
let value_tag = value.tag();
|
||||
match (value.item, self.value.clone()) {
|
||||
(obj @ Value::Row(_), Some(v)) => match &self.field {
|
||||
Some(f) => match obj.insert_data_at_column_path(value_tag.clone(), f, v) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!(
|
||||
"add could not find place to insert field {:?} {}",
|
||||
obj,
|
||||
f.iter()
|
||||
.map(|i| {
|
||||
match &i.item {
|
||||
Value::Primitive(primitive) => primitive.format(None),
|
||||
_ => String::from(""),
|
||||
}
|
||||
})
|
||||
.join(".")
|
||||
),
|
||||
"column name",
|
||||
&value_tag,
|
||||
))
|
||||
}
|
||||
|
||||
match (&value, &self.value, &self.field) {
|
||||
(
|
||||
obj @ Tagged {
|
||||
item: Value::Row(_),
|
||||
..
|
||||
},
|
||||
None => Err(ShellError::labeled_error(
|
||||
"add needs a column name when adding a value to a table",
|
||||
"column name",
|
||||
value_tag,
|
||||
)),
|
||||
},
|
||||
(value, _) => Err(ShellError::type_error(
|
||||
Some(v),
|
||||
Some(field),
|
||||
) => obj.clone().insert_data_at_column_path(field, v.clone()),
|
||||
(value, ..) => Err(ShellError::type_error(
|
||||
"row",
|
||||
value.type_name().tagged(value_tag),
|
||||
value.type_name().spanned(value_tag),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@ -78,16 +56,21 @@ impl Plugin for Insert {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
match &args[0] {
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
item: Value::Primitive(Primitive::ColumnPath(_)),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(table.as_column_path()?.item);
|
||||
}
|
||||
|
||||
value => return Err(ShellError::type_error("table", value.tagged_type_name())),
|
||||
value => {
|
||||
return Err(ShellError::type_error(
|
||||
"table",
|
||||
value.type_name().spanned(value.span()),
|
||||
))
|
||||
}
|
||||
}
|
||||
match &args[1] {
|
||||
Tagged { item: v, .. } => {
|
||||
v @ Tagged { .. } => {
|
||||
self.value = Some(v.clone());
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
use nu::{
|
||||
did_you_mean, serve_plugin, tag_for_tagged_list, CallInfo, Plugin, Primitive, ReturnSuccess,
|
||||
ReturnValue, ShellError, Signature, SyntaxShape, Tagged, TaggedItem, Value,
|
||||
did_you_mean, serve_plugin, span_for_spanned_list, CallInfo, ColumnPath, Plugin, Primitive,
|
||||
ReturnSuccess, ReturnValue, ShellError, ShellTypeName, Signature, SyntaxShape, Tagged,
|
||||
TaggedItem, Value,
|
||||
};
|
||||
use std::cmp;
|
||||
|
||||
@ -12,10 +13,8 @@ enum Action {
|
||||
Substring(usize, usize),
|
||||
}
|
||||
|
||||
pub type ColumnPath = Tagged<Vec<Tagged<Value>>>;
|
||||
|
||||
struct Str {
|
||||
field: Option<ColumnPath>,
|
||||
field: Option<Tagged<ColumnPath>>,
|
||||
params: Option<Vec<String>>,
|
||||
error: Option<String>,
|
||||
action: Option<Action>,
|
||||
@ -62,7 +61,7 @@ impl Str {
|
||||
Ok(applied)
|
||||
}
|
||||
|
||||
fn for_field(&mut self, column_path: ColumnPath) {
|
||||
fn for_field(&mut self, column_path: Tagged<ColumnPath>) {
|
||||
self.field = Some(column_path);
|
||||
}
|
||||
|
||||
@ -130,55 +129,33 @@ impl Str {
|
||||
Some(ref f) => {
|
||||
let fields = f.clone();
|
||||
|
||||
let replace_for = value.item.get_data_by_column_path(
|
||||
value.tag(),
|
||||
f,
|
||||
Box::new(move |(obj_source, column_path_tried)| {
|
||||
match did_you_mean(&obj_source, &column_path_tried) {
|
||||
Some(suggestions) => {
|
||||
return ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", suggestions[0].1),
|
||||
tag_for_tagged_list(fields.iter().map(|p| p.tag())),
|
||||
)
|
||||
let replace_for =
|
||||
value.get_data_by_column_path(
|
||||
&f,
|
||||
Box::new(move |(obj_source, column_path_tried, error)| {
|
||||
match did_you_mean(&obj_source, &column_path_tried) {
|
||||
Some(suggestions) => {
|
||||
return ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", suggestions[0].1),
|
||||
span_for_spanned_list(fields.iter().map(|p| p.span)),
|
||||
)
|
||||
}
|
||||
None => return error,
|
||||
}
|
||||
None => {
|
||||
return ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
"row does not contain this column",
|
||||
tag_for_tagged_list(fields.iter().map(|p| p.tag())),
|
||||
)
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
let replacement = match replace_for {
|
||||
Ok(got) => match got {
|
||||
Some(result) => self.strutils(result.map(|x| x.clone()))?,
|
||||
None => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"inc could not find field to replace",
|
||||
"column name",
|
||||
value.tag(),
|
||||
))
|
||||
}
|
||||
},
|
||||
Err(reason) => return Err(reason),
|
||||
};
|
||||
let got = replace_for?;
|
||||
let replacement = self.strutils(got.map(|x| x.clone()))?;
|
||||
|
||||
match value.item.replace_data_at_column_path(
|
||||
value.tag(),
|
||||
f,
|
||||
replacement.item.clone(),
|
||||
) {
|
||||
match value.replace_data_at_column_path(&f, replacement.item.clone()) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::type_error(
|
||||
"column name",
|
||||
value.tagged_type_name(),
|
||||
))
|
||||
}
|
||||
None => Err(ShellError::labeled_error(
|
||||
"str could not find field to replace",
|
||||
"column name",
|
||||
value.tag(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
None => Err(ShellError::untagged_runtime_error(format!(
|
||||
@ -189,7 +166,7 @@ impl Str {
|
||||
},
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Unrecognized type in stream",
|
||||
value.type_name(),
|
||||
value.item.type_name(),
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
@ -245,27 +222,9 @@ impl Plugin for Str {
|
||||
}
|
||||
|
||||
if let Some(possible_field) = args.nth(0) {
|
||||
match possible_field {
|
||||
string @ Tagged {
|
||||
item: Value::Primitive(Primitive::String(_)),
|
||||
..
|
||||
} => {
|
||||
self.for_field(string.as_column_path()?);
|
||||
}
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(table.as_column_path()?);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
possible_field.type_name(),
|
||||
&possible_field.tag,
|
||||
))
|
||||
}
|
||||
}
|
||||
let possible_field = possible_field.as_column_path()?;
|
||||
|
||||
self.for_field(possible_field);
|
||||
}
|
||||
for param in args.positional_iter() {
|
||||
match param {
|
||||
@ -303,8 +262,8 @@ mod tests {
|
||||
use super::{Action, Str};
|
||||
use indexmap::IndexMap;
|
||||
use nu::{
|
||||
CallInfo, EvaluatedArgs, Plugin, Primitive, ReturnSuccess, Tag, Tagged, TaggedDictBuilder,
|
||||
TaggedItem, Value,
|
||||
CallInfo, EvaluatedArgs, Plugin, Primitive, RawPathMember, ReturnSuccess, Tag, Tagged,
|
||||
TaggedDictBuilder, TaggedItem, Value,
|
||||
};
|
||||
use num_bigint::BigInt;
|
||||
|
||||
@ -419,14 +378,13 @@ mod tests {
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(
|
||||
plugin.field.map(|f| f
|
||||
.iter()
|
||||
.map(|f| match &f.item {
|
||||
Value::Primitive(Primitive::String(s)) => s.clone(),
|
||||
_ => panic!(""),
|
||||
})
|
||||
.collect()),
|
||||
Some(vec!["package".to_string(), "description".to_string()])
|
||||
plugin
|
||||
.field
|
||||
.map(|f| f.iter().cloned().map(|f| f.item).collect()),
|
||||
Some(vec![
|
||||
RawPathMember::String("package".to_string()),
|
||||
RawPathMember::String("description".to_string())
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
use crossterm::{cursor, terminal, RawScreen};
|
||||
use crossterm::{InputEvent, KeyEvent};
|
||||
use nu::{
|
||||
serve_plugin, AnchorLocation, CallInfo, Plugin, Primitive, ShellError, Signature, Tagged, Value,
|
||||
outln, serve_plugin, AnchorLocation, CallInfo, Plugin, Primitive, ShellError, Signature,
|
||||
Tagged, Value,
|
||||
};
|
||||
|
||||
use syntect::easy::HighlightLines;
|
||||
@ -15,6 +16,7 @@ enum DrawCommand {
|
||||
DrawString(Style, String),
|
||||
NextLine,
|
||||
}
|
||||
|
||||
struct TextView;
|
||||
|
||||
impl TextView {
|
||||
@ -202,7 +204,7 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
|
||||
}
|
||||
}
|
||||
|
||||
println!("");
|
||||
outln!("");
|
||||
}
|
||||
|
||||
fn scroll_view(s: &str) {
|
||||
|
Reference in New Issue
Block a user