mirror of
https://github.com/nushell/nushell.git
synced 2025-04-17 01:38:19 +02:00
Fix Format for non-basic data types (#5095)
This commit is contained in:
parent
657b631fdc
commit
d64cf1687e
@ -2,7 +2,8 @@ use nu_engine::CallExt;
|
|||||||
use nu_protocol::ast::{Call, PathMember};
|
use nu_protocol::ast::{Call, PathMember};
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
Category, Example, ListStream, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
Category, Config, Example, ListStream, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||||
|
Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -34,13 +35,14 @@ impl Command for Format {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let config = stack.get_config()?;
|
||||||
let specified_pattern: Result<Value, ShellError> = call.req(engine_state, stack, 0);
|
let specified_pattern: Result<Value, ShellError> = call.req(engine_state, stack, 0);
|
||||||
match specified_pattern {
|
match specified_pattern {
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
Ok(pattern) => {
|
Ok(pattern) => {
|
||||||
let string_pattern = pattern.as_string()?;
|
let string_pattern = pattern.as_string()?;
|
||||||
let ops = extract_formatting_operations(string_pattern);
|
let ops = extract_formatting_operations(string_pattern);
|
||||||
format(input, &ops, call.head)
|
format(input, &ops, call.head, &config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,7 +74,7 @@ enum FormatOperation {
|
|||||||
|
|
||||||
/// Given a pattern that is fed into the Format command, we can process it and subdivide it
|
/// Given a pattern that is fed into the Format command, we can process it and subdivide it
|
||||||
/// in two kind of operations.
|
/// in two kind of operations.
|
||||||
/// FormatOperation::FixedText contains a portion of the patter that has to be placed
|
/// FormatOperation::FixedText contains a portion of the pattern that has to be placed
|
||||||
/// there without any further processing.
|
/// there without any further processing.
|
||||||
/// FormatOperation::ValueFromColumn contains the name of a column whose values will be
|
/// FormatOperation::ValueFromColumn contains the name of a column whose values will be
|
||||||
/// formatted according to the input pattern.
|
/// formatted according to the input pattern.
|
||||||
@ -119,28 +121,33 @@ fn format(
|
|||||||
input_data: PipelineData,
|
input_data: PipelineData,
|
||||||
format_operations: &[FormatOperation],
|
format_operations: &[FormatOperation],
|
||||||
span: Span,
|
span: Span,
|
||||||
|
config: &Config,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let data_as_value = input_data.into_value(span);
|
let data_as_value = input_data.into_value(span);
|
||||||
|
|
||||||
// We can only handle a Record or a List of Record's
|
// We can only handle a Record or a List of Records
|
||||||
match data_as_value {
|
match data_as_value {
|
||||||
Value::Record { .. } => match format_record(format_operations, &data_as_value, span) {
|
Value::Record { .. } => {
|
||||||
Ok(value) => Ok(PipelineData::Value(Value::string(value, span), None)),
|
match format_record(format_operations, &data_as_value, span, config) {
|
||||||
Err(value) => Err(value),
|
Ok(value) => Ok(PipelineData::Value(Value::string(value, span), None)),
|
||||||
},
|
Err(value) => Err(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Value::List { vals, .. } => {
|
Value::List { vals, .. } => {
|
||||||
let mut list = vec![];
|
let mut list = vec![];
|
||||||
for val in vals.iter() {
|
for val in vals.iter() {
|
||||||
match val {
|
match val {
|
||||||
Value::Record { .. } => match format_record(format_operations, val, span) {
|
Value::Record { .. } => {
|
||||||
Ok(value) => {
|
match format_record(format_operations, val, span, config) {
|
||||||
list.push(Value::string(value, span));
|
Ok(value) => {
|
||||||
|
list.push(Value::string(value, span));
|
||||||
|
}
|
||||||
|
Err(value) => {
|
||||||
|
return Err(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(value) => {
|
}
|
||||||
return Err(value);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ShellError::UnsupportedInput(
|
return Err(ShellError::UnsupportedInput(
|
||||||
@ -167,13 +174,14 @@ fn format_record(
|
|||||||
format_operations: &[FormatOperation],
|
format_operations: &[FormatOperation],
|
||||||
data_as_value: &Value,
|
data_as_value: &Value,
|
||||||
span: Span,
|
span: Span,
|
||||||
|
config: &Config,
|
||||||
) -> Result<String, ShellError> {
|
) -> Result<String, ShellError> {
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
for op in format_operations {
|
for op in format_operations {
|
||||||
match op {
|
match op {
|
||||||
FormatOperation::FixedText(s) => output.push_str(s.as_str()),
|
FormatOperation::FixedText(s) => output.push_str(s.as_str()),
|
||||||
|
|
||||||
// The referenced code suggest to use the correct Span's
|
// The referenced code suggests to use the correct Spans
|
||||||
// See: https://github.com/nushell/nushell/blob/c4af5df828135159633d4bc3070ce800518a42a2/crates/nu-command/src/commands/strings/format/command.rs#L61
|
// See: https://github.com/nushell/nushell/blob/c4af5df828135159633d4bc3070ce800518a42a2/crates/nu-command/src/commands/strings/format/command.rs#L61
|
||||||
FormatOperation::ValueFromColumn(col_name) => {
|
FormatOperation::ValueFromColumn(col_name) => {
|
||||||
match data_as_value
|
match data_as_value
|
||||||
@ -182,7 +190,9 @@ fn format_record(
|
|||||||
val: col_name.clone(),
|
val: col_name.clone(),
|
||||||
span,
|
span,
|
||||||
}]) {
|
}]) {
|
||||||
Ok(value_at_column) => output.push_str(value_at_column.as_string()?.as_str()),
|
Ok(value_at_column) => {
|
||||||
|
output.push_str(value_at_column.into_string(", ", config).as_str())
|
||||||
|
}
|
||||||
Err(se) => return Err(se),
|
Err(se) => return Err(se),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user