cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::utils::table_t Class Reference

格式化打印表格工具/Formatted table printing utility More...

#include <print.hpp>

Public Member Functions

 table_t (const print_style_t &style=get_default_style())
 构造函数 / Constructor
 
table_tset_headers (const std::vector< std::string > &hdrs)
 设置表头 / Set table_t headers
 
table_tadd_row (const std::vector< std::string > &row)
 添加一行数据 / Add a data row
 
template<typename... Args, std::enable_if_t< !(sizeof...(Args)==1 &&std::is_same_v< std::decay_t< std::tuple_element_t< 0, std::tuple< Args... > > >, std::vector< std::string > >), int > = 0>
auto add_row (Args &&... args) -> table_t &
 添加一行数据(可变参数)/ Add a row with variadic arguments
 
table_tset_title (const std::string &title)
 设置表格标题 / Set table title text
 
table_tset_footer (const std::string &footer)
 设置表格尾部文本 / Set table footer text
 
table_tset_column_align (size_t column_index, align_t align)
 设置指定列的对齐方式 / Set alignment for a specific column
 
table_tset_all_columns_align (align_t align)
 设置所有列的对齐方式 / Set alignment for all columns
 
table_tset_column_width (size_t col, size_t width)
 为指定列设置固定宽度 / Set fixed width for a column
 
table_tset_column_min_max (size_t col, size_t min_width, size_t max_width)
 为指定列设置最小和最大宽度约束 / Set min and max width constraints for a column
 
table_tset_row_height (size_t row, size_t height)
 为指定行设置固定行高 / Set fixed height for a row
 
table_tenable_wrap (bool enable)
 启用或禁用自动内容换行 / Enable or disable automatic content wrapping
 
table_tset_wrap_ellipsis (const std::string &ellipsis)
 设置截断时的省略符 / Set ellipsis string when truncating
 
table_tspan_cells (size_t start_row, size_t start_col, size_t row_span, size_t col_span)
 合并单元格 / Merge cells
 
table_tenable_zebra (bool enable)
 启用或禁用斑马纹 / Enable or disable zebra striping
 
table_tset_zebra_colors (color_t odd_bg, color_t even_bg)
 设置斑马纹的奇偶行背景色 / Set zebra background colors for odd/even rows
 
table_tset_highlight_callback (std::function< print_style_t(size_t, size_t, const std::string &)> cb)
 设置条件高亮回调 / Set conditional highlight callback
 
table_tset_output_color (bool enable)
 控制终端输出时是否保留颜色码 / Control whether to keep ANSI colors for terminal output
 
table_tset_file_output_color (bool enable)
 控制写入文件时是否保留颜色码 / Control whether to keep ANSI colors for file output
 
auto to_string (bool with_color=true) const -> std::string
 将表格渲染为字符串 / Render table as a string
 

Friends

CPP_TOOLBOX_EXPORT std::ostream & operator<< (std::ostream &os, const table_t &tbl)
 打印表格 / Stream operator for printing table_t
 

Detailed Description

格式化打印表格工具/Formatted table printing utility

支持以下高级特性/Supports the following advanced features:

  • 自动列宽/Auto column width
  • 最小/最大宽度约束/Min/max width constraints
  • 内容换行或截断/Content wrapping and truncation
  • 单元格合并(水平/垂直)/Cell merging (horizontal/vertical)
  • 斑马纹(隔行变色)/Zebra striping (alternating row colors)
  • 条件高亮/Conditional highlighting
  • 文件/终端颜色输出切换/File/terminal color output switching
table_t tbl;
tbl.set_headers({"Name", "Age", "Role"});
tbl.add_row("Alice", 25, "Engineer");
tbl.add_row("Bob", 30, "Manager");
std::cout << tbl;
// Output:
// +--------+-----+----------+
// | Name | Age | Role |
// +--------+-----+----------+
// | Alice | 25 | Engineer |
// | Bob | 30 | Manager |
// +--------+-----+----------+
格式化打印表格工具/Formatted table printing utility
Definition print.hpp:467
table_t & add_row(const std::vector< std::string > &row)
添加一行数据 / Add a data row
table_t & set_headers(const std::vector< std::string > &hdrs)
设置表头 / Set table_t headers

Constructor & Destructor Documentation

◆ table_t()

toolbox::utils::table_t::table_t ( const print_style_t style = get_default_style())
explicit

构造函数 / Constructor

Member Function Documentation

◆ add_row() [1/2]

template<typename... Args, std::enable_if_t< !(sizeof...(Args)==1 &&std::is_same_v< std::decay_t< std::tuple_element_t< 0, std::tuple< Args... > > >, std::vector< std::string > >), int > = 0>
auto toolbox::utils::table_t::add_row ( Args &&...  args) -> table_t&
inline

添加一行数据(可变参数)/ Add a row with variadic arguments

Template Parameters
Args参数类型/Parameter types
Parameters
args行数据,每个参数对应一列的值 / Row data, each argument corresponds to a column value
Returns
table_t& 当前对象的引用 / Reference to this table_t
t.add_row("Alice", 30, "Engineer");

◆ add_row() [2/2]

table_t & toolbox::utils::table_t::add_row ( const std::vector< std::string > &  row)

添加一行数据 / Add a data row

Parameters
row字符串向量,每个元素对应一列 / Vector of strings, each for a column
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ enable_wrap()

