From 5e779d8b2bf37394d6e7277d12df9a0e3e7f0eb9 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 4 Jul 2019 17:23:05 +1200 Subject: [PATCH] Add pretty binary viewing --- Cargo.lock | 7 ++++++ Cargo.toml | 5 +++++ src/commands/autoview.rs | 2 +- src/plugins/binaryview.rs | 46 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/plugins/binaryview.rs diff --git a/Cargo.lock b/Cargo.lock index 8a66266319..e9a0a85e14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1471,6 +1471,7 @@ dependencies = [ "nom 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "nom_locate 0.3.1 (git+https://github.com/wycats/nom_locate.git?branch=nom5)", "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "pretty-hex 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_env_logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "prettyprint 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1774,6 +1775,11 @@ dependencies = [ "xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "pretty-hex" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pretty_assertions" version = "0.6.1" @@ -3169,6 +3175,7 @@ dependencies = [ "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum plist 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a9f075f6394100e7c105ed1af73fb1859d6fd14e49d4290d578120beb167f" +"checksum pretty-hex 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "119929a2a3b731bb3d888f7a1b5dc3c1db28b6c134def5d99f7e16e2da16b8f7" "checksum pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427" "checksum pretty_env_logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df8b3f4e0475def7d9c2e5de8e5a1306949849761e107b360d03e98eafaffd61" "checksum prettyprint 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f32f02328f651d5283173c7a9b2ef354b079fa535706547dde16d61ae23ecded" diff --git a/Cargo.toml b/Cargo.toml index 57c430ca7f..8df4b8955f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,7 @@ subprocess = "0.1.18" sys-info = "0.5.7" mime = "0.3.13" regex = "1.1.7" +pretty-hex = "0.1.0" [dev-dependencies] pretty_assertions = "0.6.1" @@ -88,6 +89,10 @@ path = "src/plugins/newskip.rs" name = "nu_plugin_treeview" path = "src/plugins/treeview.rs" +[[bin]] +name = "nu_plugin_binaryview" +path = "src/plugins/binaryview.rs" + [[bin]] name = "nu" path = "src/main.rs" diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index 6894b0559b..4d8d85bd76 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -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 { diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs new file mode 100644 index 0000000000..2a19a49ebb --- /dev/null +++ b/src/plugins/binaryview.rs @@ -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 { + 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) { + 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()); +}