Linting and other cleanup

This commit is contained in:
Yehuda Katz 2019-05-24 12:35:22 -07:00
parent 9f8d2a4de5
commit bd055f2af1
11 changed files with 46 additions and 96 deletions

View File

@ -210,10 +210,6 @@ async fn process_line(readline: Result<String, ReadlineError>, 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<String, ReadlineError>, ctx: &mut Context
}
}
// async fn process_command(
// parsed: Vec<crate::parser::Item>,
// input: InputStream,
// context: &mut Context,
// ) -> Result<InputStream, ShellError> {
// 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<OutputStream, ShellError> {
let host = args.host.clone();
// let input = args.input.map(|a| a.copy());
// let input = input.collect::<Vec<_>>();
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<Value>) -> bool {

View File

@ -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<InputStream, ShellError> {
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<dyn Command>,
crate args: Vec<Value>,
@ -119,10 +80,10 @@ impl ExternalCommand {
crate async fn run(
self,
context: &mut Context,
mut input: ClassifiedInputStream,
input: ClassifiedInputStream,
stream_next: StreamNext,
) -> Result<ClassifiedInputStream, ShellError> {
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())
// }
}
}

16
src/commands/format.rs Normal file
View File

@ -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<Value>, 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!("");
}
}
}

13
src/env/host.rs vendored
View File

@ -1,3 +1,5 @@
use crate::prelude::*;
pub trait Host {
fn out_terminal(&self) -> Box<term::StdoutTerminal>;
fn err_terminal(&self) -> Box<term::StderrTerminal>;
@ -49,3 +51,14 @@ impl Host for BasicHost {
}
}
}
crate fn handle_unexpected<T>(
host: &mut dyn Host,
func: impl FnOnce(&mut dyn Host) -> Result<T, ShellError>,
) {
let result = func(host);
if let Err(err) = result {
host.stderr(&format!("Something unexpected happened:\n{:?}", err));
}
}

View File

@ -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");

View File

@ -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(())
}

View File

@ -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<Cell> = self

View File

@ -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()),
}
}

View File

@ -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};

View File

@ -14,7 +14,6 @@ crate fn dir_entry_dict(entry: &std::fs::DirEntry) -> Result<Dictionary, ShellEr
dict.add("file name", Value::string(filename.to_string_lossy()));
let metadata = entry.metadata()?;
// let file_type = inner.file_type()?;
let kind = if metadata.is_dir() {
FileType::Directory

View File

@ -1,13 +1,12 @@
crate use crate::cli::MaybeOwned;
crate use crate::commands::command::{Command, CommandAction, CommandArgs, ReturnValue};
crate use crate::context::Context;
crate use crate::env::host::handle_unexpected;
crate use crate::env::{Environment, Host};
crate use crate::errors::ShellError;
crate use crate::object::{Primitive, Value};
#[allow(unused)]
crate use crate::stream::{empty_stream, single_output, InputStream, OutputStream};
#[allow(unused)]
crate use futures::{Future, FutureExt, Sink, SinkExt, Stream, StreamExt};
crate use crate::stream::{single_output, InputStream, OutputStream};
crate use futures::{FutureExt, SinkExt, StreamExt};
crate use std::collections::VecDeque;
crate use std::pin::Pin;
crate use std::sync::{Arc, Mutex};