Merge pull request #114 from jonathandturner/lines

Add lines and skip-while
This commit is contained in:
Jonathan Turner 2019-06-18 15:47:41 +12:00 committed by GitHub
commit ea326b1aff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 152 additions and 30 deletions

View File

@ -64,6 +64,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("open", open::open), command("open", open::open),
command("enter", enter::enter), command("enter", enter::enter),
command("exit", exit::exit), command("exit", exit::exit),
command("lines", lines::lines),
command("pick", pick::pick), command("pick", pick::pick),
command("split-column", split_column::split_column), command("split-column", split_column::split_column),
command("split-row", split_row::split_row), command("split-row", split_row::split_row),
@ -75,6 +76,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("to-toml", to_toml::to_toml), command("to-toml", to_toml::to_toml),
Arc::new(Where), Arc::new(Where),
Arc::new(Config), Arc::new(Config),
Arc::new(SkipWhile),
command("sort-by", sort_by::sort_by), command("sort-by", sort_by::sort_by),
]); ]);
@ -147,36 +149,41 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
} }
} }
LineResult::Error(mut line, err) => match err { LineResult::Error(mut line, err) => {
ShellError::Diagnostic(diag) => { rl.add_history_entry(line.clone());
let host = context.host.lock().unwrap(); match err {
let writer = host.err_termcolor(); ShellError::Diagnostic(diag) => {
line.push_str(" "); let host = context.host.lock().unwrap();
let files = crate::parser::span::Files::new(line); let writer = host.err_termcolor();
line.push_str(" ");
let files = crate::parser::span::Files::new(line);
language_reporting::emit( language_reporting::emit(
&mut writer.lock(), &mut writer.lock(),
&files, &files,
&diag.diagnostic, &diag.diagnostic,
&language_reporting::DefaultConfig, &language_reporting::DefaultConfig,
) )
.unwrap(); .unwrap();
}
ShellError::TypeError(desc) => context
.host
.lock()
.unwrap()
.stdout(&format!("TypeError: {}", desc)),
ShellError::MissingProperty { subpath, .. } => context
.host
.lock()
.unwrap()
.stdout(&format!("Missing property {}", subpath)),
ShellError::String(_) => {
context.host.lock().unwrap().stdout(&format!("{}", err))
}
} }
}
ShellError::TypeError(desc) => context
.host
.lock()
.unwrap()
.stdout(&format!("TypeError: {}", desc)),
ShellError::MissingProperty { subpath, .. } => context
.host
.lock()
.unwrap()
.stdout(&format!("Missing property {}", subpath)),
ShellError::String(_) => context.host.lock().unwrap().stdout(&format!("{}", err)),
},
LineResult::Break => { LineResult::Break => {
break; break;

View File

@ -14,6 +14,7 @@ crate mod from_toml;
crate mod from_xml; crate mod from_xml;
crate mod from_yaml; crate mod from_yaml;
crate mod get; crate mod get;
crate mod lines;
crate mod ls; crate mod ls;
crate mod open; crate mod open;
crate mod pick; crate mod pick;
@ -22,6 +23,7 @@ crate mod reject;
crate mod save; crate mod save;
crate mod size; crate mod size;
crate mod skip; crate mod skip;
crate mod skip_while;
crate mod sort_by; crate mod sort_by;
crate mod split_column; crate mod split_column;
crate mod split_row; crate mod split_row;
@ -38,3 +40,4 @@ crate use command::command;
crate use config::Config; crate use config::Config;
crate use where_::Where; crate use where_::Where;
crate use skip_while::SkipWhile;

37
src/commands/lines.rs Normal file
View File

@ -0,0 +1,37 @@
use crate::errors::ShellError;
use crate::object::{Primitive, Value};
use crate::prelude::*;
pub fn lines(args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
let span = args.name_span;
let stream = input
.map(move |v| match v {
Value::Primitive(Primitive::String(s)) => {
let split_result: Vec<_> = s.lines().filter(|s| s.trim() != "").collect();
let mut result = VecDeque::new();
for s in split_result {
result.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
s.to_string(),
))));
}
result
}
_ => {
let mut result = VecDeque::new();
result.push_back(ReturnValue::Value(Value::Error(Box::new(
ShellError::maybe_labeled_error(
"Expected string values from pipeline",
"expects strings from pipeline",
span,
),
))));
result
}
})
.flatten();
Ok(stream.boxed())
}

View File

@ -71,8 +71,8 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
), ),
Err(_) => { Err(_) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"File cound not be opened", "File could not be opened",
"file not found", "could not be opened",
args.positional[0].span, args.positional[0].span,
)); ));
} }

View File

@ -0,0 +1,51 @@
use crate::errors::ShellError;
use crate::parser::registry::PositionalType;
use crate::parser::CommandConfig;
use crate::prelude::*;
pub struct SkipWhile;
impl Command for SkipWhile {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
skip_while(args)
}
fn name(&self) -> &str {
"skip-while"
}
fn config(&self) -> CommandConfig {
CommandConfig {
name: self.name().to_string(),
mandatory_positional: vec![PositionalType::Block("condition".to_string())],
optional_positional: vec![],
rest_positional: false,
named: indexmap::IndexMap::new(),
}
}
}
pub fn skip_while(args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.positional.len() == 0 {
return Err(ShellError::maybe_labeled_error(
"Where requires a condition",
"needs condition",
args.name_span,
));
}
let block = args.positional[0].as_block()?;
let input = args.input;
let objects = input.skip_while(move |item| {
let result = block.invoke(&item);
let return_value = match result {
Ok(v) if v.is_true() => true,
_ => false,
};
futures::future::ready(return_value)
});
Ok(objects.map(|x| ReturnValue::Value(x)).boxed())
}

View File

@ -6,6 +6,13 @@ use log::trace;
// TODO: "Amount remaining" wrapper // TODO: "Amount remaining" wrapper
pub fn split_column(args: CommandArgs) -> Result<OutputStream, ShellError> { pub fn split_column(args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.positional.len() == 0 {
return Err(ShellError::maybe_labeled_error(
"Split-column needs more information",
"needs parameter (eg split-column \",\")",
args.name_span,
));
}
let input = args.input; let input = args.input;
let span = args.name_span; let span = args.name_span;
let args = args.positional; let args = args.positional;

View File

@ -6,6 +6,14 @@ use log::trace;
// TODO: "Amount remaining" wrapper // TODO: "Amount remaining" wrapper
pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> { pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.positional.len() == 0 {
return Err(ShellError::maybe_labeled_error(
"Split-row needs more information",
"needs parameter (eg split-row \"\\n\")",
args.name_span,
));
}
let input = args.input; let input = args.input;
let span = args.name_span; let span = args.name_span;
let args = args.positional; let args = args.positional;

1
tests/lines.out Normal file
View File

@ -0,0 +1 @@
rustyline

3
tests/lines.txt Normal file
View File

@ -0,0 +1,3 @@
cd tests
open test.toml --raw | lines | skip-while $it != "[dependencies]" | skip 1 | first 1 | split-column "=" | get Column1 | echo $it
exit

View File

@ -96,4 +96,9 @@ mod tests {
fn enter() { fn enter() {
test_helper("enter"); test_helper("enter");
} }
#[test]
fn lines() {
test_helper("lines");
}
} }