mirror of
https://github.com/nushell/nushell.git
synced 2026-11-26 03:25:39 +08:00
## Description In this PR, I added striped rows to `table` and `explore`: every other data row gets a different background, which makes wide tables easier to read across. <img src="https://cdn.discordapp.com/attachments/615329862395101194/1572010038610894980/CleanShot_2026-11-17_at_09.06.242x.png?ex=6afccfef&is=6afb7e6f&hm=b78f4a5e3bd4c1655bf3f5023dd237c751d711492569690a0b1dc0b7834fe854" /> Striping is turned on with `$env.config.table.striped`, and its colors come from `color_config`, as plain colors like the rest of it: `row_stripe_bg` for the first row and every other one, `row_stripe_alt` for the rows between them. When only `row_stripe_bg` is given, as `"#rrggbb"`, the alternate color is worked out from it: its WCAG 2 relative luminance[^luminance] decides whether white or black is laid over it, composited at `$env.config.table.stripe_blend` (default `0.08`), so stripes are slightly lighter on a dark background and slightly darker on a light one. With neither color set there are no stripes. For stripes that follow the terminal, `std/config` gets two commands, `terminal-background` and `terminal-foreground`, in the way of `std/clip paste52`: they ask the terminal with `term query` and return the color as `"#rrggbb"`, or nothing from a terminal that does not report its colors, or when stdin or stdout is not a terminal. The cursor position is asked for after the color, so a terminal that does not report its colors still answers and nothing waits. They are for `config.nu`, once, when nushell starts; `doc_config.nu` has the lines for it. In `table`, stripes are painted onto the rendered text between the outer borders, which leaves the existing color logic untouched, and the background is put back after any escape sequence in a cell that resets it. With `table --expand`, only the rows of the outer table are striped. In `explore`, the row backgrounds are set on the ratatui buffer, under cells that have no background of their own, and follow the rows while scrolling. [^luminance]: The brightness of a color as the eye perceives it, from 0 (black) to 1 (white): each sRGB channel is made linear, then they are summed as `0.2126 R + 0.7152 G + 0.0722 B`, green counting most. Defined in [WCAG 2.2](https://www.w3.org/TR/WCAG22/#dfn-relative-luminance). A background above 0.179, where black and white contrast with it equally, counts as light. ## User-facing changes (Release notes) ### Striped table rows `table` and `explore` can now draw every other row on a different background. It is off by default, and takes a color to make the stripes from: ```nu $env.config.table.striped = true $env.config.color_config.row_stripe_bg = "#282c34" # the background of your terminal ``` The stripe is a little lighter than a dark `row_stripe_bg` and a little darker than a light one. `$env.config.table.stripe_blend` sets how strong it is, from `0.0` to `1.0` (default `0.08`), or set its color yourself: ```nu $env.config.color_config.row_stripe_alt = "#393d44" # the rows between ``` To take `row_stripe_bg` from the terminal when nushell starts: ```nu use std/config terminal-background if $nu.is-interactive and $env.config.table.striped { let background = terminal-background if $background != null { $env.config.color_config.row_stripe_bg = $background } } ``` `terminal-background` returns nothing from a terminal that does not report its background, which leaves tables without stripes. Keys typed before the terminal answers are lost, so this belongs where nushell starts, not at each prompt. `terminal-foreground` gives the color of the text the same way. ## Additional notes - No new dependency, and nothing that differs between platforms: the PR touches `nu-table`, `nu-explore`, the config and its documentation, and `std/config`. - `row_stripe_bg` is painted on its rows, so setting it to the terminal's background shows on a terminal with a transparent background or a background image. `row_stripe_alt` alone leaves those rows as they are. - Not striped: tables nested in a `table --expand` cell, the markdown and reStructuredText themes, `table --collapse`, and the transposed layout of `explore`. - `terminal-background` and `terminal-foreground` have no automated test, as they need a terminal. They were tried against a fake terminal (one that reports colors, as `rgb:` or `rgba:`, with channels of one, two and four digits and either string terminator; one that does not; stdout sent to a file; and no terminal at all), not in a real one. - `term query` has no timeout: where nothing answers either query, as in a pty with no terminal behind it, the commands wait until Ctrl+C. Over ssh to Windows they return nothing, as the terminal's color reply does not get through. `explore` is covered by widget tests, not run on a live terminal. `typos` was not run locally.