From 1efae6876d39dd95f9b032d21288fc0686e54d90 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sat, 25 Dec 2021 05:15:01 +1100 Subject: [PATCH] Wire hex viewing into a few more places (#572) --- .../nu-command/src/conversions/into/binary.rs | 39 +++++++++++-------- crates/nu-command/src/viewers/table.rs | 13 +++++++ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/crates/nu-command/src/conversions/into/binary.rs b/crates/nu-command/src/conversions/into/binary.rs index f2b310d44c..71393b9882 100644 --- a/crates/nu-command/src/conversions/into/binary.rs +++ b/crates/nu-command/src/conversions/into/binary.rs @@ -97,25 +97,30 @@ fn into_binary( let head = call.head; let column_paths: Vec = call.rest(engine_state, stack, 0)?; - input.map( - move |v| { - if column_paths.is_empty() { - action(&v, head) - } else { - let mut ret = v; - for path in &column_paths { - let r = - ret.update_cell_path(&path.members, Box::new(move |old| action(old, head))); - if let Err(error) = r { - return Value::Error { error }; + match input { + PipelineData::ByteStream(..) => Ok(input), + _ => input.map( + move |v| { + if column_paths.is_empty() { + action(&v, head) + } else { + let mut ret = v; + for path in &column_paths { + let r = ret.update_cell_path( + &path.members, + Box::new(move |old| action(old, head)), + ); + if let Err(error) = r { + return Value::Error { error }; + } } - } - ret - } - }, - engine_state.ctrlc.clone(), - ) + ret + } + }, + engine_state.ctrlc.clone(), + ), + } } fn int_to_endian(n: i64) -> Vec { diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index d5ac7f144b..4ad9749a75 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -75,6 +75,19 @@ impl Command for Table { head, None, )), + PipelineData::Value(Value::Binary { val, .. }, ..) => Ok(PipelineData::StringStream( + StringStream::from_stream( + vec![Ok(if val.iter().all(|x| x.is_ascii()) { + format!("{}", String::from_utf8_lossy(&val)) + } else { + format!("{}\n", nu_pretty_hex::pretty_hex(&val)) + })] + .into_iter(), + ctrlc, + ), + head, + None, + )), PipelineData::Value(Value::List { vals, .. }, ..) => { let table = convert_to_table(row_offset, &vals, ctrlc, &config, call.head)?;