Using Colors
Tabular supports both predefined terminal colors and custom RGB colors for text foreground and background.
Color Enum
Use the Color enum for standard terminal colors:
Table table;
table . addRow ({ "Status" , "Message" })
. addRow ({ "Success" , "Operation completed" })
. addRow ({ "Error" , "Operation failed" });
// Green foreground for success
table [ 1 ][ 0 ]. style (). fg ( Color ::Green);
// Red foreground for error
table [ 2 ][ 0 ]. style (). fg ( Color ::Red);
// Blue foreground for message
table [ 1 ][ 1 ]. style (). fg ( Color ::Blue);
table [ 2 ][ 1 ]. style (). fg ( Color ::Yellow);
render ( table . str () + ' \n ' , stdout);
RGB Colors
For precise color control, use the Rgb struct:
Table table;
table . addRow ({ "ID" , "Dev" , "Task" , "Priority" , "Status" })
. addRow ({ "001" , "Alice Carlisle" , "Authentication" , "High" , "In Progress" })
. addRow ({ "002" , "Henry Dawson" , "Database" , "Medium" , "Completed" })
. addRow ({ "003" , "Carol Bennett" , "Frontend" , "High" , "Testing" })
. addRow ({ "004" , "Amelia Turner" , "API" , "Low" , "Not Started" })
. addRow ({ "005" , "Charlotte Whitaker" , "Documentation" , "Medium" , "In Progress" });
// Define priority colors
Rgb high = { 255 , 0 , 0 }; // Red
Rgb medium = { 255 , 165 , 0 }; // Orange
Rgb low = { 165 , 255 , 0 }; // Light green
// Apply base colors to developer names
table [ 1 ][ 1 ]. style (). base (high);
table [ 2 ][ 1 ]. style (). base (medium);
table [ 3 ][ 1 ]. style (). base (high);
table [ 4 ][ 1 ]. style (). base (low);
table [ 5 ][ 1 ]. style (). base (medium);
// Apply foreground colors to priority column
table [ 1 ][ 3 ]. style (). fg (high);
table [ 2 ][ 3 ]. style (). fg (medium);
table [ 3 ][ 3 ]. style (). fg (high);
table [ 4 ][ 3 ]. style (). fg (low);
table [ 5 ][ 3 ]. style (). fg (medium);
render ( table . str () + ' \n ' , stdout);
Text Attributes
Apply text styling attributes using the attrs() method:
Bold and Underline
Dim and Italic
Underline Numbers
Table table;
table . addRow ({ "Product" , "Price" , "Status" })
. addRow ({ "Laptop" , "$999" , "Available" });
// Bold header
table [ 0 ][ 0 ]. style (). attrs ( Attr ::Bold);
// Bold with underline using bitwise OR
table [ 0 ][ 2 ]. style (). attrs ( Attr ::Bold | Attr ::Underline);
Available Attributes
Attr::Bold - Bold text
Attr::Dim - Dimmed text
Attr::Italic - Italic text
Attr::Underline - Single underline
Attr::Dunderline - Double underline (not widely supported)
Attr::Blink - Blinking text
Attr::Flink - Fast blink (very rare support)
Attr::Reverse - Reverse video
Attr::Concealed - Hidden text
Attr::Crossed - Crossed-out text
Text attribute support depends on your terminal emulator. Some attributes may not work in all terminals.
Column Alignment
Control text alignment within columns using the align() configuration:
Individual Cells
Entire Table
Specific Column
Table table;
table . addRow ({ "Left" , "Center" , "Right" })
. addRow ({ "A" , "B" , "C" });
table [ 0 ][ 0 ]. config (). align ( Align ::Left);
table [ 0 ][ 1 ]. config (). align ( Align ::Center);
table [ 0 ][ 2 ]. config (). align ( Align ::Right);
Table table;
table . addRow ({ "ID" , "Dev" , "Task" , "Priority" , "Status" })
. addRow ({ "001" , "Alice Carlisle" , "Authentication" , "High" , "In Progress" });
// Center all columns
for ( auto & row : table . rows ()) {
for ( auto & column : row . columns ()) {
column . config (). align ( Align ::Center);
}
}
Table table;
table . addRow ({ "Product" , "Price" , "Stock" })
. addRow ({ "Laptop" , "$999" , "15" })
. addRow ({ "Mouse" , "$25" , "150" });
// Right-align prices
for ( int i = 0 ; i < 3 ; ++ i) {
table [i][ 1 ]. config (). align ( Align ::Right);
}
Padding Configuration
Customize cell padding using the Padd struct:
Table table;
table . addRow ({
" __ ___. .__ \n "
" _/ |______ \\ _ |__ __ __| | _____ _______ \n "
" \\ __ \\ __ \\ | __ \\ | | \\ | \\\\ __ \\ _ __ \\\n "
" | | / __ \\ | \\ _ \\ \\ | / |__/ __ \\ | | \\ / \n "
" |__| (____ /___ /____/|____(____ /__| \n "
" \\ / \\ / \\ / \n " ,
})
. addRow ({
"lightweight header-only library for constructing well-formatted, "
"fully-customizable CLI tables."
});
// No padding for ASCII art (horizontal and vertical)
table [ 0 ][ 0 ]. config (). padd ( Padd ( 0 , 0 ));
// Center align the description
table [ 1 ][ 0 ]. config (). align ( Align ::Center);
// Don't skip empty whitespaces at the start of lines
// This is important when spaces at the start are part of the design
table [ 0 ][ 0 ]. config (). skipEmptyLineIndent ( false );
table . config (). width ( 48 );
table . border ( Border :: rounded ());
render ( table . str () + ' \n ' , stdout);
Padd Constructor:
Padd() - Default padding
Padd(horizontal, vertical) - Set horizontal (left/right) and vertical (top/bottom) padding
Complete Styling Example
Here’s a comprehensive example combining colors, attributes, alignment, and custom widths:
#include "tabular/table.h"
#include "tabular/render.h"
void adjustWidth ( Table & table )
{
// Set specific widths for each column
for ( int i = 0 ; i < 6 ; ++ i) table [i][ 0 ]. config (). width ( 5 );
for ( int i = 0 ; i < 6 ; ++ i) table [i][ 1 ]. config (). width ( 20 );
for ( int i = 0 ; i < 6 ; ++ i) table [i][ 2 ]. config (). width ( 16 );
for ( int i = 0 ; i < 6 ; ++ i) table [i][ 3 ]. config (). width ( 10 );
for ( int i = 0 ; i < 6 ; ++ i) table [i][ 4 ]. config (). width ( 13 );
// Total: 5 + 20 + 16 + 10 + 13 = 64
// With borders: 64 + 6 = 70
table . config (). width ( 70 );
}
void styleTable ( Table & table )
{
// Underline the ID numbers
for ( int i = 1 ; i < 6 ; ++ i) table [i][ 0 ]. style (). attrs ( Attr ::Underline);
// Priority colors
Rgb high = { 255 , 0 , 0 };
Rgb medium = { 255 , 165 , 0 };
Rgb low = { 165 , 255 , 0 };
// Color developer names by priority
table [ 1 ][ 1 ]. style (). base (high);
table [ 2 ][ 1 ]. style (). base (medium);
table [ 3 ][ 1 ]. style (). base (high);
table [ 4 ][ 1 ]. style (). base (low);
table [ 5 ][ 1 ]. style (). base (medium);
// Dim task descriptions
for ( int i = 1 ; i < 6 ; ++ i) table [i][ 2 ]. style (). attrs ( Attr ::Dim);
// Color priorities
table [ 1 ][ 3 ]. style (). fg (high);
table [ 2 ][ 3 ]. style (). fg (medium);
table [ 3 ][ 3 ]. style (). fg (high);
table [ 4 ][ 3 ]. style (). fg (low);
table [ 5 ][ 3 ]. style (). fg (medium);
// Color status
table [ 1 ][ 4 ]. style (). fg ( Color ::Blue);
table [ 2 ][ 4 ]. style (). fg ( Color ::Green);
table [ 3 ][ 4 ]. style (). fg ( Color ::Yellow);
table [ 4 ][ 4 ]. style (). fg ( Color ::Red);
table [ 5 ][ 4 ]. style (). fg ( Color ::Blue);
}
void alignColumns ( Table & table )
{
// Center all columns
for ( auto & row : table . rows ()) {
for ( auto & column : row . columns ()) {
column . config (). align ( Align ::Center);
}
}
}
int main ()
{
using namespace tabular ;
Table table;
table . addRow ({ "ID" , "Dev" , "Task" , "Priority" , "Status" })
. addRow ({ "001" , "Alice Carlisle" , "Authentication" , "High" , "In Progress" })
. addRow ({ "002" , "Henry Dawson" , "Database" , "Medium" , "Completed" })
. addRow ({ "003" , "Carol Bennett" , "Frontend" , "High" , "Testing" })
. addRow ({ "004" , "Amelia Turner" , "API" , "Low" , "Not Started" })
. addRow ({ "005" , "Charlotte Whitaker" , "Documentation" , "Medium" , "In Progress" });
adjustWidth (table);
styleTable (table);
alignColumns (table);
table . border ( Border :: rounded ());
render ( table . str () + ' \n ' , stdout);
return 0 ;
}
This example creates a styled task management table with:
Underlined ID numbers
Priority-based colored developer names and priority values
Dimmed task descriptions
Colored status indicators
Center-aligned content
Custom column widths
Rounded borders