From d88d057bf69c2440b2808318b45a9c7b992633a4 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Wed, 11 May 2022 00:24:06 +0800 Subject: [PATCH] keep metadata while format filesize (#5502) --- crates/nu-command/src/strings/format/filesize.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/strings/format/filesize.rs b/crates/nu-command/src/strings/format/filesize.rs index 508c569a8..b15a205e9 100644 --- a/crates/nu-command/src/strings/format/filesize.rs +++ b/crates/nu-command/src/strings/format/filesize.rs @@ -2,8 +2,8 @@ use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - format_filesize, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, - Span, SyntaxShape, Value, + format_filesize, Category, Example, IntoPipelineData, PipelineData, PipelineMetadata, + ShellError, Signature, Span, SyntaxShape, Value, }; use std::iter; @@ -47,13 +47,16 @@ impl Command for FileSize { .as_string()? .to_ascii_lowercase(); let span = call.head; + let input_metadata = input.metadata(); let data_as_value = input.into_value(span); // Something need to consider: // 1. what if input data type is not table? For now just output nothing. // 2. what if value is not a FileSize type? For now just return nothing too for the value. match data_as_value { - Value::List { vals, span } => format_impl(vals, field, format_value, span), + Value::List { vals, span } => { + format_impl(vals, field, format_value, span, input_metadata) + } _ => Ok(Value::Nothing { span }.into_pipeline_data()), } } @@ -79,6 +82,7 @@ fn format_impl( field: String, format_value: String, input_span: Span, + input_metadata: Option, ) -> Result { let records: Vec = vals .into_iter() @@ -113,11 +117,12 @@ fn format_impl( }) .collect(); - Ok(Value::List { + let result = Value::List { vals: records, span: input_span, } - .into_pipeline_data()) + .into_pipeline_data(); + Ok(result.set_metadata(input_metadata)) } fn format_value_impl(val: Value, format_value: &str, span: Span) -> Value {