add nu-pretty-hex, add into binary, update binaryview (#3370)

* add nu-pretty-hex, add into binary, update binaryview

* updated parameter name, updated examples

* fixed nu-pretty-hex test

* fixed tests again! and added a no color option to pretty-hex
This commit is contained in:
Darren Schroeder
2021-05-01 11:12:25 -05:00
committed by GitHub
parent a8f555856a
commit 8cd639f6a2
22 changed files with 1256 additions and 31 deletions

View File

@ -1,7 +1,7 @@
use crossterm::{style::Attribute, ExecutableCommand};
use nu_protocol::outln;
use nu_pretty_hex::*;
use nu_protocol::{outln, Value};
use nu_source::AnchorLocation;
use pretty_hex::*;
#[derive(Default)]
pub struct BinaryView;
@ -16,6 +16,8 @@ pub fn view_binary(
b: &[u8],
source: Option<&AnchorLocation>,
lores_mode: bool,
skip: Option<&Value>,
length: Option<&Value>,
) -> Result<(), Box<dyn std::error::Error>> {
if b.len() > 3 {
if let (0x4e, 0x45, 0x53) = (b[0], b[1], b[2]) {
@ -23,7 +25,8 @@ pub fn view_binary(
return Ok(());
}
}
view_contents(b, source, lores_mode)?;
view_contents(b, source, lores_mode, skip, length)?;
Ok(())
}
@ -207,7 +210,29 @@ pub fn view_contents(
buffer: &[u8],
_source: Option<&AnchorLocation>,
lores_mode: bool,
skip: Option<&Value>,
length: Option<&Value>,
) -> Result<(), Box<dyn std::error::Error>> {
let skip_bytes = match skip {
Some(s) => Some(s.as_usize().unwrap_or(0)),
None => None,
};
let num_bytes = match length {
Some(b) => Some(b.as_usize().unwrap_or(0)),
None => None,
};
let config = HexConfig {
title: true,
ascii: true,
width: 16,
group: 4,
chunk: 1,
skip: skip_bytes,
length: num_bytes,
};
let mut raw_image_buffer = load_from_png_buffer(buffer);
if raw_image_buffer.is_err() {
@ -216,7 +241,7 @@ pub fn view_contents(
if raw_image_buffer.is_err() {
//Not yet supported
outln!("{:?}", buffer.hex_dump());
outln!("{}", config_hex(&buffer, config));
return Ok(());
}
let raw_image_buffer = raw_image_buffer?;
@ -270,7 +295,8 @@ pub fn view_contents(
}
_ => {
//Not yet supported
outln!("{:?}", buffer.hex_dump());
// outln!("{:?}", buffer.hex_dump());
outln!("{}", config_hex(&buffer, config));
return Ok(());
}
}

View File

@ -1,6 +1,6 @@
use nu_errors::ShellError;
use nu_plugin::Plugin;
use nu_protocol::{CallInfo, Primitive, Signature, UntaggedValue, Value};
use nu_protocol::{CallInfo, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
use crate::binaryview::view_binary;
use crate::BinaryView;
@ -9,14 +9,29 @@ impl Plugin for BinaryView {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("binaryview")
.desc("Autoview of binary data.")
.switch("lores", "use low resolution output mode", Some('l')))
.switch("lores", "use low resolution output mode", Some('l'))
.named(
"skip",
SyntaxShape::Int,
"skip x number of bytes",
Some('s'),
)
.named(
"bytes",
SyntaxShape::Int,
"show y number of bytes",
Some('b'),
))
}
fn sink(&mut self, call_info: CallInfo, input: Vec<Value>) {
for v in input {
let value_anchor = v.anchor();
if let UntaggedValue::Primitive(Primitive::Binary(b)) = &v.value {
let _ = view_binary(&b, value_anchor.as_ref(), call_info.args.has("lores"));
let low_res = call_info.args.has("lores");
let skip = call_info.args.get("skip");
let length = call_info.args.get("bytes");
let _ = view_binary(&b, value_anchor.as_ref(), low_res, skip, length);
}
}
}