forked from extern/nushell
Merge pull request #124 from jonathandturner/vtable
Add vertical table view
This commit is contained in:
commit
9018eefca9
@ -85,7 +85,9 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
sink("autoview", autoview::autoview),
|
||||
sink("clip", clip::clip),
|
||||
sink("save", save::save),
|
||||
sink("table", table::table),
|
||||
sink("tree", tree::tree),
|
||||
sink("vtable", vtable::vtable),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ crate mod sort_by;
|
||||
crate mod split_column;
|
||||
crate mod split_row;
|
||||
crate mod sysinfo;
|
||||
crate mod table;
|
||||
crate mod to_array;
|
||||
crate mod to_ini;
|
||||
crate mod to_json;
|
||||
@ -35,6 +36,7 @@ crate mod to_toml;
|
||||
crate mod tree;
|
||||
crate mod trim;
|
||||
crate mod view;
|
||||
crate mod vtable;
|
||||
crate mod where_;
|
||||
|
||||
crate use command::command;
|
||||
|
16
src/commands/table.rs
Normal file
16
src/commands/table.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use crate::commands::command::SinkCommandArgs;
|
||||
use crate::errors::ShellError;
|
||||
use crate::format::TableView;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub fn table(args: SinkCommandArgs) -> Result<(), ShellError> {
|
||||
if args.input.len() > 0 {
|
||||
let mut host = args.ctx.host.lock().unwrap();
|
||||
let view = TableView::from_list(&args.input);
|
||||
if let Some(view) = view {
|
||||
handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
16
src/commands/vtable.rs
Normal file
16
src/commands/vtable.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use crate::commands::command::SinkCommandArgs;
|
||||
use crate::errors::ShellError;
|
||||
use crate::format::VTableView;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub fn vtable(args: SinkCommandArgs) -> Result<(), ShellError> {
|
||||
if args.input.len() > 0 {
|
||||
let mut host = args.ctx.host.lock().unwrap();
|
||||
let view = VTableView::from_list(&args.input);
|
||||
if let Some(view) = view {
|
||||
handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@ -3,6 +3,7 @@ crate mod generic;
|
||||
crate mod list;
|
||||
crate mod table;
|
||||
crate mod tree;
|
||||
crate mod vtable;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
@ -10,6 +11,7 @@ crate use entries::EntriesView;
|
||||
crate use generic::GenericView;
|
||||
crate use table::TableView;
|
||||
crate use tree::TreeView;
|
||||
crate use vtable::VTableView;
|
||||
|
||||
crate trait RenderView {
|
||||
fn render_view(&self, host: &mut dyn Host) -> Result<(), ShellError>;
|
||||
|
@ -6,11 +6,6 @@ use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
|
||||
|
||||
use prettytable::{color, Attr, Cell, Row, Table};
|
||||
|
||||
// An entries list is printed like this:
|
||||
//
|
||||
// name : ...
|
||||
// name2 : ...
|
||||
// another_name : ...
|
||||
#[derive(new)]
|
||||
pub struct TableView {
|
||||
headers: Vec<DataDescriptor>,
|
||||
|
84
src/format/vtable.rs
Normal file
84
src/format/vtable.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use crate::format::RenderView;
|
||||
use crate::object::{DescriptorName, Value};
|
||||
use crate::prelude::*;
|
||||
use derive_new::new;
|
||||
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
|
||||
|
||||
use prettytable::{color, Attr, Cell, Row, Table};
|
||||
|
||||
#[derive(new)]
|
||||
pub struct VTableView {
|
||||
entries: Vec<Vec<String>>,
|
||||
}
|
||||
|
||||
impl VTableView {
|
||||
pub fn from_list(values: &[Value]) -> Option<VTableView> {
|
||||
if values.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let item = &values[0];
|
||||
let headers = item.data_descriptors();
|
||||
|
||||
if headers.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut entries = vec![];
|
||||
|
||||
for header in headers {
|
||||
let mut row = vec![];
|
||||
|
||||
if let DescriptorName::String(s) = &header.name {
|
||||
row.push(s.clone());
|
||||
} else {
|
||||
row.push("value".to_string());
|
||||
}
|
||||
for value in values {
|
||||
row.push(value.get_data(&header).borrow().format_leaf(Some(&header)));
|
||||
}
|
||||
entries.push(row);
|
||||
}
|
||||
|
||||
Some(VTableView { entries })
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderView for VTableView {
|
||||
fn render_view(&self, host: &mut dyn Host) -> Result<(), ShellError> {
|
||||
if self.entries.len() == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut table = Table::new();
|
||||
|
||||
let fb = FormatBuilder::new()
|
||||
.separator(LinePosition::Top, LineSeparator::new('-', '+', ' ', ' '))
|
||||
.separator(LinePosition::Bottom, LineSeparator::new('-', '+', ' ', ' '))
|
||||
.column_separator('|')
|
||||
.padding(1, 1);
|
||||
|
||||
table.set_format(fb.build());
|
||||
|
||||
for row in &self.entries {
|
||||
table.add_row(Row::new(
|
||||
row.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, h)| {
|
||||
if idx == 0 {
|
||||
Cell::new(h)
|
||||
.with_style(Attr::ForegroundColor(color::GREEN))
|
||||
.with_style(Attr::Bold)
|
||||
} else {
|
||||
Cell::new(h)
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
));
|
||||
}
|
||||
|
||||
table.print_term(&mut *host.out_terminal()).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user