C/C++:libfort用于在终端输出表格

libfort

用于在终端输出表格,提供了表格布局和样式设置的功能

https://github.com/seleznevae/libfort

示例: C示例

cpp 复制代码
#include <stdio.h>

#include "fort.h"

int main(void)
{
    ft_table_t *table = ft_create_table();

    /* Setup header */
    ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
    ft_write_ln(table, "N", "Driver", "Time", "Avg Speed");

    ft_write_ln(table, "1", "Ricciardo", "1:25.945", "222.128");
    ft_write_ln(table, "2", "Hamilton", "1:26.373", "221.027");
    ft_write_ln(table, "3", "Verstappen", "1:26.469", "220.782");

    printf("%s\n", ft_to_string(table));
    ft_destroy_table(table);

    return 0;
}

输出结果

shell 复制代码
$ gcc 1-simple_table.c ../lib/fort.c -I../lib  && ./a.out 

+---+------------+----------+-----------+
| N | Driver     | Time     | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo  | 1:25.945 | 222.128   |
| 2 | Hamilton   | 1:26.373 | 221.027   |
| 3 | Verstappen | 1:26.469 | 220.782   |
+---+------------+----------+-----------+

示例: C++示例

cpp 复制代码
#include <iostream>

#include "fort.hpp"


int main()
{
    fort::char_table table;
    table << fort::header
        << "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
        << "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr
        << "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr
        << "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr;

    std::cout << table.to_string() << std::endl;
}

输出结果

shell 复制代码
g++ 1-simple_table.cpp ../lib/fort.c -I../lib  -std=c++11 && ./a.out

+---+------------+----------+-----------+
| N | Driver     | Time     | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo  | 1:25.945 | 47.362    |
| 2 | Hamilton   | 1:26.373 | 35.02     |
| 3 | Verstappen | 1:26.469 | 29.78     |
+---+------------+----------+-----------+
相关推荐
wjs20249 小时前
JavaScript 语句
开发语言
CoderCodingNo9 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
cmpxr_10 小时前
【C】局部变量和全局变量及同名情况
c语言·开发语言
hetao173383710 小时前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
6Hzlia10 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
小碗羊肉10 小时前
【从零开始学Java | 第三十一篇下】Stream流
java·开发语言
汉克老师11 小时前
GESP2024年6月认证C++三级( 第二部分判断题(1-10))
c++·数组·位运算·补码·gesp三级·gesp3级
aq553560011 小时前
Laravel10.x重磅升级,新特性一览
android·java·开发语言
报错小能手11 小时前
ios开发方向——swift错误处理:do/try/catch、Result、throws
开发语言·学习·ios·swift
无限进步_12 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode