From 6a0c88d516faf9fd1165fddbd7ef69ba47f97cc6 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Thu, 18 May 2023 03:19:07 +0330 Subject: [PATCH] Fmt f64 (#9142) Fixes: #9131 As octal and some other format not valid for f64 we have to specify it . just wonder if need for generic impl or no for just one type ? --- crates/nu-command/src/conversions/fmt.rs | 37 +++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/conversions/fmt.rs b/crates/nu-command/src/conversions/fmt.rs index 581268648c..e717583491 100644 --- a/crates/nu-command/src/conversions/fmt.rs +++ b/crates/nu-command/src/conversions/fmt.rs @@ -82,13 +82,14 @@ fn fmt( fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { match input { + Value::Float { val, .. } => fmt_it_64(*val, span), Value::Int { val, .. } => fmt_it(*val, span), Value::Filesize { val, .. } => fmt_it(*val, span), // Propagate errors by explicitly matching them before the final case. Value::Error { .. } => input.clone(), other => Value::Error { error: Box::new(ShellError::OnlySupportsThisInputType { - exp_input_type: "integer or filesize".into(), + exp_input_type: "float , integer or filesize".into(), wrong_type: other.get_type().to_string(), dst_span: span, src_span: other.expect_span(), @@ -131,6 +132,40 @@ fn fmt_it(num: i64, span: Span) -> Value { Value::Record { cols, vals, span } } +fn fmt_it_64(num: f64, span: Span) -> Value { + let mut cols = vec![]; + let mut vals = vec![]; + + cols.push("binary".into()); + vals.push(Value::string(format!("{:b}", num.to_bits()), span)); + + cols.push("debug".into()); + vals.push(Value::string(format!("{num:#?}"), span)); + + cols.push("display".into()); + vals.push(Value::string(format!("{num}"), span)); + + cols.push("lowerexp".into()); + vals.push(Value::string(format!("{num:#e}"), span)); + + cols.push("lowerhex".into()); + vals.push(Value::string(format!("{:0x}", num.to_bits()), span)); + + cols.push("octal".into()); + vals.push(Value::string(format!("{:0o}", num.to_bits()), span)); + + // cols.push("pointer".into()); + // vals.push(Value::string(format!("{:#p}", &num), span)); + + cols.push("upperexp".into()); + vals.push(Value::string(format!("{num:#E}"), span)); + + cols.push("upperhex".into()); + vals.push(Value::string(format!("{:0X}", num.to_bits()), span)); + + Value::Record { cols, vals, span } +} + #[cfg(test)] mod test { use super::*;