跳到主要内容

C++ tabulate 库详解:终端表格美化指南

简介

在 C++ 终端中打印美观的表格,tabulate 是一个非常好用的库。它支持 对齐、颜色、边框、列宽控制、自动换行,并且是 header-only 的库,使用起来非常方便。

1. 安装 tabulate

1.1 下载并手动集成

tabulate 是一个 header-only 库,无需编译或链接。只需下载 tabulate 源码包,解压后将 include/ 目录加入到项目的 include_directories 中,即可使用。

安装步骤

  1. 访问 GitHub: p-ranav/tabulate

  2. 下载最新的 tar.gzzip 源码包

  3. 解压并将 include/ 目录复制到项目的适当位置

  4. 在 CMake 或构建系统中添加:

    include_directories("path/to/tabulate/include")
  5. 代码中 #include <tabulate/table.hpp> 即可使用

说明

  • tabulate 是一个 header-only 库,仅需 include/ 目录即可使用。
  • 如果需要更简洁的单头文件版本,可以使用 single_include/ 目录下的 tabulate.hpp
  • tabulate 需要 C++11 及以上,但官方文档假定使用 C++17 进行开发。

1.2 使用 vcpkg 安装

vcpkg install tabulate

然后在 C++ 代码中:

#include <tabulate/table.hpp>

2. 创建基础表格

#include <iostream>
#include <tabulate/table.hpp>

using namespace tabulate;

int main() {
Table table;
table.add_row({"ID", "Name", "Age"});
table.add_row({"1", "Alice", "23"});
table.add_row({"2", "Bob", "30"});
table.add_row({"3", "Charlie", "27"});

std::cout << table << std::endl;
return 0;
}

输出

+----+---------+-----+
| ID | Name | Age |
+----+---------+-----+
| 1 | Alice | 23 |
| 2 | Bob | 30 |
| 3 | Charlie | 27 |
+----+---------+-----+

3. 设置对齐方式

// 设置表头居中
table[0].format().font_align(FontAlign::center);

// 第一列居中
table.column(0).format().font_align(FontAlign::center);

// 其余列右对齐
for (size_t i = 1; i < table[0].size(); ++i) {
table.column(i).format().font_align(FontAlign::right);
}

示例输出

+----+---------+-----+
| ID | Name | Age |
+----+---------+-----+
| 1 | Alice | 23 |
| 2 | Bob | 30 |
| 3 | Charlie | 27 |
+----+---------+-----+

4. 颜色 & 样式

#include <iostream>
#include <tabulate/table.hpp>

using namespace tabulate;

int main() {
Table table;
table.add_row({"ID", "Status", "Score"});
table.add_row({"1", "✔ Passed", "85"});
table.add_row({"2", "✘ Failed", "40"});
table.add_row({"3", "✔ Passed", "95"});

table[1][1].format().font_color(Color::green);
table[2][1].format().font_color(Color::red);
table[3][1].format().font_color(Color::green);

std::cout << table << std::endl;
return 0;
}

image-20250313101400329

#include <iostream>
#include <tabulate/table.hpp>

using namespace tabulate;

int main() {
Table table;
table.add_row({"ID", "Status", "Score"});
table.add_row({"1", "✔ Passed", "85"});
table.add_row({"2", "✘ Failed", "40"});
table.add_row({"3", "✔ Passed", "95"});

// **让 tabulate 识别 Unicode 字符宽度**
table.column(1).format().multi_byte_characters(true);

table[1][1].format().font_color(Color::green);
table[2][1].format().font_color(Color::red);
table[3][1].format().font_color(Color::green);

std::cout << table << std::endl;
return 0;
}

image-20250313101635936

5. 使用 RowStream 插入数据

RowStream 允许使用流式插入方式向表格中添加数据,更加灵活。

Table employees;
employees.add_row({"Emp. ID", "First Name", "Last Name", "Department / Business Unit", "Pay Rate"});

table.add_row(RowStream{} << 101 << "Donald" << "Patrick" << "Finance" << 59.6154);
table.add_row(RowStream{} << 102 << "Rachel" << "Williams" << "Marketing and Operational\nLogistics Planning" << 34.9707);
table.add_row(RowStream{} << 103 << "Ian" << "Jacob" << "IT Department" << 57.0048);

std::cout << employees << std::endl;

6. 设置列宽 & 自动换行

// 限制列宽
table.column(1).format().width(20);

如果文本超出宽度,将自动换行。

7. 自定义边框

table[0][0].format()
.border_top("*")
.border_bottom("-")
.border_left("^")
.border_right("|");

8. 结束语

tabulate 是一个功能强大但易用的 C++ 终端表格库,可以用来美化 CLI 输出,适用于日志、数据统计等场景。