2019-07-04 05:11:56 +00:00
|
|
|
use derive_new::new;
|
2019-12-09 18:39:51 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-27 13:30:14 +00:00
|
|
|
use nu_plugin::{serve_plugin, Plugin};
|
|
|
|
use nu_protocol::{format_primitive, CallInfo, Signature, UntaggedValue, Value};
|
2019-07-04 05:11:56 +00:00
|
|
|
use ptree::item::StringItem;
|
|
|
|
use ptree::output::print_tree_with;
|
|
|
|
use ptree::print_config::PrintConfig;
|
|
|
|
use ptree::style::{Color, Style};
|
|
|
|
use ptree::TreeBuilder;
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct TreeView {
|
|
|
|
tree: StringItem,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeView {
|
2019-12-09 18:39:51 +00:00
|
|
|
fn from_value_helper(value: &UntaggedValue, mut builder: &mut TreeBuilder) {
|
2019-07-04 05:11:56 +00:00
|
|
|
match value {
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(p) => {
|
2019-12-09 18:39:51 +00:00
|
|
|
let _ = builder.add_empty_child(format_primitive(p, None));
|
2019-07-04 05:11:56 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Row(o) => {
|
2019-07-04 05:11:56 +00:00
|
|
|
for (k, v) in o.entries.iter() {
|
|
|
|
builder = builder.begin_child(k.clone());
|
|
|
|
Self::from_value_helper(v, builder);
|
|
|
|
builder = builder.end_child();
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Table(l) => {
|
2019-07-04 05:11:56 +00:00
|
|
|
for elem in l.iter() {
|
|
|
|
Self::from_value_helper(elem, builder);
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 18:39:51 +00:00
|
|
|
_ => {}
|
2019-07-04 05:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_value(value: &Value) -> TreeView {
|
|
|
|
let descs = value.data_descriptors();
|
|
|
|
|
|
|
|
let mut tree = TreeBuilder::new("".to_string());
|
|
|
|
let mut builder = &mut tree;
|
|
|
|
|
|
|
|
for desc in descs {
|
2019-12-09 18:39:51 +00:00
|
|
|
let value = match &value.value {
|
|
|
|
UntaggedValue::Row(d) => d.get_data(&desc).borrow().clone(),
|
|
|
|
_ => value.clone(),
|
|
|
|
};
|
2019-07-04 05:11:56 +00:00
|
|
|
builder = builder.begin_child(desc.clone());
|
2019-12-09 18:39:51 +00:00
|
|
|
Self::from_value_helper(&value, &mut builder);
|
2019-07-04 05:11:56 +00:00
|
|
|
builder = builder.end_child();
|
|
|
|
//entries.push((desc.name.clone(), value.borrow().copy()))
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeView::new(builder.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_view(&self) -> Result<(), ShellError> {
|
|
|
|
// Set up the print configuration
|
|
|
|
let config = {
|
|
|
|
let mut config = PrintConfig::from_env();
|
|
|
|
config.branch = Style {
|
|
|
|
foreground: Some(Color::Green),
|
|
|
|
dimmed: true,
|
|
|
|
..Style::default()
|
|
|
|
};
|
|
|
|
config.leaf = Style {
|
|
|
|
bold: true,
|
|
|
|
..Style::default()
|
|
|
|
};
|
|
|
|
config.indent = 4;
|
|
|
|
config
|
|
|
|
};
|
|
|
|
|
|
|
|
// Print out the tree using custom formatting
|
|
|
|
print_tree_with(&self.tree, &config)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TreeViewer;
|
|
|
|
|
|
|
|
impl Plugin for TreeViewer {
|
2019-08-02 19:15:07 +00:00
|
|
|
fn config(&mut self) -> Result<Signature, ShellError> {
|
2019-09-02 06:11:05 +00:00
|
|
|
Ok(Signature::build("tree").desc("View the contents of the pipeline as a tree."))
|
2019-07-04 05:11:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn sink(&mut self, _call_info: CallInfo, input: Vec<Value>) {
|
2019-12-31 07:36:08 +00:00
|
|
|
if !input.is_empty() {
|
2019-07-04 05:11:56 +00:00
|
|
|
for i in input.iter() {
|
|
|
|
let view = TreeView::from_value(&i);
|
|
|
|
let _ = view.render_view();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
serve_plugin(&mut TreeViewer);
|
|
|
|
}
|