文雅地运用 C++ 制造表格:tabulate
论文翻译:Speech Enhancement Based on the General Transfer Function GSC and Postfiltering
0x00 引见 tabulate
是一个运用 C++ 17 编写的库,它能够制造表格。运用它,把表格对齐、格式化和着色,不在话下!你以至能够运用 tabulate,将你的表格导出为 Markdown 代码。下图是一个运用 tabulate 制造的表格输出在敕令行的样例:
固然,除了表格,你还能够玩出名堂。瞥见下面这个马里奥了吗?这也是用 tabulate 制造的!源码在 。
0x10 预备
起首你须要装置 。
建立一个文件夹(下文用 X 替代),作为你运用 tabulate 的处所。再将 这个文件夹下载到 X 里。然后在 X 里建立 main.cpp
以及一个 CMakeLists.txt
。
注重:须要下载
include
全部文件夹而不是仅仅下载tabulate
文件夹你能够点击 下载 tabulate 项目,然后将
include
文件夹复制到 X 中。
将下面的代码复制进 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.8)
# 这里的 tabulateDemo 能够换为你喜好的名字
project(tabulateDemo)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(include)
add_executable(main main.cpp)
末了 X 文件夹的构造应当是如许的:
.
├── CMakeLists.txt
├── include
│ └── tabulate
└── main.cpp
请仔细查对好 X 的构造!
可前去 查对文件构造。
0x20 小试技艺
将下面这段代码复制进 main.cpp
中:
#include "tabulate/table.hpp"
using namespace std;
using namespace tabulate;
int main()
{
Table hellogithub; // 建立一个叫做 hellogithub 的 Table 对象
hellogithub.add_row({"HelloGitHub"});
hellogithub.add_row({"hellogithub.com"});
hellogithub[1][0].format()
.font_style({FontStyle::underline});
hellogithub.add_row({"github.com/521xueweihan/HelloGitHub"});
hellogithub[2][0].format()
.font_style({FontStyle::underline});
hellogithub.add_row({"xueweihan NB!!!"});
cout << hellogithub << endl;
return 0;
}
- 假如你运用的是 Linux/MacOS 体系,请在终端进入 X 文件夹并输入以下敕令:
mkdir build cd build cmake .. make ./main
- 假如你运用的是 Windows 体系和 MinGW,请搜检是不是装置
mingw32-make.exe
,并在终端中进入 X 文件夹,输入:mkdir build cd build cmake .. mingw32-make ./main.exe
- 假如你运用 Windows 以及 MSVC,在终端中输入:
mkdir build cd build cmake ..
然后运用 Visual Studio 翻开 build
文件夹下的 tabulateDemo.sln
来运转。
假如没有问题,那末你应当会在终端里看到:
0x30 格式化表格
请先仔细剖析 0x20 小试技艺 章节中的代码并尝试着修正一下它!
0x31 Word Wrapping
为了防备表格中的内容太长致使不整洁,你能够指定表格每一列的宽度,tabulate 就会自动帮你换行。语法以下:
// 将表格第 0 行第 0 列的宽度设为20
table[0][0].format().width(20);
除了自动换行,你也能够在内容中运用 n
来手动设置换行。
这是一个 Word Wrapping 的例子:
#include "tabulate/table.hpp"
using namespace std;
using namespace tabulate;
int main()
{
Table table;
table.add_row({"This paragraph contains a veryveryveryveryveryverylong word. The long word will break and word wrap to the next line.",
"This paragraph nhas embedded 'n' ncharacters andn will breakn exactly wheren you want itn ton break."});
table[0][0].format().width(20); // 设置第 0 行第 0 列的宽度为 20
table[0][1].format().width(50); // 设置第 0 行第 1 列的宽度为 50
cout << table << endl;
return 0;
}
return 0;
}
- 第 0 行第 0 列的笔墨是不是是很长?然则设置了它的宽度后,就不必忧郁了。tabulate 将会帮你自动换行。假如不设置的话,表格就会变得很不整洁,你也能够尝试一下。
- 第 0 行第 1 列的内容里运用了
n
的换行符,所以纵然我们给它设置了 50 的宽度,也会先依据内容里的n
换行符来换行。 - 值得注重的是,tabulate 会自动删除每一行内容双方的空缺字符。
0x32 字体对齐
tabulate 支撑三种对齐设置:左、中和右。默许情况下,全部内容都邑靠左对齐。
要手动设置对齐体式格局,能够运用 .format().font_align(方向)
。
举一个例子:
#include "tabulate/table.hpp"
using namespace std;
using namespace tabulate;
int main()
{
Table hellogithub;
hellogithub.add_row({"HelloGitHub"});
hellogithub[0][0].format()
.font_align(FontAlign::center); // 设置居中对齐
hellogithub.add_row({"hellogithub.com"});
hellogithub[1][0].format()
.font_align(FontAlign::left); // 设置靠左对齐
hellogithub.add_row({"github.com/521xueweihan/HelloGitHub"});
hellogithub[2][0].format()
.font_align(FontAlign::center); // 设置居中对齐
hellogithub.add_row({"xueweihan NB!!!"});
hellogithub[3][0].format()
.font_align(FontAlign::right); // 设置靠右对齐
hellogithub[0][0].format().width(50);
cout << hellogithub << endl;
return 0;
}
0x33 字体款式
tabulate 支撑以下八种字体款式:
- 粗体
bold
- 深色
dark
- 斜体
italic
- 下划线
underline
- 闪灼
blink
- 翻转
reverse
- 隐蔽
concealed
- 删除线
crossed
某些款式可能会由于终端的缘由而没法显现。
如:粗体、深色、斜体、闪灼等款式,请慎用。
要运用这些款式,能够挪用 .format().font_style({...})
。款式也能够叠加运用。
0x34 色彩
你能够对表格的字体、边框、角以及列分开标记设置它们的远景或背景色彩。
tabulate 支撑 8 种色彩:
- 灰色
gray
- 赤色
red
- 绿色
green
- 黄色
yellow
- 蓝色
blue
- 洋赤色
magenta
- 青色
cyan
- 白色
white
能够经由过程 .format().<element>_color(色彩)
的体式格局定义远景色或经由过程 .format().<element>_background_color(色彩)
的体式格局定义背景色。
#include "tabulate/table.hpp"
using namespace tabulate;
using namespace std;
int main()
{
Table colors;
colors.add_row({"Font Color is Red", "Font Color is Blue", "Font Color is Green"});
colors.add_row({"Everything is Red", "Everything is Blue", "Everything is Green"});
colors.add_row({"Font Background is Red", "Font Background is Blue", "Font Background is Green"});
colors[0][0].format()
.font_color(Color::red)
.font_style({FontStyle::bold});
colors[0][1].format()
.font_color(Color::blue)
.font_style({FontStyle::bold});
colors[0][2].format()
.font_color(Color::green)
.font_style({FontStyle::bold});
colors[1][0].format()
.border_left_color(Color::red)
.border_left_background_color(Color::red)
.font_background_color(Color::red)
.font_color(Color::red);
colors[1][1].format()
.border_left_color(Color::blue)
.border_left_background_color(Color::blue)
.font_background_color(Color::blue)
.font_color(Color::blue);
colors[1][2].format()
.border_left_color(Color::green)
.border_left_background_color(Color::green)
.font_background_color(Color::green)
.font_color(Color::green)
.border_right_color(Color::green)
.border_right_background_color(Color::green);
colors[2][0].format()
.font_background_color(Color::red)
.font_style({FontStyle::bold});
colors[2][1].format()
.font_background_color(Color::blue)
.font_style({FontStyle::bold});
colors[2][2].format()
.font_background_color(Color::green)
.font_style({FontStyle::bold});
cout << colors << endl;
return 0;
}
0x35 边框、角
你能够对表格的边框和角的文本、色彩或背景色彩举行自定义。
你能够运用 .corner(..)
、.corner_color(..)
和 corner_background_color(..)
来对一切的角设置一个配合的款式。你也能够运用 .border(..)
、.border_color(..)
和 .border_background_color(..)
来对一切的边框设置一个配合的款式。
这是一个零丁设定一切边框和角的示例:
#include <tabulate/table.hpp>
using namespace tabulate;
int main()
{
Table table;
table.add_row({"ᛏᚺᛁᛊ ᛁᛊ ᚨ ᛊᛏᛟᚱy ᛟᚠᚨ ᛒᛖᚨᚱ ᚨᚾᛞn"
"ᚨ ᚹᛟᛚᚠ, ᚹᚺᛟ ᚹᚨᚾᛞᛖᚱᛖᛞ ᛏᚺᛖn"
"ᚱᛖᚨᛚᛗᛊ ᚾᛁᚾᛖ ᛏᛟ ᚠᚢᛚᚠᛁᛚᛚ ᚨ ᛈᚱᛟᛗᛁᛊᛖn"
"ᛏᛟ ᛟᚾᛖ ᛒᛖᚠᛟᚱᛖ; ᛏᚺᛖy ᚹᚨᛚᚲ ᛏᚺᛖn"
"ᛏᚹᛁᛚᛁᚷᚺᛏ ᛈᚨᛏᚺ, ᛞᛖᛊᛏᛁᚾᛖᛞ ᛏᛟn"
"ᛞᛁᛊcᛟᚹᛖᚱ ᛏᚺᛖ ᛏᚱᚢᛏᚺnᛏᚺᚨᛏ ᛁᛊ ᛏᛟ cᛟᛗᛖ."});
table.format()
.multi_byte_characters(true)
// Font styling
.font_style({FontStyle::bold, FontStyle::dark})
.font_align(FontAlign::center)
.font_color(Color::red)
.font_background_color(Color::yellow)
// Corners
.corner_top_left("ᛰ")
.corner_top_right("ᛯ")
.corner_bottom_left("ᛮ")
.corner_bottom_right("ᛸ")
.corner_top_left_color(Color::cyan)
.corner_top_right_color(Color::yellow)
.corner_bottom_left_color(Color::green)
.corner_bottom_right_color(Color::red)
// Borders
.border_top("ᛜ")
.border_bottom("ᛜ")
.border_left("ᚿ")
.border_right("ᛆ")
.border_left_color(Color::yellow)
.border_right_color(Color::green)
.border_top_color(Color::cyan)
.border_bottom_color(Color::red);
std::cout << table << std::endl;
return 0;
}
0x36 基于局限的迭代
一个一个设置表格的款式是不是是很贫苦?tabulate 供应了迭代器,支撑对表、行和列的迭代,更方便地格式化表格。
#include <tabulate/table.hpp>
using namespace tabulate;
int main() {
Table table;
table.add_row({"Company", "Contact", "Country"});
table.add_row({"Alfreds Futterkiste", "Maria Anders", "Germany"});
table.add_row({"Centro comercial Moctezuma", "Francisco Chang", "Mexico"});
table.add_row({"Ernst Handel", "Roland Mendel", "Austria"});
table.add_row({"Island Trading", "Helen Bennett", "UK"});
table.add_row({"Laughing Bacchus Winecellars", "Yoshi Tannamuri", "Canada"});
table.add_row({"Magazzini Alimentari Riuniti", "Giovanni Rovelli", "Italy"});
// 设置每一行的宽度
table.column(0).format().width(40);
table.column(1).format().width(30);
table.column(2).format().width(30);
// 遍历第一行中的单元格
for (auto& cell : table[0]) {
cell.format()
.font_style({FontStyle::underline})
.font_align(FontAlign::center);
}
// 遍历第一列中的单元格
for (auto& cell : table.column(0)) {
if (cell.get_text() != "Company") {
cell.format()
.font_align(FontAlign::right);
}
}
// 遍历表格中的行
size_t index = 0;
for (auto& row : table) {
row.format()
.font_style({FontStyle::bold});
// 轮番把整行的背景设为蓝色
if (index > 0 && index % 2 == 0) {
for (auto& cell : row) {
cell.format()
.font_background_color(Color::blue);
}
}
index += 1;
}
std::cout << table << std::endl;
}
0x37 嵌套表格
在 tabulate 中嵌套表格很轻易,由于 Table.add_row(...)
这个函数能够接收 std::string
范例和 tabulate::Table
。下面是一个嵌套表格的例子:
#include "tabulate/table.hpp"
using namespace tabulate;
using namespace std;
int main() {
Table hellogithub;
hellogithub.add_row({"HelloGitHub"});
hellogithub[0][0]
.format()
.font_background_color(Color::blue)
.font_align(FontAlign::center);
Table hglink;
hglink.add_row({"GitHub repo", "Website"});
hglink.add_row({"github.com/521xueweihan/HelloGitHub", "hellogithub.com"});
hellogithub.add_row({hglink}); // 嵌套!
cout << hellogithub << endl;
return 0;
}
0x40 导出
0x41 Markdown
能够运用 MarkdownExporter
来将一个表格导出为 GFM 作风的 Markdown。
#include "tabulate/markdown_exporter.hpp" // 注重这个头文件
#include "tabulate/table.hpp"
using namespace tabulate;
using namespace std;
int main() {
Table hellogithub;
hellogithub.add_row({"HelloGitHub"});
hellogithub[0][0].format().font_style({FontStyle::bold}); // 加粗款式,在 Markdown 中能够表现出来
hellogithub.add_row({"GitHub repo: github.com/521xueweihan/HelloGitHub"});
hellogithub.add_row({"Website: hellogithub.com"});
// 导出为 Markdown
MarkdownExporter exporter;
auto markdown = exporter.dump(hellogithub);
cout << hellogithub << endl << endl;
cout << "Markdown Source:nn" << markdown << endl;
return 0;
}
导出结果以下:
注重:Markdown 不能指定每个单元格的对齐款式,只能指定一列的对齐款式,像如许
hg.column(1).format().font_align(FontAlign::center);
。
0x50 尾声
假如想要更细致地相识 tabulate 的用法,请检察官方文档 。
本文是作者的第一次关于此范例文章的尝试,若有不足之处,请斧正,感谢!
再会!
关注民众号到场交换群
详解Net Core Web Api项目与在NginX下发布