More cleanup

This commit is contained in:
Yehuda Katz 2019-05-15 15:23:36 -07:00
parent 91f7d5384f
commit a5a34b88a8
17 changed files with 53 additions and 128 deletions

View File

@ -1,6 +1,6 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::process::Process; use crate::object::process::Process;
use crate::object::{dir_entry_dict, ShellObject, Value}; use crate::object::{dir_entry_dict, Value};
use crate::prelude::*; use crate::prelude::*;
use crate::Args; use crate::Args;
use derive_new::new; use derive_new::new;

View File

@ -1,6 +1,6 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::process::Process; use crate::object::process::Process;
use crate::object::{dir_entry_dict, ShellObject, Value}; use crate::object::{dir_entry_dict, Value};
use crate::prelude::*; use crate::prelude::*;
use crate::Args; use crate::Args;
use crate::Command; use crate::Command;

View File

@ -1,6 +1,6 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::process::process_dict; use crate::object::process::process_dict;
use crate::object::{ShellObject, Value}; use crate::object::Value;
use crate::prelude::*; use crate::prelude::*;
use crate::Command; use crate::Command;
use derive_new::new; use derive_new::new;

View File

@ -1,7 +1,7 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::base::reject; use crate::object::base::reject;
use crate::object::process::Process; use crate::object::process::Process;
use crate::object::{dir_entry_dict, ShellObject, Value}; use crate::object::{dir_entry_dict, Value};
use crate::prelude::*; use crate::prelude::*;
use crate::Args; use crate::Args;
use derive_new::new; use derive_new::new;

View File

@ -1,7 +1,7 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::base::select; use crate::object::base::select;
use crate::object::process::Process; use crate::object::process::Process;
use crate::object::{dir_entry_dict, ShellObject, Value}; use crate::object::{dir_entry_dict, Value};
use crate::prelude::*; use crate::prelude::*;
use crate::Args; use crate::Args;
use derive_new::new; use derive_new::new;

View File

@ -1,6 +1,6 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::process::Process; use crate::object::process::Process;
use crate::object::{dir_entry_dict, ShellObject, Value}; use crate::object::{dir_entry_dict, Value};
use crate::prelude::*; use crate::prelude::*;
use crate::Args; use crate::Args;
use derive_new::new; use derive_new::new;

View File

@ -1,6 +1,6 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::process::Process; use crate::object::process::Process;
use crate::object::{dir_entry_dict, ShellObject, Value}; use crate::object::{dir_entry_dict, Value};
use crate::prelude::*; use crate::prelude::*;
use crate::Args; use crate::Args;
use derive_new::new; use derive_new::new;

View File

