some minor improvements and removing dead code (#1563)

This commit is contained in:
Jonathan Turner 2020-04-10 07:48:10 +12:00 committed by GitHub
parent 2c513d1883
commit c86cf31aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 7 additions and 129 deletions

View File

@ -176,11 +176,15 @@ fn coerce_compare_primitive(
(Decimal(left), Bytes(right)) => { (Decimal(left), Bytes(right)) => {
CompareValues::Decimals(left.clone(), BigDecimal::from(*right)) CompareValues::Decimals(left.clone(), BigDecimal::from(*right))
} }
(Bytes(left), Bytes(right)) => {
CompareValues::Ints(BigInt::from(*left), BigInt::from(*right))
}
(Bytes(left), Int(right)) => CompareValues::Ints(BigInt::from(*left), right.clone()), (Bytes(left), Int(right)) => CompareValues::Ints(BigInt::from(*left), right.clone()),
(Bytes(left), Decimal(right)) => { (Bytes(left), Decimal(right)) => {
CompareValues::Decimals(BigDecimal::from(*left), right.clone()) CompareValues::Decimals(BigDecimal::from(*left), right.clone())
} }
(Bytes(left), Nothing) => CompareValues::Ints(BigInt::from(*left), BigInt::from(0)), (Bytes(left), Nothing) => CompareValues::Ints(BigInt::from(*left), BigInt::from(0)),
(Nothing, Nothing) => CompareValues::Ints(BigInt::from(0), BigInt::from(0)),
(Nothing, Bytes(right)) => CompareValues::Ints(BigInt::from(0), BigInt::from(*right)), (Nothing, Bytes(right)) => CompareValues::Ints(BigInt::from(0), BigInt::from(*right)),
(Int(left), Nothing) => CompareValues::Ints(left.clone(), BigInt::from(0)), (Int(left), Nothing) => CompareValues::Ints(left.clone(), BigInt::from(0)),
(Nothing, Int(right)) => CompareValues::Ints(BigInt::from(0), right.clone()), (Nothing, Int(right)) => CompareValues::Ints(BigInt::from(0), right.clone()),

View File

@ -275,7 +275,7 @@ mod tests {
assert_eq!( assert_eq!(
actual.path(), actual.path(),
Some( Some(
UntaggedValue::table(&vec![ UntaggedValue::table(&[
UntaggedValue::string("/Users/andresrobalino/.volta/bin") UntaggedValue::string("/Users/andresrobalino/.volta/bin")
.into_untagged_value(), .into_untagged_value(),
UntaggedValue::string("/users/mosqueteros/bin").into_untagged_value(), UntaggedValue::string("/users/mosqueteros/bin").into_untagged_value(),

View File

@ -1,12 +1,8 @@
pub(crate) mod entries;
pub(crate) mod generic;
pub(crate) mod list;
pub(crate) mod table; pub(crate) mod table;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
pub(crate) use entries::EntriesView;
pub(crate) use table::TableView; pub(crate) use table::TableView;
pub(crate) trait RenderView { pub(crate) trait RenderView {

View File

@ -1,50 +0,0 @@
use crate::data::value;
use crate::format::RenderView;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::Value;
use derive_new::new;
// An entries list is printed like this:
//
// name : ...
// name2 : ...
// another_name : ...
#[derive(new)]
pub struct EntriesView {
entries: Vec<(String, String)>,
}
impl EntriesView {
pub(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::format_leaf(value.borrow()).plain_string(75);
entries.push((desc.clone(), formatted_value))
}
EntriesView::new(entries)
}
}
impl RenderView for EntriesView {
fn render_view(&self, _host: &mut dyn Host) -> Result<(), ShellError> {
if self.entries.is_empty() {
return Ok(());
}
if let Some(max_name_size) = self.entries.iter().map(|(n, _)| n.len()).max() {
for (name, value) in &self.entries {
outln!("{:width$} : {}", name, value, width = max_name_size)
}
}
Ok(())
}
}

View File

@ -1,48 +0,0 @@
use crate::data::value::format_leaf;
use crate::format::{EntriesView, RenderView, TableView};
use crate::prelude::*;
use derive_new::new;
use nu_errors::ShellError;
use nu_protocol::{format_primitive, UntaggedValue, Value};
// A list is printed one line at a time with an optional separator between groups
#[derive(new)]
pub struct GenericView<'value> {
value: &'value Value,
}
impl RenderView for GenericView<'_> {
fn render_view(&self, host: &mut dyn Host) -> Result<(), ShellError> {
let tag = &self.value.tag;
match &self.value.value {
UntaggedValue::Primitive(p) => {
host.stdout(&format_primitive(p, None));
Ok(())
}
UntaggedValue::Table(l) => {
let view = TableView::from_list(l, 0);
if let Some(view) = view {
view.render_view(host)?;
}
Ok(())
}
o @ UntaggedValue::Row(_) => {
let view = EntriesView::from_value(&o.clone().into_value(tag));
view.render_view(host)?;
Ok(())
}
b @ UntaggedValue::Block(_) => {
let printed = format_leaf(b).plain_string(host.width());
let view = EntriesView::from_value(&UntaggedValue::string(printed).into_value(tag));
view.render_view(host)?;
Ok(())
}
UntaggedValue::Error(e) => Err(e.clone()),
}
}
}

View File

@ -1,23 +0,0 @@
use crate::format::RenderView;
use crate::prelude::*;
use derive_new::new;
use nu_errors::ShellError;
// A list is printed one line at a time with an optional separator between groups
#[derive(new)]
pub struct ListView {
list: Vec<Vec<String>>,
sep: String,
}
impl RenderView for ListView {
fn render_view(&self, host: &mut dyn Host) -> Result<(), ShellError> {
for output in &self.list {
let string: String = output.iter().map(|l| format!("{}\n", l)).collect();
host.stdout(&format!("{}{}", string, self.sep));
}
Ok(())
}
}

View File

@ -8,7 +8,6 @@ use textwrap::fill;
use prettytable::format::{Alignment, FormatBuilder, LinePosition, LineSeparator}; use prettytable::format::{Alignment, FormatBuilder, LinePosition, LineSeparator};
use prettytable::{color, Attr, Cell, Row, Table}; use prettytable::{color, Attr, Cell, Row, Table};
use term::color::Color;
type Entries = Vec<Vec<(String, &'static str)>>; type Entries = Vec<Vec<(String, &'static str)>>;
@ -389,7 +388,7 @@ impl RenderView for TableView {
} }
} }
fn str_to_color(s: String) -> Option<Color> { fn str_to_color(s: String) -> Option<color::Color> {
match s.as_str() { match s.as_str() {
"g" | "green" => Some(color::GREEN), "g" | "green" => Some(color::GREEN),
"r" | "red" => Some(color::RED), "r" | "red" => Some(color::RED),

View File

@ -34,7 +34,7 @@ mod integration {
.unwrap(); .unwrap();
match r { match r {
Primitive::Date(_) => (), Primitive::Date(_) => (),
_ => assert!(false, "failed to convert string to date"), _ => panic!("failed to convert string to date"),
} }
} }
} }