Skip to main content

Column Styling

Each column can be individually styled using the style() method. The Style class provides control over text colors, background colors, and text attributes.
table[0][0].style().fg(Color::Red)
                  .bg(Color::White)
                  .attrs(Attr::Bold);

Color Options

Tabular supports two color systems: terminal colors and RGB colors.

Terminal Colors

Use predefined terminal color constants:
table[0][0].style().fg(Color::BrightRed);
table[0][0].style().bg(Color::Blue);
Available terminal colors:
  • Color::Black
  • Color::Red
  • Color::Green
  • Color::Yellow
  • Color::Blue
  • Color::Magenta
  • Color::Cyan
  • Color::White
  • Color::BrightBlack (Gray)
  • Color::BrightRed
  • Color::BrightGreen
  • Color::BrightYellow
  • Color::BrightBlue
  • Color::BrightMagenta
  • Color::BrightCyan
  • Color::BrightWhite

RGB Colors

For precise color control, use RGB values:
// RGB struct: {red, green, blue}
table[0][0].style().fg(Rgb{255, 0, 0});    // Bright red
table[0][0].style().bg(Rgb{0, 255, 0});    // Bright green
RGB color support depends on your terminal emulator. Most modern terminals support RGB (true color), but some older ones may fall back to 256-color or 16-color palettes.

Color Types

Foreground Color (fg)

Sets the text color:
// Using terminal color
table[0][0].style().fg(Color::Blue);

// Using RGB
table[0][0].style().fg(Rgb{100, 150, 200});

Background Color (bg)

Sets the background color behind the text:
// Using terminal color
table[0][0].style().bg(Color::Green);

// Using RGB
table[0][0].style().bg(Rgb{50, 50, 50});

Base Color (base)

Sets the background color for the entire column cell, including padding:
// Using terminal color
table[0][0].style().base(Color::Yellow);

// Using RGB
table[0][0].style().base(Rgb{255, 255, 200});
The base() color fills the entire column width, while bg() only colors behind the actual text content.

Text Attributes

Text attributes modify the appearance of text beyond color.

Available Attributes

Attr::Bold       // Bold/bright text
Attr::Dim        // Dimmed text
Attr::Italic     // Italic text
Attr::Underline  // Single underline
Attr::Dunderline // Double underline (rare support)
Attr::Blink      // Blinking text
Attr::Flink      // Fast blink (very rare support)
Attr::Reverse    // Swap foreground and background
Attr::Concealed  // Hidden text
Attr::Crossed    // Strikethrough text

Setting Single Attribute

table[0][0].style().attrs(Attr::Bold);
table[0][1].style().attrs(Attr::Italic);
table[0][2].style().attrs(Attr::Underline);

Combining Multiple Attributes

Use the bitwise OR operator (|) to combine attributes:
// Bold and underlined
table[0][0].style().attrs(Attr::Bold | Attr::Underline);

// Bold, italic, and underlined
table[0][0].style().attrs(Attr::Bold | Attr::Italic | Attr::Underline);
Text attribute support varies by terminal emulator. Not all terminals support all attributes, especially Dunderline, Flink, and Concealed.

Styling Examples

Header Styling

Table table;
table.addRow({"Product", "Price", "Status"})
     .addRow({"Laptop", "$999", "Available"});

// Style header row
table[0][0].style().attrs(Attr::Bold)
                  .fg(Color::Blue);

table[0][1].style().attrs(Attr::Bold)
                  .fg(Color::Blue);

table[0][2].style().attrs(Attr::Bold)
                  .fg(Color::Blue);

Status Indicators

// Green background for available
table[1][2].style().fg(Color::Black)
                  .bg(Color::Green)
                  .attrs(Attr::Bold);

// Red background for unavailable
table[2][2].style().fg(Color::White)
                  .bg(Color::Red)
                  .attrs(Attr::Bold);

Price Highlighting

// Highlight price with custom RGB colors
table[1][1].style().fg(Rgb{0, 200, 0})      // Green text
                  .bg(Rgb{240, 240, 240})   // Light gray background
                  .attrs(Attr::Bold);

Complete Styled Table Example

Table table;
table.addRow({"Product", "Price", "Status"})
     .addRow({"Laptop", "$999", "Available"});

// Header row - bold blue text
table[0][0].style().attrs(Attr::Bold)
                  .fg(Color::Blue);

// Price column - green with background
table[1][1].style().fg(Color::Black)
                  .bg(Color::Green)
                  .attrs(Attr::Bold);

// Multiple attributes
table[0][2].style().attrs(Attr::Bold | Attr::Underline);

// Base color fills entire cell
table[1][0].style().base(Color::BrightBlack);

Resetting Styles

You can reset individual style components or all styles:
// Reset individual colors
table[0][0].style().resetFg();
table[0][0].style().resetBg();
table[0][0].style().resetBase();
table[0][0].style().resetAttrs();

// Reset all styles
table[0][0].style().reset();

Style Method Chaining

All style methods return a reference to the Style object, allowing for fluent chaining:
table[0][0].style()
    .fg(Color::White)
    .bg(Color::Blue)
    .base(Color::BrightBlack)
    .attrs(Attr::Bold | Attr::Italic);

Color Summary

MethodDescriptionParametersAffects
fg()Foreground (text) colorColor or RgbText only
bg()Background colorColor or RgbBehind text
base()Base colorColor or RgbEntire cell
attrs()Text attributesAttr or combined with |Text appearance

Build docs developers (and LLMs) love