From 79d9a0542fcad231630068049332b5c7cb786302 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Sun, 16 Jul 2023 15:04:35 +0200 Subject: [PATCH] allow `into filesize` to take tables as input / output (#9706) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description i have the following command that should give a table of all the mounted devices with information about their sizes, etc, etc... a glorified output for the `df -h` command: ```nushell def disk [] { df -h | str replace "Mounted on" "Mountpoint" | detect columns | rename filesystem size used avail used% mountpoint | into filesize size used avail | upsert used% {|it| 100 * (1 - $it.avail / $it.size)} } ``` this should work given the first example of `into filesize` ```nushell Convert string to filesize in table > [[bytes]; ['5'] [3.2] [4] [2kb]] | into filesize bytes ``` ## before this PR it does not even parse ```nushell Error: nu::parser::input_type_mismatch × Command does not support table input. ╭─[entry #1:5:1] 5 │ | rename filesystem size used avail used% mountpoint 6 │ | into filesize size used avail · ──────┬────── · ╰── command doesn't support table input 7 │ | upsert used% {|it| 100 * (1 - $it.avail / $it.size)} ╰──── ``` > **Note** > this was working before the recent input / output type changes ## with this PR it parses again and gives ```nushell > disk | where mountpoint == "/" | into record ╭────────────┬───────────────────╮ │ filesystem │ /dev/sda2 │ │ size │ 217.9 GiB │ │ used │ 158.3 GiB │ │ avail │ 48.4 GiB │ │ used% │ 77.77777777777779 │ │ mountpoint │ / │ ╰────────────┴───────────────────╯ ``` > **Note** > the two following commands also work now and did not before the PR > ```nushell > ls | insert name_size {|it| $it.name | str length} | into filesize name_size > ``` > ```nushell > [[device size]; ["/dev/sda1" 200] ["/dev/loop0" 50]] | into filesize size > ``` # User-Facing Changes `into filesize` works back with tables and this effectively fixes the doc. # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` this PR gives a `result` back to the first table example to make sure it works fine. # After Submitting --- .../src/conversions/into/filesize.rs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/conversions/into/filesize.rs b/crates/nu-command/src/conversions/into/filesize.rs index 1f89e89b21..071900e42a 100644 --- a/crates/nu-command/src/conversions/into/filesize.rs +++ b/crates/nu-command/src/conversions/into/filesize.rs @@ -21,7 +21,9 @@ impl Command for SubCommand { (Type::Number, Type::Filesize), (Type::String, Type::Filesize), (Type::Filesize, Type::Filesize), + (Type::Table(vec![]), Type::Table(vec![])), ]) + .vectorizes_over_list(true) .rest( "rest", SyntaxShape::CellPath, @@ -54,8 +56,40 @@ impl Command for SubCommand { vec![ Example { description: "Convert string to filesize in table", - example: "[[bytes]; ['5'] [3.2] [4] [2kb]] | into filesize bytes", - result: None, + example: r#"[[device size]; ["/dev/sda1" "200"] ["/dev/loop0" "50"]] | into filesize size"#, + result: Some(Value::List { + vals: vec![ + Value::Record { + cols: vec!["device".to_string(), "size".to_string()], + vals: vec![ + Value::String { + val: "/dev/sda1".to_string(), + span: Span::test_data(), + }, + Value::Filesize { + val: 200, + span: Span::test_data(), + }, + ], + span: Span::test_data(), + }, + Value::Record { + cols: vec!["device".to_string(), "size".to_string()], + vals: vec![ + Value::String { + val: "/dev/loop0".to_string(), + span: Span::test_data(), + }, + Value::Filesize { + val: 50, + span: Span::test_data(), + }, + ], + span: Span::test_data(), + }, + ], + span: Span::test_data(), + }), }, Example { description: "Convert string to filesize",