Display Attributes

Urwid supports a number of common display attributes in monochrome, 16-color, 88-color, 256-color, and 24-bit (true color) modes.

You are encouraged to provide support for as many of these modes as you like, while allowing your interface to degrade gracefully by providing command line arguments or other interfaces to switch modes.

When setting up a palette with MainLoop (or directly on your screen instance), you may specify attributes for 16-color, monochrome and high color modes. You can then switch between these modes with screen.set_terminal_properties(), where screen is your screen instance or MainLoop.screen. The same method turns on terminal hyperlinks with hyperlinks=True (see Hyperlinks).

The raw display starts with the number of colors that TERM, its terminfo entry and the environment give. As it starts, it asks the terminal for its own capabilities (XTGETTCAP) and its version (XTVERSION). A reply can raise the number of colors and turn on the underline styles, and the screen is then drawn again. RGB counts when it has no value or one that names the bits per component, as 8; xterm answers -1 when it keeps colors in its palette.

Once the terminal reported that it switched to the alternate screen and answered those queries, and a frame was drawn, the screen asks once how the terminal stores a 24-bit background: it sets one, asks for the rendition (DECRQSS), and writes the rendition of its last frame again. A terminal that keeps the 24-bit value draws 24-bit colors, and one that keeps a 256-color palette entry draws 256 colors; any other answer is no evidence. The number of colors is decided by the first of these that gives one:

  1. the application, with screen.set_terminal_properties() or by assigning screen.colors;

  2. NO_COLOR, CLICOLOR=0, FORCE_COLOR or CLICOLOR_FORCE in the environment;

  3. the answer to the rendition question, which may also lower a number of colors the guesses below gave;

  4. the capability and version replies, which only raise it; the version replies of Konsole, tmux and xterm.js (the VS Code terminal) mean 24-bit colors, which tmux converts for the terminal around it;

  5. TERM and its terminfo entry, and on Windows 10 the console’s version.

Variables that name a terminal, such as COLORTERM, WT_SESSION or TERM_PROGRAM, are not read: a terminal or multiplexer started inside another one inherits them, and they then describe the wrong terminal. A terminal that draws 24-bit colors and answers none of the queries gets 256 colors; FORCE_COLOR=3 gives it 24-bit colors.

With those queries it also asks for the terminal’s default foreground and background colors (OSC 10 and OSC 11), which display.raw.Screen.terminal_default_colors then holds, and with which the Terminal widget answers its child’s queries of the same colors. Like the other queries, they are not sent on the Linux console, nor when the application set both the number of colors and the underline styles.

Using Display Attributes

Once you have defined a palette you may use its display attribute names anywhere that expects a display attribute. When no display attribute is defined None is used as a default display attribute.

None will typically be rendered with the terminal’s default foreground and background colors.

You can also specify an exact foreground and background using an AttrSpec instance instead of a display attribute name. Every display urwid provides draws any AttrSpec: a color it does not have is drawn as the nearest one it has, as Colors the Display Does Not Have describes, so a 24-bit AttrSpec can be used on any of them.

Text Markup

A Text widget can specify which display attributes each part of the text will use with the format defined in Text class reference. Some examples:

Text("a simple string with default attribute")

The string and space around will use the None default display attribute which usually appears in the terminal’s default foreground and background.

Text(('attr1', "a string in display attribute attr1"))

The string will appear with foreground and backgrounds specified in the display module’s palette for 'attr1', but the space around (before/after) the text will appear with the default display attribute.

Text(["a simple string ", ('attr1', "ending with attr1")])

The first three words have the default display attribute and the last three words have display attribute 'attr1'.

Text([('attr1', "start in attr1 "), ('attr2', "end in attr2")])

The first three words have display attribute 'attr1' and the last three words have display attribute 'attr2'.

Text(('attr1', ["nesting example ", ('attr2', "inside"), " outside"]))

When markup is nested the inner attribute is placed over the outer one. Here "inside" has attribute 'attr2' and all the rest of the text has attribute 'attr1'. A color that 'attr2' leaves to 'inherit' is taken from 'attr1' (see Inherited Foreground and Background). Text.get_text() returns such a run as LayeredAttr('attr2', 'attr1').

Assigning Display Attributes with AttrMap

