Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Styling

cols supports terminal colors and text attributes via the optional color feature, which uses the colored crate.

cols = { version = "0.2", features = ["color"] }

CellStyle

The CellStyle struct describes the visual appearance of a cell: foreground color, background color, and text attributes (bold, italic, underline, etc.).

use cols::CellStyle;
use cols::Color;

let style = CellStyle::new()
    .fg(Color::Green)
    .bg(Color::Black)
    .bold();

Available builder methods:

MethodEffect
.fg(Color)Set the foreground (text) color
.bg(Color)Set the background color
.bold()Bold text
.italic()Italic text
.underline()Underlined text
.dimmed()Dimmed/faint text
.strikethrough()Strikethrough text

cols re-exports Color from the colored crate, so you don’t need to add colored as a separate dependency. The Color enum supports the 8 standard colors (Red, Green, Blue, etc.), their bright variants (BrightRed, etc.), ANSI 256 codes (AnsiColor(n)), and 24-bit true color (TrueColor { r, g, b }).

Applying styles

Styles can be set at three levels:

Column level — the default style for all data cells in the column, and a separate style for the header:

use cols::{Column, CellStyle};
use cols::Color;

Column::new("STATUS")
    .header_style(CellStyle::new().bold())
    .style(CellStyle::new().fg(Color::Green));

Line level — overrides the column style for every cell in that line:

table.line_mut(row).style_set(CellStyle::new().fg(Color::Red).bold());

Cell level — overrides both column and line styles for a single cell:

table.line_mut(row)
    .cell_mut(0).unwrap()
    .style_set(CellStyle::new().fg(Color::Yellow));

Cascading

Styles cascade from column to line to cell. At each level, only the fields that are explicitly set override the parent. Unset fields are inherited.

For example, if a column sets bold + green:

  • A line that sets fg: red produces bold red (bold inherited from column)
  • A cell on that line that sets underline produces bold red underline (bold from column, red from line, underline from cell)

This means you can set a base style on the column and only override what changes at the line or cell level.

use cols::{Table, Column, CellStyle};
use cols::Color;

let mut table = Table::new();
table.colors_set(true);

// All cells in this column are bold green by default.
table.add_column(
    Column::new("STATUS")
        .header_style(CellStyle::new().bold().underline())
        .style(CellStyle::new().fg(Color::Green).bold()),
);

let ok = table.new_line(None);
table.line_mut(ok).data_set(0, "OK");
// This line renders as bold green (inherited from column).

let err = table.new_line(None);
table.line_mut(err).data_set(0, "ERROR");
// Override just the color for this line — bold is inherited.
table.line_mut(err).style_set(CellStyle::new().fg(Color::Red));
// This line renders as bold red.

Enabling color output

Styles are only applied when color output is enabled on the table:

table.colors_set(true);

When disabled (the default), styles are stored but ignored during printing. This lets callers decide at runtime whether to use colors — for example, based on whether stdout is a terminal.