diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index 9848bb6051..0285664fb9 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -282,8 +282,22 @@ fn evaluate_source( }?; if let PipelineData::ByteStream(..) = pipeline { - pipeline.print(engine_state, stack, false, false) - } else if let Some(hook) = engine_state.get_config().hooks.display_output.clone() { + // run the display hook on bytestreams too + run_display_hook(engine_state, stack, pipeline, false) + } else { + run_display_hook(engine_state, stack, pipeline, true) + }?; + + Ok(false) +} + +fn run_display_hook( + engine_state: &mut EngineState, + stack: &mut Stack, + pipeline: PipelineData, + no_newline: bool, +) -> Result<(), ShellError> { + if let Some(hook) = engine_state.get_config().hooks.display_output.clone() { let pipeline = eval_hook( engine_state, stack, @@ -292,14 +306,11 @@ fn evaluate_source( &hook, "display_output", )?; - pipeline.print(engine_state, stack, false, false) + pipeline.print(engine_state, stack, no_newline, false) } else { - pipeline.print(engine_state, stack, true, false) - }?; - - Ok(false) + pipeline.print(engine_state, stack, no_newline, false) + } } - #[cfg(test)] mod test { use super::*; diff --git a/crates/nu-command/src/filesystem/open.rs b/crates/nu-command/src/filesystem/open.rs index ea2a253a2a..855272d978 100644 --- a/crates/nu-command/src/filesystem/open.rs +++ b/crates/nu-command/src/filesystem/open.rs @@ -146,6 +146,9 @@ impl Command for Open { } }; + // Assigning content type should only happen in raw mode. Otherwise, the content + // will potentially be in one of the built-in nushell `from xxx` formats and therefore + // cease to be in the original content-type.... or so I'm told. :) let content_type = if raw { path.extension() .map(|ext| ext.to_string_lossy().to_string()) @@ -283,6 +286,9 @@ fn detect_content_type(extension: &str) -> Option { match extension { // Per RFC-9512, application/yaml should be used "yaml" | "yml" => Some("application/yaml".to_string()), + "nu" => Some("application/x-nuscript".to_string()), + "json" | "jsonl" | "ndjson" => Some("application/json".to_string()), + "nuon" => Some("application/x-nuon".to_string()), _ => mime_guess::from_ext(extension) .first() .map(|mime| mime.to_string()), diff --git a/crates/nu-command/tests/commands/open.rs b/crates/nu-command/tests/commands/open.rs index b50c6c9e6c..c3e7db137f 100644 --- a/crates/nu-command/tests/commands/open.rs +++ b/crates/nu-command/tests/commands/open.rs @@ -394,7 +394,7 @@ fn test_content_types_with_open_raw() { let result = nu!(cwd: dirs.formats(), "open --raw sample_data.xlsx | metadata"); assert!(result.out.contains("vnd.openxmlformats-officedocument")); let result = nu!(cwd: dirs.formats(), "open --raw sample_def.nu | metadata"); - assert!(!result.out.contains("content_type")); + assert!(result.out.contains("application/x-nuscript")); let result = nu!(cwd: dirs.formats(), "open --raw sample.eml | metadata"); assert!(result.out.contains("message/rfc822")); let result = nu!(cwd: dirs.formats(), "open --raw cargo_sample.toml | metadata"); diff --git a/crates/nu-protocol/src/pipeline/byte_stream.rs b/crates/nu-protocol/src/pipeline/byte_stream.rs index 8e46402a1d..da78b9c836 100644 --- a/crates/nu-protocol/src/pipeline/byte_stream.rs +++ b/crates/nu-protocol/src/pipeline/byte_stream.rs @@ -41,10 +41,7 @@ impl ByteStreamSource { /// Source is a `Child` or `File`, rather than `Read`. Currently affects trimming fn is_external(&self) -> bool { - matches!( - self, - ByteStreamSource::File(..) | ByteStreamSource::Child(..) - ) + matches!(self, ByteStreamSource::Child(..)) } }