If you want a whole widget to be assigned a display attribute, or if you want to change one or more display attributes to other display attributes, you can wrap your widget in an AttrMap widget. Text widgets have no way to specify a display attribute for the whitespace around the text caused by alignment and wrapping so AttrMap may be used. Some examples:

AttrMap(Text("hello"), 'attr1')

The whole Text widget will have display attribute 'attr1' including whitespace around the "hello" text.

AttrMap(Text(('attr1', "hello")), 'attr2')

The "hello" text will appear with display attribute 'attr1' and all surrounding whitespace will appear with display attribute 'attr2'.

AttrMap(Text([('attr1', "hello"), " world"]), {'attr1': 'attr2'})

The AttrMap widget will apply display attribute 'attr2' to all parts of the Text widget that are using 'attr1'. The result is the "hello" text appearing with display attribute 'attr2' and all other text and whitespace appearing in the default display attribute.

The attributes of the wrapped widget are placed over the attribute AttrMap gives to None, so a color they leave to 'inherit' is taken from it:

AttrMap(Pile([Text(('red_fg', "error")), Text("plain")]), 'panel_bg')

With ('red_fg', 'dark red', 'inherit') and ('panel_bg', 'inherit', 'dark blue') in the palette, "error" is dark red and all other text and whitespace is in the default foreground, everything on dark blue.

AttrMap can also change display attributes differently when they are in focus. This can be used to “highlight” one or more widgets to make your interface more user friendly. To use this feature set the focus_map parameter when creating the AttrMap widget.

Foreground and Background Settings

Supported by Terminal

xterm / gnome-term

rxvt

linux console

others

16 standard foreground colors

YES

YES

YES

very widely supported

8 standard background colors

YES

YES

YES

very widely supported

default foreground/background

YES

YES

YES

widely supported

bold, underline, standout

YES

YES

standout

widely supported

italics

YES

YES

NO

widely supported

blink

YES/NO

NO

NO

some support

strikethrough

YES

NO

NO

some supported

faint

YES

NO

NO

some support

underline styles and color

gnome-term only

NO

NO

kitty, Konsole, VTE

“bright” background colors

YES

urxvt

some support

256-color foreground/background

YES

some support

88-color foreground/background

w/palette setting

urxvt

limited support

RGB palette setting

YES

limited support

16 Standard Foreground Colors

  • 'black'

  • 'dark red'

  • 'dark green'

  • 'brown'

  • 'dark blue'

  • 'dark magenta'

  • 'dark cyan'

  • 'light gray'

  • 'dark gray'

  • 'light red'

  • 'light green'

  • 'yellow'

  • 'light blue'

  • 'light magenta'

  • 'light cyan'

  • 'white'

8 Standard Background Colors

  • 'black'

  • 'dark red'

  • 'dark green'

  • 'brown'

  • 'dark blue'

  • 'dark magenta'

  • 'dark cyan'

  • 'light gray'

Default Foreground and Background

  • 'default' (or simply '')

'default' may be specified as a foreground or background to use a terminal’s default color. For terminals with transparent backgrounds 'default' is the only way to show the transparent background. There is no way to tell what the default colors are, so it is best to use default foregrounds and backgrounds together (not with other colors) to ensure good contrast.

Inherited Foreground and Background

  • 'inherit'

'inherit' may be specified as a foreground or background color (with settings: 'inherit,bold') to take that color from the enclosing display attribute: the attribute of the outer markup, or the attribute an AttrMap around the widget gives to None. With nothing enclosing it, the color comes from the None palette entry, so registering (None, 'default', 'dark magenta') gives every attribute with an 'inherit' background a dark magenta background. A color is resolved separately for each run of text, so the text after a run with its own background shows the enclosing background again.

This lets a few foreground-only and background-only palette entries replace an entry for every combination of them. To highlight the focused row of a list whose text uses many foreground colors, give those entries an 'inherit' background and wrap each row:

palette = [
    ('method', 'light green', 'inherit'),
    ('size', 'brown', 'inherit'),
    ('focus_bg', 'inherit', 'dark blue'),
]
row = AttrMap(Columns([Text(('method', "GET")), Text(('size', "1k"))]), None, focus_map='focus_bg')

