From bd055f2af179c53f3060e7c1b99f85bede3cde76 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 24 May 2019 12:35:22 -0700 Subject: [PATCH] Linting and other cleanup --- src/cli.rs | 37 +++++---------------------- src/commands/classified.rs | 52 ++------------------------------------ src/commands/format.rs | 16 ++++++++++++ src/env/host.rs | 13 ++++++++++ src/format/entries.rs | 2 +- src/format/generic.rs | 4 +-- src/format/table.rs | 4 --- src/object/base.rs | 4 +-- src/object/dict.rs | 2 +- src/object/files.rs | 1 - src/prelude.rs | 7 +++-- 11 files changed, 46 insertions(+), 96 deletions(-) create mode 100644 src/commands/format.rs diff --git a/src/cli.rs b/src/cli.rs index cee37ef4e7..ca644a03d0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -210,10 +210,6 @@ async fn process_line(readline: Result, ctx: &mut Context } } } - // input = match process_command(item.clone(), input, ctx).await { - // Ok(val) => val, - // Err(err) => return LineResult::Error(format!("{}", err.description())), - // }; } let input_vec: VecDeque<_> = input.objects.collect().await; @@ -248,16 +244,6 @@ async fn process_line(readline: Result, ctx: &mut Context } } -// async fn process_command( -// parsed: Vec, -// input: InputStream, -// context: &mut Context, -// ) -> Result { -// let command = classify_command(&parsed, context)?; - -// command.run(context, input).await -// } - fn classify_command( command: &[crate::parser::Item], context: &Context, @@ -295,10 +281,11 @@ crate fn format(args: CommandArgs) -> OutputStream { let mut host = host.lock().unwrap(); for (i, item) in input.iter().enumerate() { let view = GenericView::new(item); - crate::format::print_view(&view, &mut *host); + + handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host)); if last != i { - println!(""); + host.stdout(""); } } @@ -310,31 +297,19 @@ crate fn format(args: CommandArgs) -> OutputStream { crate fn format_list(args: CommandArgs) -> Result { let host = args.host.clone(); - // let input = args.input.map(|a| a.copy()); - // let input = input.collect::>(); let view = EntriesListView::from_stream(args.input); Ok(view .then(move |view| { - crate::format::print_view(&view, &mut *host.lock().unwrap()); + handle_unexpected(&mut *host.lock().unwrap(), |host| { + crate::format::print_view(&view, host) + }); futures::future::ready(empty_stream()) }) .flatten_stream() .boxed()) - - // Ok(empty_stream()) - // Ok(args - // .input - // .map(|input| { - // let view = EntriesListView::from_stream(input); - // crate::format::print_view(&view, &mut *args.host.lock().unwrap()); - // }) - // .collect() - // .then(|_| empty_stream()) - // .flatten_stream() - // .boxed()) } fn equal_shapes(input: &VecDeque) -> bool { diff --git a/src/commands/classified.rs b/src/commands/classified.rs index 5778e647cf..5d0b48fc2e 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -1,5 +1,4 @@ use crate::prelude::*; -use futures::compat::AsyncRead01CompatExt; use futures_codec::{Framed, LinesCodec}; use std::sync::Arc; use subprocess::Exec; @@ -37,44 +36,6 @@ crate enum ClassifiedCommand { External(ExternalCommand), } -impl ClassifiedCommand { - crate async fn run( - self, - context: &mut Context, - input: ClassifiedInputStream, - ) -> Result { - match self { - ClassifiedCommand::Internal(internal) => { - let result = context.run_command(internal.command, internal.args, input.objects)?; - let env = context.env.clone(); - - let stream = result.filter_map(move |v| match v { - ReturnValue::Action(action) => match action { - CommandAction::ChangeCwd(cwd) => { - env.lock().unwrap().cwd = cwd; - futures::future::ready(None) - } - }, - - ReturnValue::Value(v) => futures::future::ready(Some(v)), - }); - - Ok(stream.boxed() as InputStream) - } - - ClassifiedCommand::External(external) => { - Exec::shell(&external.name) - .args(&external.args) - .cwd(context.env.lock().unwrap().cwd()) - .join() - .unwrap(); - - Ok(VecDeque::new().boxed() as InputStream) - } - } - } -} - crate struct InternalCommand { crate command: Arc, crate args: Vec, @@ -119,10 +80,10 @@ impl ExternalCommand { crate async fn run( self, context: &mut Context, - mut input: ClassifiedInputStream, + input: ClassifiedInputStream, stream_next: StreamNext, ) -> Result { - let mut process = Exec::shell(&self.name) + let process = Exec::shell(&self.name) .args(&self.args) .cwd(context.env.lock().unwrap().cwd()); @@ -156,14 +117,5 @@ impl ExternalCommand { Ok(ClassifiedInputStream::from_input_stream(stream.boxed())) } } - - // if stream_next { - // let stdout = popen.stdout.take().unwrap(); - // Ok(ClassifiedInputStream::from_stdout(stdout)) - // } else { - // // popen.stdin.take(); - // popen.wait()?; - // Ok(ClassifiedInputStream::new()) - // } } } diff --git a/src/commands/format.rs b/src/commands/format.rs new file mode 100644 index 0000000000..02c32dabd6 --- /dev/null +++ b/src/commands/format.rs @@ -0,0 +1,16 @@ +use crate::prelude::*; +use crate::{EntriesListView, GenericView}; +use futures::stream::{self, StreamExt}; +use std::sync::{Arc, Mutex}; + +crate fn format(input: Vec, host: &mut dyn Host) { + let last = input.len() - 1; + for (i, item) in input.iter().enumerate() { + let view = GenericView::new(item); + crate::format::print_view(&view, &mut *host); + + if last != i { + println!(""); + } + } +} diff --git a/src/env/host.rs b/src/env/host.rs index 57f6064181..b452af154f 100644 --- a/src/env/host.rs +++ b/src/env/host.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + pub trait Host { fn out_terminal(&self) -> Box; fn err_terminal(&self) -> Box; @@ -49,3 +51,14 @@ impl Host for BasicHost { } } } + +crate fn handle_unexpected( + host: &mut dyn Host, + func: impl FnOnce(&mut dyn Host) -> Result, +) { + let result = func(host); + + if let Err(err) = result { + host.stderr(&format!("Something unexpected happened:\n{:?}", err)); + } +} diff --git a/src/format/entries.rs b/src/format/entries.rs index 8f9ee83936..773c0ba1ee 100644 --- a/src/format/entries.rs +++ b/src/format/entries.rs @@ -78,7 +78,7 @@ impl RenderView for EntriesListView { for (i, item) in self.values.iter().enumerate() { let view = EntriesView::from_value(item); - view.render_view(host); + view.render_view(host)?; if i != last { host.stdout("\n"); diff --git a/src/format/generic.rs b/src/format/generic.rs index 45f78af8df..02383dd000 100644 --- a/src/format/generic.rs +++ b/src/format/generic.rs @@ -17,7 +17,7 @@ impl RenderView for GenericView<'value> { let view = TableView::from_list(l); if let Some(view) = view { - view.render_view(host); + view.render_view(host)?; } Ok(()) @@ -39,7 +39,7 @@ impl RenderView for GenericView<'value> { o @ Value::Object(_) => { let view = EntriesView::from_value(o); - view.render_view(host); + view.render_view(host)?; Ok(()) } diff --git a/src/format/table.rs b/src/format/table.rs index 1981018ef7..a7436a87f5 100644 --- a/src/format/table.rs +++ b/src/format/table.rs @@ -1,7 +1,6 @@ use crate::format::RenderView; use crate::object::Value; use crate::prelude::*; -use ansi_term::Color; use derive_new::new; use prettytable::{color, Attr, Cell, Row, Table}; @@ -51,9 +50,6 @@ impl RenderView for TableView { let mut table = Table::new(); - // let format = prettytable::format::FormatBuilder::new(); - // .column_separator(Color::Black.bold().paint("|")); - table.set_format(*prettytable::format::consts::FORMAT_NO_COLSEP); let header: Vec = self diff --git a/src/object/base.rs b/src/object/base.rs index d915844346..f708b0f044 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -1,5 +1,5 @@ use crate::errors::ShellError; -use crate::object::{DataDescriptor, DescriptorName}; +use crate::object::DataDescriptor; use crate::parser::parse::Operator; use crate::prelude::*; use ansi_term::Color; @@ -221,7 +221,7 @@ crate fn reject_fields(obj: &Value, fields: &[String]) -> crate::object::Diction match desc.name.as_string() { None => continue, Some(s) if fields.iter().any(|field| field == s) => continue, - Some(s) => out.add(desc.copy(), obj.get_data(&desc).borrow().copy()), + Some(_) => out.add(desc.copy(), obj.get_data(&desc).borrow().copy()), } } diff --git a/src/object/dict.rs b/src/object/dict.rs index 1e2379304b..2b2e4b7cc1 100644 --- a/src/object/dict.rs +++ b/src/object/dict.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use crate::object::{DataDescriptor, DescriptorName}; +use crate::object::DataDescriptor; use crate::object::{Primitive, Value}; use indexmap::IndexMap; use std::cmp::{Ordering, PartialOrd}; diff --git a/src/object/files.rs b/src/object/files.rs index 55faf6d451..b42a7ce3bb 100644 --- a/src/object/files.rs +++ b/src/object/files.rs @@ -14,7 +14,6 @@ crate fn dir_entry_dict(entry: &std::fs::DirEntry) -> Result