Add pretty binary viewing

This commit is contained in:
Jonathan Turner
2019-07-04 17:23:05 +12:00
parent 65a0d27c8a
commit 5e779d8b2b
4 changed files with 59 additions and 1 deletions

View File

@ -6,7 +6,7 @@ use crate::prelude::*;
pub fn autoview(args: SinkCommandArgs) -> Result<(), ShellError> {
if args.input.len() > 0 {
if let Value::Binary(_) = args.input[0] {
println!("Binary");
args.ctx.get_sink("binaryview").run(args)?;
} else if equal_shapes(&args.input) {
args.ctx.get_sink("table").run(args)?;
} else {

46
src/plugins/binaryview.rs Normal file
View File

@ -0,0 +1,46 @@
use indexmap::IndexMap;
use nu::{serve_plugin, Args, CommandConfig, Plugin, Primitive, ShellError, Value};
struct BinaryView;
impl BinaryView {
fn new() -> BinaryView {
BinaryView
}
}
impl Plugin for BinaryView {
fn config(&mut self) -> Result<CommandConfig, ShellError> {
Ok(CommandConfig {
name: "binaryview".to_string(),
mandatory_positional: vec![],
optional_positional: vec![],
can_load: vec![],
can_save: vec![],
is_filter: false,
is_sink: true,
named: IndexMap::new(),
rest_positional: true,
})
}
fn sink(&mut self, _args: Args, input: Vec<Value>) {
for v in input {
match v {
Value::Binary(b) => {
view_binary(&b);
}
_ => {}
}
}
}
}
fn view_binary(b: &[u8]) {
use pretty_hex::*;
println!("{:?}", b.hex_dump());
}
fn main() {
serve_plugin(&mut BinaryView::new());
}