When an attribute inherits a color, a setting ('bold', 'underline', …) it does not mention is taken from the enclosing attribute, and 'no-' turns one off: with ('plain', 'inherit,no-bold', 'inherit') inside a bold attribute, the text is not bold. An attribute that sets both colors, including 'default' and '', hides the enclosing attribute completely, settings included. Attributes are combined from the outermost inward, so each one keeps what all of the attributes around it set. ANSIText turns an SGR reset such as ESC[24m into 'no-underline', so the reset also applies to the enclosing attribute. It keeps bold apart from the color: ESC[1;31m is 'dark red,bold', which the raw display draws in light red where the terminal shows bold text in bright colors.

A change to a palette entry shows on the next MainLoop.draw_screen(), and neither the widgets nor the screen have to be redrawn for it: the raw and web displays compare each drawn row by its resolved colors and write again only the rows whose colors changed, and curses writes every row on each draw.

To switch to another theme, screen.register_palette(theme, replace=True) registers the new entries and removes the ones the theme does not name, keeping the screen’s None entry unless the theme sets it. BaseScreen.palette is a read-only view of the registered entries.

AttrSpec.layered_over() combines two specifications. Canvases record the enclosing attributes as a LayeredAttr; a custom display module turns any canvas attribute into an AttrSpec with BaseScreen.resolve_attr() instead of reading the palette itself. An AttrMap can replace a LayeredAttr as a whole by using it as a key.

Code written for earlier releases sees these differences, also with palettes that never use 'inherit':

  • Nested markup and attributes inside an AttrMap appear as LayeredAttr in Text.get_text() and in canvas content, where they used to be the innermost name.

  • A display module that reads self._palette itself shows layered attributes in default colors; it calls BaseScreen.resolve_attr() instead.

  • An attribute an AttrMap maps to None shows the map’s None attribute.

  • AttrSpec.copy_modified() without colors keeps the 88-color or true-color mode of the specification, and uses 256 colors otherwise, instead of the fewest colors it needs. A color it is not given keeps its kind and number.

  • AttrSpec.layered_over() without colors, and BaseScreen.resolve_attr() at every index but 2, keep the kind of each color for layers of different color modes, where they used to convert every color to the larger mode.

  • repr() of a true color AttrSpec names colors=16777216.

  • ANSIText, Terminal and TerminalOutput keep a palette entry beside a 24-bit color as an entry.

Bold, Underline, Standout

  • 'bold'

  • 'underline'

  • 'standout'

  • 'blink'

  • 'italics'

  • 'strikethrough'

  • 'faint'

These settings may be tagged on to foreground colors using commas, eg: 'light gray,underline,bold,strikethrough'. 'no-' in front of one turns it off over an enclosing attribute (see Inherited Foreground and Background).

For monochrome mode combinations of these are the only values that may be used.

Many terminals will turn foreground colors into their bright versions when you use bold, eg: 'dark blue,bold' might look the same as 'light blue'. Some terminals also will display bright colors in a bold font even if you don’t specify bold. To inhibit this you can try setting bright_is_bold=False with display.raw.Screen.set_terminal_properties(), but it is not always supported.

'standout' is usually displayed as the foreground and background colors reversed.

Underline Styles and Color

  • 'double underline'

  • 'curly underline'

  • 'dotted underline'

  • 'dashed underline'

Each of these settings is an underline in that style, used in place of 'underline': 'yellow,curly underline'. 'no-underline' turns any of them off.

The underline has a color of its own, given to AttrSpec as underline_color in any form a background color takes; 'default' draws it in the text color, and without one the color of the enclosing attribute is taken:

AttrSpec("default,curly underline", "default", underline_color="light red")

The raw display draws the styles and the color where the terminal does, and an underline in the text color and single elsewhere. It finds them in the terminal’s terminfo entry (Smulx or Setulc), or in the terminal’s replies to its capability and version queries; the version replies of VTE, Konsole, iTerm2, WezTerm, Ghostty, foot, mintty, Rio and xterm.js (the VS Code terminal) are known. It does not read the variables that name these terminals, such as VTE_VERSION, because a terminal started from inside another one inherits them. underline_styles given to display.raw.Screen or display.raw.Screen.set_terminal_properties() turns them on or off. The web displays draw them as the text decoration’s style and color; the curses display draws a single underline.

“Bright” Background Colors

Warning

Terminal support for bright background colors is spotty, and they generally should be avoided. If you are in a high-color mode you might have better luck using the high-color versions 'h8', 'h9', 'h10', …, 'h15'.

  • 'dark gray'

  • 'light red'

  • 'light green'

  • 'yellow'

  • 'light blue'

  • 'light magenta'

  • 'light cyan'

  • 'white'

24-Bit Foreground and Background Colors

In 24-bit color mode, any hex color code of the form #rrggbb can be used to specify a precise RGB value for foreground and background colors. Support for 24-bit color mode varies widely among terminal programs. Furthermore, terminal multiplexers such as tmux and screen can sometimes interfere with the operation of 24-bit color mode unless properly configured.

In 24-bit mode, 'h0' to 'h255' are entries of the 256-color palette, kept as entries: the raw display writes 38;5;n for them, so a terminal whose theme redefines an entry draws the redefined color. A cube or gray shortcut such as '#f00' or 'g50' is a 24-bit color in this mode.

See also

The palette_test.py example program

Colors the Display Does Not Have

An AttrSpec keeps the kind of each of its colors, the foreground, the background and the underline color: a basic color, an entry of the 88- or the 256-color palette, or a 24-bit color. One specification may hold several kinds, such as a palette entry beside a 24-bit background, and combining display attributes (see Inherited Foreground and Background) keeps each color’s kind. An 88-color entry above 15 that is combined with a color of another mode becomes its RGB value.

Each display converts the colors once, as it draws them:

  • A display without colors draws the default colors.

  • The basic colors, and the palette entries 0 to 15, which the terminal theme defines, keep their number wherever the display has them; the curses display, which draws 8 colors, draws 8 to 15 as 0 to 7.

  • A palette entry above 15 that the display has is kept. An 88-color entry on a display of 256 or 24-bit colors is drawn as its own color: the nearest 256-color entry, or its RGB value.

  • Any other color is drawn as the nearest color the display has, by the distance of the red, green and blue values: among the basic colors on a display of 16 colors, among the first 8 on curses, and among the entries above 15 of the palette of an 88- or 256-color display.

  • The web and HTML displays draw every color as it is.

The settings and the hyperlink are never changed.

256-Color Foreground and Background Colors

In 256-color mode you have the 16 basic colors, a 6 * 6 * 6 color cube and a gray scale with 24 entries (white and black not included).

The color cube is weighted towards the brighter colors, with RGB points at 0, 0x5f, 0x87, 0xaf, 0xd7 and 0xff. The hex characters '0', '6', '8', 'a', 'd' and 'f' are used as short-forms for these values.

High colors may be specified by their index 'h0', …, 'h255' or with the shortcuts for the color cube '#000', '#006', '#008', …, '#fff' or gray scale entries 'g0' (black from color cube) , 'g3', 'g7', … 'g100' (white from color cube).

See also

The palette_test.py example program

88-Color Foreground and Background Colors

In 88-color mode you have the 16 basic colors, a 4 * 4 * 4 color cube and a gray scale with 8 entries (white and black not included).

The color cube is weighted towards the brighter colors, with RGB points at 0, 0x8b, 0xcd, and 0xff. The hex characters '0', '8', 'c' and 'f' are used as short-forms for these values.

High colors may be specified by their index 'h0', …, 'h87' or with the shortcuts for the color cube '#000', '#008', '#00c', …, '#fff' or gray scale entries 'g0' (black from color cube), 'g19', 'g35', … 'g100' (white from color cube).

See also

The palette_test.py example program

RGB Palette Setting

A few terminals have the ability to customize the terminal palette’s RGB values with display.raw.Screen.modify_terminal_palette(). There is no automatic way to tell if this is supported by a user’s terminal, so this feature shouldn’t be relied on.

Most terminals take the xterm OSC 4 sequence. On the Linux console (TERM=linux and its variants) the console’s own sequence is used instead, which covers only the 16 basic colors: entries 16-255 are skipped there. Check which terminal is in use (Screen.term) before relying on a modified palette. The modified entries are reset to the terminal’s default when the screen stops; the Linux console resets all 16 entries, and fbterm, which has no reset sequence, keeps them.

display.raw.Screen.reset_default_terminal_palette() is used to reset the palette in the palette_test.py example program when switching modes.