ansi
for platform
Output ANSI codes to change color and style of text.
Signature
> ansi {flags} (code)
Flags
--escape, -e
: escape sequence without the escape character(s) ('\x1b[' is not required)--osc, -o
: operating system command (osc) escape sequence without the escape character(s) ('\x1b]' is not required)--list, -l
: list available ansi code names
Parameters
code
: The name of the code to use (fromansi -l
).
Input/output types:
input | output |
---|---|
nothing | any |
Examples
Change color to green (see how the next example text will be green!)
> ansi green
Reset all styles and colors
> ansi reset
Use different colors and styles in the same text
> $'(ansi red_bold)Hello(ansi reset) (ansi green_dimmed)Nu(ansi reset) (ansi purple_italic)World(ansi reset)'
Hello Nu World
The same example as above with short names
> $'(ansi rb)Hello(ansi rst) (ansi gd)Nu(ansi rst) (ansi pi)World(ansi rst)'
Hello Nu World
Avoid resetting color when setting/resetting different style codes
> $'Set color to (ansi g)GREEN then style to (ansi bo)BOLD(ansi rst_bo) or (ansi d)DIMMED(ansi rst_d) or (ansi i)ITALICS(ansi rst_i) or (ansi u)UNDERLINE(ansi rst_u) or (ansi re)REVERSE(ansi rst_re) or (ansi h)HIDDEN(ansi rst_h) or (ansi s)STRIKE(ansi rst_s) then (ansi rst)reset everything'
Set color to GREEN then style to BOLD or DIMMED or ITALICS or UNDERLINE or REVERSE or HIDDEN or STRIKE then reset everything
Use escape codes, without the '\x1b['
> $"(ansi --escape '3;93;41m')Hello(ansi reset)" # italic bright yellow on red background
Hello
Use simple hex string
> $"(ansi '#4169E1')Hello(ansi reset)" # royal blue foreground color
Hello
Use structured escape codes
> let bold_blue_on_red = { # `fg`, `bg`, `attr` are the acceptable keys, all other keys are considered invalid and will throw errors.
fg: '#0000ff'
bg: '#ff0000'
attr: b
}
$"(ansi --escape $bold_blue_on_red)Hello, Nu World!(ansi reset)"
Hello, Nu World!
Notes
An introduction to what ANSI escape sequences are can be found in the
]8;;https://en.wikipedia.org/wiki/ANSI_escape_code\ANSI escape code]8;;\ Wikipedia page.
Escape sequences usual values:
╭────┬────────────┬────────┬────────┬─────────╮
│ # │ type │ normal │ bright │ name │
├────┼────────────┼────────┼────────┼─────────┤
│ 0 │ foreground │ 30 │ 90 │ black │
│ 1 │ foreground │ 31 │ 91 │ red │
│ 2 │ foreground │ 32 │ 92 │ green │
│ 3 │ foreground │ 33 │ 93 │ yellow │
│ 4 │ foreground │ 34 │ 94 │ blue │
│ 5 │ foreground │ 35 │ 95 │ magenta │
│ 5 │ foreground │ 35 │ 95 │ purple │
│ 6 │ foreground │ 36 │ 96 │ cyan │
│ 7 │ foreground │ 37 │ 97 │ white │
│ 8 │ foreground │ 39 │ │ default │
│ 9 │ background │ 40 │ 100 │ black │
│ 10 │ background │ 41 │ 101 │ red │
│ 11 │ background │ 42 │ 102 │ green │
│ 12 │ background │ 43 │ 103 │ yellow │
│ 13 │ background │ 44 │ 104 │ blue │
│ 14 │ background │ 45 │ 105 │ magenta │
│ 14 │ background │ 45 │ 105 │ purple │
│ 15 │ background │ 46 │ 106 │ cyan │
│ 16 │ background │ 47 │ 107 │ white │
│ 17 │ background │ 49 │ │ default │
╰────┴────────────┴────────┴────────┴─────────╯
Escape sequences style attributes:
╭────┬────┬──────────────┬─────────────────────────────────────────╮
│ # │ id │ abbreviation │ description │
├────┼────┼──────────────┼─────────────────────────────────────────┤
│ 0 │ 0 │ rst │ reset / normal display │
│ 1 │ 1 │ bo │ bold on │
│ 2 │ 2 │ d │ dimmed on │
│ 3 │ 3 │ i │ italic on (non-mono font) │
│ 4 │ 4 │ u │ underline on │
│ 5 │ 5 │ bl │ blink on │
│ 6 │ 6 │ bf │ fast blink on │
│ 7 │ 7 │ r │ reverse video on │
│ 8 │ 8 │ h │ hidden (invisible) on │
│ 9 │ 9 │ s │ strike-through on │
│ 10 │ 21 │ rst_bo │ bold or dimmed off │
│ 11 │ 22 │ du │ double underline (not widely supported) │
│ 12 │ 23 │ rst_i │ italic off (non-mono font) │
│ 13 │ 24 │ rst_u │ underline off │
│ 14 │ 25 │ rst_bl │ blink off │
│ 15 │ 26 │ │ <reserved> │
│ 16 │ 27 │ rst_r │ reverse video off │
│ 17 │ 28 │ rst_h │ hidden (invisible) off │
│ 18 │ 29 │ rst_s │ strike-through off │
╰────┴────┴──────────────┴─────────────────────────────────────────╯
Operating system commands:
╭───┬─────┬───────────────────────────────────────╮
│ # │ id │ description │
├───┼─────┼───────────────────────────────────────┤
│ 0 │ 0 │ Set window title and icon name │
│ 1 │ 1 │ Set icon name │
│ 2 │ 2 │ Set window title │
│ 3 │ 4 │ Set/read color palette │
│ 4 │ 9 │ iTerm2 Grown notifications │
│ 5 │ 10 │ Set foreground color (x11 color spec) │
│ 6 │ 11 │ Set background color (x11 color spec) │
│ 7 │ ... │ others │
╰───┴─────┴───────────────────────────────────────╯
Subcommands:
name | description | type |
---|---|---|
ansi gradient | Add a color gradient (using ANSI color codes) to the given string. | built-in |
ansi link | Add a link (using OSC 8 escape sequence) to the given string. | built-in |
ansi strip | Strip ANSI escape sequences from a string. | built-in |