Merge pull request #945 from jonathandturner/format

Format
This commit is contained in:
Jonathan Turner 2019-11-09 16:39:51 -08:00 committed by GitHub
commit 6bbfd0f4f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 2 deletions

View File

@ -137,6 +137,10 @@ path = "src/plugins/insert.rs"
name = "nu_plugin_edit" name = "nu_plugin_edit"
path = "src/plugins/edit.rs" path = "src/plugins/edit.rs"
[[bin]]
name = "nu_plugin_format"
path = "src/plugins/format.rs"
[[bin]] [[bin]]
name = "nu_plugin_parse" name = "nu_plugin_parse"
path = "src/plugins/parse.rs" path = "src/plugins/parse.rs"

View File

@ -253,6 +253,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
| edit column-or-column-path value | Edit an existing column to have a new value | | edit column-or-column-path value | Edit an existing column to have a new value |
| embed column | Creates a new table of one column with the given name, and places the current table inside of it | | embed column | Creates a new table of one column with the given name, and places the current table inside of it |
| first amount | Show only the first number of rows | | first amount | Show only the first number of rows |
| format pattern | Format table row data as a string following the given pattern |
| get column-or-column-path | Open column and get data from the corresponding cells | | get column-or-column-path | Open column and get data from the corresponding cells |
| group-by column | Creates a new table with the data from the table rows grouped by the column given | | group-by column | Creates a new table with the data from the table rows grouped by the column given |
| inc (column-or-column-path) | Increment a value or version. Optionally use the column of a table | | inc (column-or-column-path) | Increment a value or version. Optionally use the column of a table |

View File

@ -422,7 +422,7 @@ impl Tagged<Value> {
} }
} }
pub(crate) fn as_string(&self) -> Result<String, ShellError> { pub fn as_string(&self) -> Result<String, ShellError> {
match &self.item { match &self.item {
Value::Primitive(Primitive::String(s)) => Ok(s.clone()), Value::Primitive(Primitive::String(s)) => Ok(s.clone()),
Value::Primitive(Primitive::Boolean(x)) => Ok(format!("{}", x)), Value::Primitive(Primitive::Boolean(x)) => Ok(format!("{}", x)),

128
src/plugins/format.rs Normal file
View File

@ -0,0 +1,128 @@
use nu::{
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature,
SyntaxShape, Tagged, TaggedItem, Value,
};
use nom::{
bytes::complete::{tag, take_while},
IResult,
};
#[derive(Debug)]
enum FormatCommand {
Text(String),
Column(String),
}
fn format(input: &str) -> IResult<&str, Vec<FormatCommand>> {
let mut output = vec![];
let mut loop_input = input;
loop {
let (input, before) = take_while(|c| c != '{')(loop_input)?;
if before.len() > 0 {
output.push(FormatCommand::Text(before.to_string()));
}
if input != "" {
// Look for column as we're now at one
let (input, _) = tag("{")(input)?;
let (input, column) = take_while(|c| c != '}')(input)?;
let (input, _) = tag("}")(input)?;
output.push(FormatCommand::Column(column.to_string()));
loop_input = input;
} else {
loop_input = input;
}
if loop_input == "" {
break;
}
}
Ok((loop_input, output))
}
struct Format {
commands: Vec<FormatCommand>,
}
impl Format {
fn new() -> Self {
Format { commands: vec![] }
}
}
impl Plugin for Format {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("format")
.desc("Format columns into a string using a simple pattern")
.required(
"pattern",
SyntaxShape::Any,
"the pattern to match. Eg) \"{foo}: {bar}\"",
)
.filter())
}
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
if let Some(args) = call_info.args.positional {
match &args[0] {
Tagged {
item: Value::Primitive(Primitive::String(pattern)),
..
} => {
let format_pattern = format(&pattern).unwrap();
self.commands = format_pattern.1
}
Tagged { tag, .. } => {
return Err(ShellError::labeled_error(
"Unrecognized type in params",
"expected a string",
tag,
));
}
}
}
Ok(vec![])
}
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
match &input {
Tagged {
item: Value::Row(dict),
..
} => {
let mut output = String::new();
for command in &self.commands {
match command {
FormatCommand::Text(s) => {
output.push_str(s);
}
FormatCommand::Column(c) => {
match dict.entries.get(c) {
Some(c) => match c.as_string() {
Ok(v) => output.push_str(&v),
_ => return Ok(vec![]),
},
None => {
// This row doesn't match, so don't emit anything
return Ok(vec![]);
}
}
}
}
}
return Ok(vec![ReturnSuccess::value(
Value::string(output).tagged_unknown(),
)]);
}
_ => {}
}
Ok(vec![])
}
}
fn main() {
serve_plugin(&mut Format::new());
}

View File

@ -57,7 +57,7 @@ fn insert_plugin() {
} }
#[test] #[test]
fn read_plugin() { fn parse_plugin() {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", h::pipeline( cwd: "tests/fixtures/formats", h::pipeline(
r#" r#"
@ -72,6 +72,21 @@ fn read_plugin() {
assert_eq!(actual, "StupidLongName"); assert_eq!(actual, "StupidLongName");
} }
#[test]
fn format_plugin() {
let actual = nu!(
cwd: "tests/fixtures/formats", h::pipeline(
r#"
open cargo_sample.toml
| get package
| format "{name} has license {license}"
| echo $it
"#
));
assert_eq!(actual, "nu has license ISC");
}
#[test] #[test]
fn prepend_plugin() { fn prepend_plugin() {
let actual = nu!( let actual = nu!(