@ -1,5 +1,4 @@
use crate::format::RenderView; use crate::format::RenderView;
use crate::object::base::ToEntriesView;
use crate::prelude::*; use crate::prelude::*;
use crate::Host; use crate::Host;
@ -15,6 +14,23 @@ pub struct EntriesView {
entries: Vec<(String, String)>, entries: Vec<(String, String)>,
} }
impl EntriesView {
crate fn from_value(value: &Value) -> EntriesView {
let descs = value.data_descriptors();
let mut entries = vec![];
for desc in descs {
let value = value.get_data(&desc);
let formatted_value = value.borrow().format_leaf(None);
entries.push((desc.name.clone(), formatted_value))
}
EntriesView::new(entries)
}
}
impl RenderView for EntriesView { impl RenderView for EntriesView {
fn render_view(&self, host: &dyn Host) -> Vec<String> { fn render_view(&self, host: &dyn Host) -> Vec<String> {
if self.entries.len() == 0 { if self.entries.len() == 0 {
@ -51,7 +67,7 @@ impl RenderView for EntriesListView {
let last = self.values.len() - 1; let last = self.values.len() - 1;
for (i, item) in self.values.iter().enumerate() { for (i, item) in self.values.iter().enumerate() {
let view = item.to_entries_view(); let view = EntriesView::from_value(item);
let out = view.render_view(host); let out = view.render_view(host);
strings.extend(out); strings.extend(out);

View File

@ -1,5 +1,4 @@
use crate::format::{RenderView, TableView}; use crate::format::{EntriesView, RenderView, TableView};
use crate::object::base::ToEntriesView;
use crate::object::Value; use crate::object::Value;
use crate::Host; use crate::Host;
use derive_new::new; use derive_new::new;
@ -38,8 +37,8 @@ impl RenderView for GenericView<'value> {
// list // list
} }
Value::Object(o) => { o @ Value::Object(_) => {
let view = o.to_entries_view(); let view = EntriesView::from_value(o);
let out = view.render_view(host); let out = view.render_view(host);
out out
} }

View File

@ -1,5 +1,4 @@
use crate::format::RenderView; use crate::format::RenderView;
use crate::object::ShellObject;
use crate::object::Value; use crate::object::Value;
use crate::Host; use crate::Host;
use derive_new::new; use derive_new::new;

View File

@ -17,9 +17,8 @@ crate use crate::commands::command::{Command, CommandAction, CommandBlueprint};
use crate::context::Context; use crate::context::Context;
crate use crate::env::{Environment, Host}; crate use crate::env::{Environment, Host};
crate use crate::errors::ShellError; crate use crate::errors::ShellError;
crate use crate::format::{EntriesListView, RenderView}; crate use crate::format::{EntriesListView, GenericView, RenderView};
use crate::object::base::{ToEntriesView, ToGenericView}; use crate::object::Value;
use crate::object::{ShellObject, Value};
use ansi_term::Color; use ansi_term::Color;
use conch_parser::lexer::Lexer; use conch_parser::lexer::Lexer;
@ -185,7 +184,7 @@ fn format(input: VecDeque<Value>, context: Arc<Mutex<Context>>) {
let last = input.len() - 1; let last = input.len() - 1;
for (i, item) in input.iter().enumerate() { for (i, item) in input.iter().enumerate() {
let view = item.to_generic_view(); let view = GenericView::new(item);
crate::format::print_rendered(&view.render_view(&ctx.host), &mut ctx.host); crate::format::print_rendered(&view.render_view(&ctx.host), &mut ctx.host);
if last != i { if last != i {

View File

@ -5,7 +5,7 @@ crate mod files;
crate mod process; crate mod process;
crate mod types; crate mod types;
crate use base::{Primitive, ShellObject, Value}; crate use base::{Primitive, Value};
crate use desc::{DataDescriptor, DataDescriptorInstance}; crate use desc::{DataDescriptor, DataDescriptorInstance};
crate use dict::Dictionary; crate use dict::Dictionary;
crate use files::dir_entry_dict; crate use files::dir_entry_dict;

View File

@ -1,6 +1,7 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::format::{EntriesView, GenericView}; use crate::format::{EntriesView, GenericView};
use crate::object::desc::DataDescriptor; use crate::object::desc::DataDescriptor;
use ansi_term::Color;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use chrono_humanize::Humanize; use chrono_humanize::Humanize;
use std::fmt::Debug; use std::fmt::Debug;
@ -20,9 +21,14 @@ pub enum Primitive {
impl Primitive { impl Primitive {
crate fn format(&self, field_name: Option<&str>) -> String { crate fn format(&self, field_name: Option<&str>) -> String {
match self { match self {
Primitive::Nothing => format!("Nothing"), Primitive::Nothing => format!("{}", Color::Black.bold().paint("-")),
Primitive::Bytes(b) => { Primitive::Bytes(b) => {
let byte = byte_unit::Byte::from_bytes(*b); let byte = byte_unit::Byte::from_bytes(*b);
if byte.get_bytes() == 0u128 {
return Color::Black.bold().paint("Empty".to_string()).to_string();
}
let byte = byte.get_appropriate_unit(true); let byte = byte.get_appropriate_unit(true);
match byte.get_unit() { match byte.get_unit() {
@ -39,11 +45,7 @@ impl Primitive {
(true, Some(s)) => format!("{}", s), (true, Some(s)) => format!("{}", s),
(false, Some(s)) => format!(""), (false, Some(s)) => format!(""),
}, },
Primitive::Date(d) => { Primitive::Date(d) => format!("{}", d.humanize()),
// let date = d.format("%-m/%-d/%-y");
// let time =
format!("{}", d.humanize())
}
} }
} }
} }
@ -56,17 +58,17 @@ pub enum Value {
Error(Box<ShellError>), Error(Box<ShellError>),
} }
impl ShellObject for Value { impl Value {
fn to_shell_string(&self) -> String { crate fn to_shell_string(&self) -> String {
match self { match self {
Value::Primitive(p) => p.format(None), Value::Primitive(p) => p.format(None),
Value::Object(o) => o.to_shell_string(), Value::Object(o) => format!("[object Object]"),
Value::List(l) => format!("[list List]"), Value::List(l) => format!("[list List]"),
Value::Error(e) => format!("{}", e), Value::Error(e) => format!("{}", e),
} }
} }
fn data_descriptors(&self) -> Vec<DataDescriptor> { crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
match self { match self {
Value::Primitive(p) => vec![], Value::Primitive(p) => vec![],
Value::Object(o) => o.data_descriptors(), Value::Object(o) => o.data_descriptors(),
@ -75,7 +77,7 @@ impl ShellObject for Value {
} }
} }
fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value> { crate fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value> {
match self { match self {
Value::Primitive(p) => crate::MaybeOwned::Owned(Value::nothing()), Value::Primitive(p) => crate::MaybeOwned::Owned(Value::nothing()),
Value::Object(o) => o.get_data(desc), Value::Object(o) => o.get_data(desc),
@ -84,7 +86,7 @@ impl ShellObject for Value {
} }
} }
fn copy(&self) -> Value { crate fn copy(&self) -> Value {
match self { match self {
Value::Primitive(p) => Value::Primitive(p.clone()), Value::Primitive(p) => Value::Primitive(p.clone()),
Value::Object(o) => Value::Object(o.copy_dict()), Value::Object(o) => Value::Object(o.copy_dict()),
@ -95,24 +97,7 @@ impl ShellObject for Value {
Value::Error(e) => Value::Error(Box::new(e.copy_error())), Value::Error(e) => Value::Error(Box::new(e.copy_error())),
} }
} }
}
impl ShellObject for &Value {
fn to_shell_string(&self) -> String {
(*self).to_shell_string()
}
fn data_descriptors(&self) -> Vec<DataDescriptor> {
(*self).data_descriptors()
}
fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value> {
(*self).get_data(desc)
}
fn copy(&self) -> Value {
(*self).copy()
}
}
impl Value {
crate fn format_leaf(&self, field_name: Option<&str>) -> String { crate fn format_leaf(&self, field_name: Option<&str>) -> String {
match self { match self {
Value::Primitive(p) => p.format(field_name), Value::Primitive(p) => p.format(field_name),
@ -181,14 +166,7 @@ impl Value {
} }
} }
pub trait ShellObject: Debug { crate fn select(obj: &Value, fields: &[String]) -> crate::object::Dictionary {
fn to_shell_string(&self) -> String;
fn data_descriptors(&self) -> Vec<DataDescriptor>;
fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value>;
fn copy(&self) -> Value;
}
crate fn select(obj: impl ShellObject, fields: &[String]) -> crate::object::Dictionary {
let mut out = crate::object::Dictionary::default(); let mut out = crate::object::Dictionary::default();
let descs = obj.data_descriptors(); let descs = obj.data_descriptors();
@ -203,7 +181,7 @@ crate fn select(obj: impl ShellObject, fields: &[String]) -> crate::object::Dict
out out
} }
crate fn reject(obj: impl ShellObject, fields: &[String]) -> crate::object::Dictionary { crate fn reject(obj: &Value, fields: &[String]) -> crate::object::Dictionary {
let mut out = crate::object::Dictionary::default(); let mut out = crate::object::Dictionary::default();
let descs = obj.data_descriptors(); let descs = obj.data_descriptors();
@ -218,59 +196,3 @@ crate fn reject(obj: impl ShellObject, fields: &[String]) -> crate::object::Dict
out out
} }
pub trait ToEntriesView {
fn to_entries_view(&self) -> EntriesView;
}
impl<T> ToEntriesView for T
where
T: ShellObject,
{
fn to_entries_view(&self) -> EntriesView {
let descs = self.data_descriptors();
let mut entries = vec![];
for desc in descs {
let value = self.get_data(&desc);
let formatted_value = match value.borrow() {
Value::Primitive(p) => p.format(None),
Value::Object(o) => format!("[object Object]"),
Value::List(l) => format!("[object List]"),
Value::Error(e) => format!("{}", e),
};
entries.push((desc.name.clone(), formatted_value))
}
EntriesView::new(entries)
}
}
impl ShellObject for Box<dyn ShellObject> {
fn to_shell_string(&self) -> String {
(**self).to_shell_string()
}
fn data_descriptors(&self) -> Vec<DataDescriptor> {
(**self).data_descriptors()
}
fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value> {
(**self).get_data(desc)
}
fn copy(&self) -> Value {
(**self).copy()
}
}
pub trait ToGenericView {
fn to_generic_view(&self) -> GenericView;
}
impl ToGenericView for Value {
fn to_generic_view(&self) -> GenericView<'_> {
GenericView::new(self)
}
}

View File

@ -26,14 +26,8 @@ impl Dictionary {
out out
} }
}
impl crate::object::ShellObject for Dictionary { crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
fn to_shell_string(&self) -> String {
format!("[object Object] lol")
}
fn data_descriptors(&self) -> Vec<DataDescriptor> {
self.entries self.entries
.iter() .iter()
.map(|(name, value)| { .map(|(name, value)| {
@ -42,14 +36,10 @@ impl crate::object::ShellObject for Dictionary {
.collect() .collect()
} }
fn get_data(&'a self, desc: &DataDescriptor) -> MaybeOwned<'a, Value> { crate fn get_data(&'a self, desc: &DataDescriptor) -> MaybeOwned<'a, Value> {
match self.entries.get(&desc.name) { match self.entries.get(&desc.name) {
Some(v) => MaybeOwned::Borrowed(v), Some(v) => MaybeOwned::Borrowed(v),
None => MaybeOwned::Owned(Value::Primitive(Primitive::Nothing)), None => MaybeOwned::Owned(Value::Primitive(Primitive::Nothing)),
} }
} }
fn copy(&self) -> Value {
Value::Object(self.copy_dict())
}
} }

View File

@ -1,5 +1,5 @@
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::object::{DataDescriptor, Dictionary, ShellObject, Value}; use crate::object::{DataDescriptor, Dictionary, Value};
use crate::MaybeOwned; use crate::MaybeOwned;
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,4 +1,4 @@
use crate::object::base::{Primitive, ShellObject, Value}; use crate::object::base::{Primitive, Value};
use crate::object::desc::DataDescriptor; use crate::object::desc::DataDescriptor;
use crate::object::dict::Dictionary; use crate::object::dict::Dictionary;
use crate::MaybeOwned; use crate::MaybeOwned;

View File

@ -3,5 +3,5 @@ crate use crate::commands::command::{Command, CommandAction, CommandBlueprint, R
crate use crate::env::{Environment, Host}; crate use crate::env::{Environment, Host};
crate use crate::errors::ShellError; crate use crate::errors::ShellError;
crate use crate::format::RenderView; crate use crate::format::RenderView;
crate use crate::object::{Primitive, ShellObject, Value}; crate use crate::object::{Primitive, Value};
crate use std::collections::VecDeque; crate use std::collections::VecDeque;