table_t & toolbox::utils::table_t::enable_wrap ( bool  enable)

启用或禁用自动内容换行 / Enable or disable automatic content wrapping

Parameters
enabletrue: 启用换行, false: 截断 / true to wrap, false to truncate
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ enable_zebra()

table_t & toolbox::utils::table_t::enable_zebra ( bool  enable)

启用或禁用斑马纹 / Enable or disable zebra striping

Parameters
enabletrue: 启用斑马纹, false: 禁用 / true to enable zebra
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_all_columns_align()

table_t & toolbox::utils::table_t::set_all_columns_align ( align_t  align)

设置所有列的对齐方式 / Set alignment for all columns

Parameters
align对齐方式 / Alignment
Returns
table& 当前对象的引用 / Reference to this table

◆ set_column_align()

table_t & toolbox::utils::table_t::set_column_align ( size_t  column_index,
align_t  align 
)

设置指定列的对齐方式 / Set alignment for a specific column

Parameters
column_index列索引 / Column index
align对齐方式 / Alignment
Returns
table& 当前对象的引用 / Reference to this table

◆ set_column_min_max()

table_t & toolbox::utils::table_t::set_column_min_max ( size_t  col,
size_t  min_width,
size_t  max_width 
)

为指定列设置最小和最大宽度约束 / Set min and max width constraints for a column

Parameters
col列索引,从0开始 / Column index (0-based)
min_width最小宽度 / Minimum width
max_width最大宽度 / Maximum width
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_column_width()

table_t & toolbox::utils::table_t::set_column_width ( size_t  col,
size_t  width 
)

为指定列设置固定宽度 / Set fixed width for a column

Parameters
col列索引,从0开始 / Column index (0-based)
width固定字符宽度 / Fixed width in characters
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_file_output_color()

table_t & toolbox::utils::table_t::set_file_output_color ( bool  enable)

控制写入文件时是否保留颜色码 / Control whether to keep ANSI colors for file output

Parameters
enabletrue: 保留颜色, false: 不保留 / true to keep colors, false to strip
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_footer()

table_t & toolbox::utils::table_t::set_footer ( const std::string &  footer)

设置表格尾部文本 / Set table footer text

Parameters
footer尾部文本 / Footer text
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_headers()

table_t & toolbox::utils::table_t::set_headers ( const std::vector< std::string > &  hdrs)

设置表头 / Set table_t headers

Parameters
hdrs表头字符串向量 / Vector of header strings
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_highlight_callback()

table_t & toolbox::utils::table_t::set_highlight_callback ( std::function< print_style_t(size_t, size_t, const std::string &)>  cb)

设置条件高亮回调 / Set conditional highlight callback

Parameters
cb回调函数:接收(row, col, cell_value)并返回print_style_t / Callback receiving (row, col, value) returning style
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_output_color()

table_t & toolbox::utils::table_t::set_output_color ( bool  enable)

控制终端输出时是否保留颜色码 / Control whether to keep ANSI colors for terminal output

Parameters
enabletrue: 保留颜色, false: 不保留 / true to keep colors, false to strip
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_row_height()

table_t & toolbox::utils::table_t::set_row_height ( size_t  row,
size_t  height 
)

为指定行设置固定行高 / Set fixed height for a row

Parameters
row行索引,从0开始 / Row index (0-based)
height固定行数 / Fixed number of printed lines
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_title()

table_t & toolbox::utils::table_t::set_title ( const std::string &  title)

设置表格标题 / Set table title text

Parameters
title标题文本 / Title text
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_wrap_ellipsis()

table_t & toolbox::utils::table_t::set_wrap_ellipsis ( const std::string &  ellipsis)

设置截断时的省略符 / Set ellipsis string when truncating

Parameters
ellipsis省略符文本 / Ellipsis text (e.g. "...")
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ set_zebra_colors()

table_t & toolbox::utils::table_t::set_zebra_colors ( color_t  odd_bg,
color_t  even_bg 
)

设置斑马纹的奇偶行背景色 / Set zebra background colors for odd/even rows

Parameters
odd_bg奇数行背景色 / Background color for odd rows
even_bg偶数行背景色 / Background color for even rows
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ span_cells()

table_t & toolbox::utils::table_t::span_cells ( size_t  start_row,
size_t  start_col,
size_t  row_span,
size_t  col_span 
)

合并单元格 / Merge cells

Parameters
start_row起始行索引 / Starting row index
start_col起始列索引 / Starting column index
row_span垂直合并跨度 / Number of rows to span vertically
col_span水平合并跨度 / Number of columns to span horizontally
Returns
table_t& 当前对象的引用 / Reference to this table_t

◆ to_string()

auto toolbox::utils::table_t::to_string ( bool  with_color = true) const -> std::string

将表格渲染为字符串 / Render table as a string

Parameters
with_color是否保留ANSI颜色 / true to keep ANSI colors
Returns
std::string 渲染后的表格 / Rendered table string

Friends And Related Symbol Documentation

◆ operator<<

CPP_TOOLBOX_EXPORT std::ostream & operator<< ( std::ostream &  os,
const table_t tbl 
)
friend

打印表格 / Stream operator for printing table_t

Parameters
os输出流 / Output stream
tbl要打印的table_t对象 / Table object to print
Returns
std::ostream& 输出流引用 / Reference to output stream

The documentation for this class was generated from the following file: