2019-05-10 18:59:12 +02:00
|
|
|
use crate::format::RenderView;
|
2019-05-15 18:12:38 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
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 {
|
|
|
|
entries: Vec<(String, String)>,
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:23:36 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:59:12 +02:00
|
|
|
impl RenderView for EntriesView {
|
2019-05-16 00:58:44 +02:00
|
|
|
fn render_view(&self, _host: &dyn Host) -> Vec<String> {
|
2019-05-10 18:59:12 +02:00
|
|
|
if self.entries.len() == 0 {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
let max_name_size: usize = self.entries.iter().map(|(n, _)| n.len()).max().unwrap();
|
|
|
|
|
|
|
|
self.entries
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| format!("{:width$} : {}", k, v, width = max_name_size))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 18:12:38 +02:00
|
|
|
|
|
|
|
pub struct EntriesListView {
|
|
|
|
values: VecDeque<Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EntriesListView {
|
2019-05-23 09:23:06 +02:00
|
|
|
crate async fn from_stream(values: InputStream) -> EntriesListView {
|
|
|
|
EntriesListView {
|
|
|
|
values: values.collect().await,
|
|
|
|
}
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RenderView for EntriesListView {
|
|
|
|
fn render_view(&self, host: &dyn Host) -> Vec<String> {
|
|
|
|
if self.values.len() == 0 {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut strings = vec![];
|
|
|
|
|
|
|
|
let last = self.values.len() - 1;
|
|
|
|
|
|
|
|
for (i, item) in self.values.iter().enumerate() {
|
2019-05-16 00:23:36 +02:00
|
|
|
let view = EntriesView::from_value(item);
|
2019-05-15 18:12:38 +02:00
|
|
|
let out = view.render_view(host);
|
|
|
|
|
|
|
|
strings.extend(out);
|
|
|
|
|
|
|
|
if i != last {
|
|
|
|
strings.push("\n".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strings
|
|
|
|
}
|
|
|
|
}
|