nushell/crates/nu-cli/src/format/entries.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

use crate::data::value;
2019-05-10 18:59:12 +02:00
use crate::format::RenderView;
2019-05-15 18:12:38 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::Value;
2019-05-15 18:12:38 +02:00
2019-05-10 18:59:12 +02:00
use derive_new::new;
// An entries list is printed like this:
//
// name : ...
// name2 : ...
// another_name : ...
#[derive(new)]
pub struct EntriesView {
2019-07-03 19:37:09 +02:00
entries: Vec<(String, String)>,
2019-05-10 18:59:12 +02:00
}
2019-05-16 00:23:36 +02:00
impl EntriesView {
pub(crate) fn from_value(value: &Value) -> EntriesView {
2019-05-16 00:23:36 +02:00
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);
2019-05-16 00:23:36 +02:00
2019-07-03 19:37:09 +02:00
entries.push((desc.clone(), formatted_value))
2019-05-16 00:23:36 +02:00
}
EntriesView::new(entries)
}
}
2019-05-10 18:59:12 +02:00
impl RenderView for EntriesView {
fn render_view(&self, _host: &mut dyn Host) -> Result<(), ShellError> {
if self.entries.is_empty() {
return Ok(());
2019-05-10 18:59:12 +02:00
}
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(())
2019-05-10 18:59:12 +02:00
}
}