Make to text output not dependent on config

This commit is contained in:
Ian Manske
2025-03-07 22:15:56 -08:00
parent f4c6ce6d06
commit 57800e500e

View File

@ -1,8 +1,6 @@
use chrono_humanize::HumanTime; use chrono_humanize::HumanTime;
use nu_engine::command_prelude::*; use nu_engine::command_prelude::*;
use nu_protocol::{ use nu_protocol::{format_duration, shell_error::io::IoError, ByteStream, PipelineMetadata};
format_duration, shell_error::io::IoError, ByteStream, Config, PipelineMetadata,
};
use std::io::Write; use std::io::Write;
const LINE_ENDING: &str = if cfg!(target_os = "windows") { const LINE_ENDING: &str = if cfg!(target_os = "windows") {
@ -50,7 +48,6 @@ impl Command for ToText {
let no_newline = call.has_flag(engine_state, stack, "no-newline")?; let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
let serialize_types = call.has_flag(engine_state, stack, "serialize")?; let serialize_types = call.has_flag(engine_state, stack, "serialize")?;
let input = input.try_expand_range()?; let input = input.try_expand_range()?;
let config = stack.get_config(engine_state);
match input { match input {
PipelineData::Empty => Ok(Value::string(String::new(), head) PipelineData::Empty => Ok(Value::string(String::new(), head)
@ -62,8 +59,7 @@ impl Command for ToText {
Value::Record { val, .. } => !val.is_empty(), Value::Record { val, .. } => !val.is_empty(),
_ => false, _ => false,
}; };
let mut str = let mut str = local_into_string(engine_state, value, LINE_ENDING, serialize_types);
local_into_string(engine_state, value, LINE_ENDING, &config, serialize_types);
if add_trailing { if add_trailing {
str.push_str(LINE_ENDING); str.push_str(LINE_ENDING);
} }
@ -98,7 +94,6 @@ impl Command for ToText {
&engine_state_clone, &engine_state_clone,
val, val,
LINE_ENDING, LINE_ENDING,
&config,
serialize_types, serialize_types,
); );
write!(buf, "{str}").map_err(&from_io_error)?; write!(buf, "{str}").map_err(&from_io_error)?;
@ -113,7 +108,6 @@ impl Command for ToText {
&engine_state_clone, &engine_state_clone,
val, val,
LINE_ENDING, LINE_ENDING,
&config,
serialize_types, serialize_types,
); );
str.push_str(LINE_ENDING); str.push_str(LINE_ENDING);
@ -163,7 +157,6 @@ fn local_into_string(
engine_state: &EngineState, engine_state: &EngineState,
value: Value, value: Value,
separator: &str, separator: &str,
config: &Config,
serialize_types: bool, serialize_types: bool,
) -> String { ) -> String {
let span = value.span(); let span = value.span();
@ -171,7 +164,7 @@ fn local_into_string(
Value::Bool { val, .. } => val.to_string(), Value::Bool { val, .. } => val.to_string(),
Value::Int { val, .. } => val.to_string(), Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(), Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => config.filesize.format(val).to_string(), Value::Filesize { val, .. } => val.to_string(),
Value::Duration { val, .. } => format_duration(val), Value::Duration { val, .. } => format_duration(val),
Value::Date { val, .. } => { Value::Date { val, .. } => {
format!("{} ({})", val.to_rfc2822(), HumanTime::from(val)) format!("{} ({})", val.to_rfc2822(), HumanTime::from(val))
@ -181,7 +174,7 @@ fn local_into_string(
Value::Glob { val, .. } => val, Value::Glob { val, .. } => val,
Value::List { vals: val, .. } => val Value::List { vals: val, .. } => val
.into_iter() .into_iter()
.map(|x| local_into_string(engine_state, x, ", ", config, serialize_types)) .map(|x| local_into_string(engine_state, x, ", ", serialize_types))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(separator), .join(separator),
Value::Record { val, .. } => val Value::Record { val, .. } => val
@ -191,7 +184,7 @@ fn local_into_string(
format!( format!(
"{}: {}", "{}: {}",
x, x,
local_into_string(engine_state, y, ", ", config, serialize_types) local_into_string(engine_state, y, ", ", serialize_types)
) )
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -221,7 +214,7 @@ fn local_into_string(
// that critical here // that critical here
Value::Custom { val, .. } => val Value::Custom { val, .. } => val
.to_base_value(span) .to_base_value(span)
.map(|val| local_into_string(engine_state, val, separator, config, serialize_types)) .map(|val| local_into_string(engine_state, val, separator, serialize_types))
.unwrap_or_else(|_| format!("<{}>", val.type_name())), .unwrap_or_else(|_| format!("<{}>", val.type_name())),
} }
} }