Merge pull request #201 from jonathandturner/row_numbers

Row numbers
This commit is contained in:
Jonathan Turner 2019-07-22 04:31:42 +12:00 committed by GitHub
commit 5108ebccac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
use crate::format::RenderView; use crate::format::RenderView;
use crate::object::Value; use crate::object::Value;
use crate::prelude::*; use crate::prelude::*;
use ansi_term::Color;
use derive_new::new; use derive_new::new;
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
@ -38,16 +39,22 @@ impl TableView {
let mut entries = vec![]; let mut entries = vec![];
for value in values { for (idx, value) in values.iter().enumerate() {
let row = headers let mut row: Vec<String> = headers
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, d)| value.get_data(d).borrow().format_leaf(Some(&headers[i]))) .map(|(i, d)| value.get_data(d).borrow().format_leaf(Some(&headers[i])))
.collect(); .collect();
if values.len() > 1 {
row.insert(0, format!("{}", Color::Black.bold().paint(idx.to_string())));
}
entries.push(row); entries.push(row);
} }
if values.len() > 1 {
headers.insert(0, format!("#"));
}
Some(TableView { headers, entries }) Some(TableView { headers, entries })
} }
} }