diff --git a/src/commands/cd.rs b/src/commands/cd.rs index 265d347c04..faab467ed5 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -1,6 +1,6 @@ use crate::errors::ShellError; 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::Args; use derive_new::new; diff --git a/src/commands/ls.rs b/src/commands/ls.rs index bd6fc7f617..8cc59a5f3b 100644 --- a/src/commands/ls.rs +++ b/src/commands/ls.rs @@ -1,6 +1,6 @@ use crate::errors::ShellError; 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::Args; use crate::Command; diff --git a/src/commands/ps.rs b/src/commands/ps.rs index 17fe91bc9f..0504aefaee 100644 --- a/src/commands/ps.rs +++ b/src/commands/ps.rs @@ -1,6 +1,6 @@ use crate::errors::ShellError; use crate::object::process::process_dict; -use crate::object::{ShellObject, Value}; +use crate::object::Value; use crate::prelude::*; use crate::Command; use derive_new::new; diff --git a/src/commands/reject.rs b/src/commands/reject.rs index fb4c45b20a..fa394f93d9 100644 --- a/src/commands/reject.rs +++ b/src/commands/reject.rs @@ -1,7 +1,7 @@ use crate::errors::ShellError; use crate::object::base::reject; 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::Args; use derive_new::new; diff --git a/src/commands/select.rs b/src/commands/select.rs index 325b857875..37a2b2b0a5 100644 --- a/src/commands/select.rs +++ b/src/commands/select.rs @@ -1,7 +1,7 @@ use crate::errors::ShellError; use crate::object::base::select; 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::Args; use derive_new::new; diff --git a/src/commands/take.rs b/src/commands/take.rs index 017c9609e0..014f73ce53 100644 --- a/src/commands/take.rs +++ b/src/commands/take.rs @@ -1,6 +1,6 @@ use crate::errors::ShellError; 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::Args; use derive_new::new; diff --git a/src/commands/to_array.rs b/src/commands/to_array.rs index d56461f64e..852af14a9a 100644 --- a/src/commands/to_array.rs +++ b/src/commands/to_array.rs @@ -1,6 +1,6 @@ use crate::errors::ShellError; 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::Args; use derive_new::new; diff --git a/src/format/entries.rs b/src/format/entries.rs index 99816b1fdb..1f6e22379f 100644 --- a/src/format/entries.rs +++ b/src/format/entries.rs @@ -1,5 +1,4 @@ use crate::format::RenderView; -use crate::object::base::ToEntriesView; use crate::prelude::*; use crate::Host; @@ -15,6 +14,23 @@ pub struct EntriesView { 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 { fn render_view(&self, host: &dyn Host) -> Vec { if self.entries.len() == 0 { @@ -51,7 +67,7 @@ impl RenderView for EntriesListView { let last = self.values.len() - 1; 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); strings.extend(out); diff --git a/src/format/generic.rs b/src/format/generic.rs index 85b549102d..7ba3b132dd 100644 --- a/src/format/generic.rs +++ b/src/format/generic.rs @@ -1,5 +1,4 @@ -use crate::format::{RenderView, TableView}; -use crate::object::base::ToEntriesView; +use crate::format::{EntriesView, RenderView, TableView}; use crate::object::Value; use crate::Host; use derive_new::new; @@ -38,8 +37,8 @@ impl RenderView for GenericView<'value> { // list } - Value::Object(o) => { - let view = o.to_entries_view(); + o @ Value::Object(_) => { + let view = EntriesView::from_value(o); let out = view.render_view(host); out } diff --git a/src/format/table.rs b/src/format/table.rs index be24b5ae80..c388f3545b 100644 --- a/src/format/table.rs +++ b/src/format/table.rs @@ -1,5 +1,4 @@ use crate::format::RenderView; -use crate::object::ShellObject; use crate::object::Value; use crate::Host; use derive_new::new; diff --git a/src/main.rs b/src/main.rs index 5c1bf3630a..bd9efcca1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,9 +17,8 @@ crate use crate::commands::command::{Command, CommandAction, CommandBlueprint}; use crate::context::Context; crate use crate::env::{Environment, Host}; crate use crate::errors::ShellError; -crate use crate::format::{EntriesListView, RenderView}; -use crate::object::base::{ToEntriesView, ToGenericView}; -use crate::object::{ShellObject, Value}; +crate use crate::format::{EntriesListView, GenericView, RenderView}; +use crate::object::Value; use ansi_term::Color; use conch_parser::lexer::Lexer; @@ -185,7 +184,7 @@ fn format(input: VecDeque, context: Arc>) { let last = input.len() - 1; 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); if last != i { diff --git a/src/object.rs b/src/object.rs index 6dea6ec880..be6337be4d 100644 --- a/src/object.rs +++ b/src/object.rs @@ -5,7 +5,7 @@ crate mod files; crate mod process; crate mod types; -crate use base::{Primitive, ShellObject, Value}; +crate use base::{Primitive, Value}; crate use desc::{DataDescriptor, DataDescriptorInstance}; crate use dict::Dictionary; crate use files::dir_entry_dict; diff --git a/src/object/base.rs b/src/object/base.rs index a81606a6b1..597f865fb6 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -1,6 +1,7 @@ use crate::errors::ShellError; use crate::format::{EntriesView, GenericView}; use crate::object::desc::DataDescriptor; +use ansi_term::Color; use chrono::{DateTime, Utc}; use chrono_humanize::Humanize; use std::fmt::Debug; @@ -20,9 +21,14 @@ pub enum Primitive { impl Primitive { crate fn format(&self, field_name: Option<&str>) -> String { match self { - Primitive::Nothing => format!("Nothing"), + Primitive::Nothing => format!("{}", Color::Black.bold().paint("-")), Primitive::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); match byte.get_unit() { @@ -39,11 +45,7 @@ impl Primitive { (true, Some(s)) => format!("{}", s), (false, Some(s)) => format!(""), }, - Primitive::Date(d) => { - // let date = d.format("%-m/%-d/%-y"); - // let time = - format!("{}", d.humanize()) - } + Primitive::Date(d) => format!("{}", d.humanize()), } } } @@ -56,17 +58,17 @@ pub enum Value { Error(Box), } -impl ShellObject for Value { - fn to_shell_string(&self) -> String { +impl Value { + crate fn to_shell_string(&self) -> String { match self { 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::Error(e) => format!("{}", e), } } - fn data_descriptors(&self) -> Vec { + crate fn data_descriptors(&self) -> Vec { match self { Value::Primitive(p) => vec![], 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 { Value::Primitive(p) => crate::MaybeOwned::Owned(Value::nothing()), 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 { Value::Primitive(p) => Value::Primitive(p.clone()), 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())), } } -} -impl ShellObject for &Value { - fn to_shell_string(&self) -> String { - (*self).to_shell_string() - } - fn data_descriptors(&self) -> Vec { - (*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 { match self { Value::Primitive(p) => p.format(field_name), @@ -181,14 +166,7 @@ impl Value { } } -pub trait ShellObject: Debug { - fn to_shell_string(&self) -> String; - fn data_descriptors(&self) -> Vec; - 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 { +crate fn select(obj: &Value, fields: &[String]) -> crate::object::Dictionary { let mut out = crate::object::Dictionary::default(); let descs = obj.data_descriptors(); @@ -203,7 +181,7 @@ crate fn select(obj: impl ShellObject, fields: &[String]) -> crate::object::Dict 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 descs = obj.data_descriptors(); @@ -218,59 +196,3 @@ crate fn reject(obj: impl ShellObject, fields: &[String]) -> crate::object::Dict out } - -pub trait ToEntriesView { - fn to_entries_view(&self) -> EntriesView; -} - -impl 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 { - fn to_shell_string(&self) -> String { - (**self).to_shell_string() - } - fn data_descriptors(&self) -> Vec { - (**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) - } -} diff --git a/src/object/dict.rs b/src/object/dict.rs index c135ec7ddc..98e315ca51 100644 --- a/src/object/dict.rs +++ b/src/object/dict.rs @@ -26,14 +26,8 @@ impl Dictionary { out } -} -impl crate::object::ShellObject for Dictionary { - fn to_shell_string(&self) -> String { - format!("[object Object] lol") - } - - fn data_descriptors(&self) -> Vec { + crate fn data_descriptors(&self) -> Vec { self.entries .iter() .map(|(name, value)| { @@ -42,14 +36,10 @@ impl crate::object::ShellObject for Dictionary { .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) { Some(v) => MaybeOwned::Borrowed(v), None => MaybeOwned::Owned(Value::Primitive(Primitive::Nothing)), } } - - fn copy(&self) -> Value { - Value::Object(self.copy_dict()) - } } diff --git a/src/object/files.rs b/src/object/files.rs index f22ec55120..45b4086b37 100644 --- a/src/object/files.rs +++ b/src/object/files.rs @@ -1,5 +1,5 @@ use crate::errors::ShellError; -use crate::object::{DataDescriptor, Dictionary, ShellObject, Value}; +use crate::object::{DataDescriptor, Dictionary, Value}; use crate::MaybeOwned; #[derive(Debug)] diff --git a/src/object/process.rs b/src/object/process.rs index abea95fdcf..fe63ff5404 100644 --- a/src/object/process.rs +++ b/src/object/process.rs @@ -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::dict::Dictionary; use crate::MaybeOwned; diff --git a/src/prelude.rs b/src/prelude.rs index 41ff85d90d..ec0a5a1533 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,5 +3,5 @@ crate use crate::commands::command::{Command, CommandAction, CommandBlueprint, R crate use crate::env::{Environment, Host}; crate use crate::errors::ShellError; crate use crate::format::RenderView; -crate use crate::object::{Primitive, ShellObject, Value}; +crate use crate::object::{Primitive, Value}; crate use std::collections::VecDeque;