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

7
Cargo.lock generated
View File

@ -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"

View File

@ -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"

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());
}