Home Explore Blog CI



nushell

8th chunk of `book/working_with_tables.md`
9f9fdaea9588570bdd03697c05fa04124035bbeacc51cb5700000001000011f7
While the results _look_ the same, the `index` is now decoupled from the internal cell-path. For example:

```nu
ls | enumerate | flatten | sort-by modified | first 5
# => ╭────┬──────────────┬──────┬─────────┬──────────────╮
# => │  # │     name     │ type │  size   │   modified   │
# => ├────┼──────────────┼──────┼─────────┼──────────────┤
# => │  0 │ CNAME        │ file │    15 B │ 9 months ago │
# => │  2 │ LICENSE      │ file │ 1.0 KiB │ 9 months ago │
# => │  4 │ assets       │ dir  │ 4.0 KiB │ 9 months ago │
# => │ 17 │ lefthook.yml │ file │ 1.1 KiB │ 9 months ago │
# => │ 24 │ snippets     │ dir  │ 4.0 KiB │ 9 months ago │
# => ╰────┴──────────────┴──────┴─────────┴──────────────╯

ls | enumerate | flatten | sort-by modified | select 4
# => ╭────┬──────────┬──────┬─────────┬──────────────╮
# => │  # │   name   │ type │  size   │   modified   │
# => ├────┼──────────┼──────┼─────────┼──────────────┤
# => │ 24 │ snippets │ dir  │ 4.0 KiB │ 9 months ago │
# => ╰────┴──────────┴──────┴─────────┴──────────────╯
```

The `sort-by modified` now _also_ sorts the `index` along with the rest of the columns.

##### Adding a Row Header

```nu
let table = [
[additions   deletions   delta ];
[       10          20     -10 ]
[       15           5      10 ]
[        8           6       2 ]]

let totals_row = ($table | math sum | insert index {"Totals"})
$table | append $totals_row
# => ╭────────┬───────────┬───────────┬───────╮
# => │      # │ additions │ deletions │ delta │
# => ├────────┼───────────┼───────────┼───────┤
# => │      0 │        10 │        20 │   -10 │
# => │      1 │        15 │         5 │    10 │
# => │      2 │         8 │         6 │     2 │
# => │ Totals │        33 │        31 │     2 │
# => ╰────────┴───────────┴───────────┴───────╯
```

### The `table` command

The [`table`](/commands/docs/table.md) command is used to _render_ structured data. This includes:

- Tables
- Lists
- Records
- Ranges

Perhaps contrary to initial assumptions, the result of rendering these types is a `string`. For example:

```nu
[ Nagasaki Ghent Cambridge Izmir Graz Lubango ] | table | describe
# => string (stream)
```

Other data types are passed through the `table` command unchanged.

With no arguments, the output rendered from the `table` command will often _display_ the same as unrendered form. For example:

```nu
[ Nagasaki Ghent Cambridge Izmir Graz Lubango ]
# => ╭───┬───────────╮
# => │ 0 │ Nagasaki  │
# => │ 1 │ Ghent     │
# => │ 2 │ Cambridge │
# => │ 3 │ Izmir     │
# => │ 4 │ Graz      │
# => │ 5 │ Lubango   │
# => ╰───┴───────────╯
[ Nagasaki Ghent Cambridge Izmir Graz Lubango ] | table
# => ╭───┬───────────╮
# => │ 0 │ Nagasaki  │
# => │ 1 │ Ghent     │
# => │ 2 │ Cambridge │
# => │ 3 │ Izmir     │
# => │ 4 │ Graz      │
# => │ 5 │ Lubango   │
# => ╰───┴───────────╯
```

This can be useful when you need to store the rendered view of structured data as a string. For example, to remove all ANSI formatting (colors) from a table:

```nu
ls | table | ansi strip
```

The `table` command also has multiple options for _changing_ the rendering of a table, such as:

- `-e` to expand data that would normally be collapsed when rendering. Compare `scope modules | table` to `scope modules | table -e`.
- `-i false` to hide the `index`/`#` column
- `-a 5` to abbreviate the table to just the first and last 5 entries
- And more

Title: Sorting and Adding Row Headers with the Table Command in Nushell
Summary
This section demonstrates how sorting by a specific column also affects the index when the index is decoupled from the internal cell-path. It also shows how to add a "Totals" row to a table using `math sum` and `insert index`. Finally, the section introduces the `table` command, explaining its primary function to render structured data (tables, lists, records, ranges) as strings. It highlights that the default output often appears similar to the unrendered form but provides utilities to store the rendered view as a string (e.g., removing ANSI formatting) and customize table rendering (expand data, hide index, abbreviate).