cpp庫-libfort中文教程

余为民同志發表於2024-08-21

表格的生命週期

libfort表生命週期:

  • 建立一個表(使用預設建構函式、複製建構函式或移動建構函式);
  • 用資料填充它(運算子<<,運算子[],write_ln...);
  • 修改基本表外觀;
  • 將錶轉換為字串表示(to_string)並列印出來。

在表生存期中分配的所有資源將在解構函式中自動釋放。

案例:

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;                      

輸出:

+---+------------+----------+-----------+
| 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     |
+---+------------+----------+-----------+

往表格中填充資料

在每個時刻,libfort表都有一個當前單元格——在下一次寫操作中將向其寫入資料。函式set_cur_cell<<(fort::endr)操作可以用來改變當前單元格:

/* 將當前單元格設定為具有座標(row, col)的單元格 */
void set_cur_cell(size_t row, size_t col)
/* 將當前單元格設定為下一行(line)第一個單元格  */
table << fort::endr;

有很多函式可以用來將資料填充表格。所有用來寫資料的函式都是成對的:(function,function_ln),其中function向一組連續的單元格寫入資料,function_ln做同樣的事情,並將當前單元格指標移動到下一行(line)的第一個單元格。

<<運算子

應用於libfort表的運算子(<<)稱為插入運算子。它將字串插入到表格單元格中。libfort內部使用std::stringstream將引數轉換為字串。如果要在表中插入某些自定義型別的引數,則應過載std::stringstream::operator<<

案例:

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;                      

輸出:

+---+------------+----------+-----------+
| 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   |
+---+------------+----------+-----------+

[]運算子

此運算子([])提供了直接更改特定單元格內容的功能。

案例:

fort::char_table table;                                                                                   
table << fort::header;                                                                               
table[0][0] = "N"; table[0][1] = "Driver";     table[0][2] = "Time";     table[0][3] = "Avg Speed";  
                                                                                                     
table[1][0] = "1"; table[1][1] = "Ricciardo";  table[1][2] = "1:25.945"; table[1][3] = "47.362";     
table[2][0] = "2"; table[2][1] = "Hamilton";   table[2][2] = "1:26.373"; table[2][3] = "35.02";      
table[3][0] = "3"; table[3][1] = "Verstappen"; table[3][2] = "1:26.469"; table[3][3] = "29.78";      
                                                                                                     
std::cout << table.to_string() << std::endl;                                                                            

輸出:

+---+------------+----------+-----------+
| 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   |
+---+------------+----------+-----------+

write, write_ln

write,write_ln可以向表單元寫入任意數量的字串引數。

案例:

fort::char_table table;                                       
table << fort::header;                                   
table.write_ln("N", "Driver", "Time", "Avg Speed");      
table.write_ln("1", "Ricciardo", "1:25.945", "47.362");  
table.write_ln("2", "Hamilton", "1:26.373", "35.02");    
table.write_ln("3", "Verstappen", "1:26.469", "29.78");  
                                                         
std::cout << table.to_string() << std::endl;    

輸出:

+---+------------+----------+-----------+
| 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   |
+---+------------+----------+-----------+

range_write, range_write_ln

range_write,range_write_ln從由一對迭代器確定的容器中寫入資料。

案例:

template <typename InputIt>
bool range_write(InputIt first, InputIt last);
template <typename InputIt>
bool range_write_ln(InputIt first, InputIt last);
fort::char_table table;                                                                  
table << fort::header;                                                              
std::vector<std::string> header = {"N", "Driver", "Time", "Avg Speed"};             
std::list<std::string> line_1 = {"1", "Ricciardo", "1:25.945", "47.362"};           
std::initializer_list<std::string> line_2 = {"2", "Hamilton", "1:26.373", "35.02"}; 
std::deque<std::string> line_3 = {"3", "Verstappen", "1:26.469", "29.78"};          
                                                                                    
table.range_write_ln(header.begin(), header.end());                                   
table.range_write_ln(line_1.begin(), line_1.end());                                   
table.range_write_ln(line_2.begin(), line_2.end());                                   
table.range_write_ln(line_3.begin(), line_3.end());                                   
                                                                                    
std::cout << table.to_string() << std::endl;                                        

輸出:

+---+------------+----------+-----------+
| 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   |
+---+------------+----------+-----------+

單元格和表格的操作

預設情況下,在libfort表中建立的所有單元格都將具有相同的屬性。

要更改單元格屬性,應使用函式set_cell_{property_name}

bool set_cell_min_width(unsigned value);
bool set_cell_text_align(enum fort::text_align value);
bool set_cell_top_padding(unsigned value);
bool set_cell_bottom_padding(unsigned value);
bool set_cell_left_padding(unsigned value);
bool set_cell_right_padding(unsigned value);
bool set_cell_empty_str_height(unsigned value);
bool set_cell_row_type(enum fort::row_type value);
bool set_cell_content_fg_color(enum fort::color value);
bool set_cell_bg_color(enum fort::color value);
bool set_cell_content_bg_color(enum fort::color value);
bool set_cell_text_style(enum fort::text_style value);
bool set_cell_content_text_style(enum fort::text_style value);

屬性可以設定為預設值(用於將來建立的表中的所有單元格)、用於指定表的所有單元格、用於指定某一行的單元格、用於指定某一列中的單元格以及用於指定表的特定單元格。

以下示例說明了這一點:

// 為以後建立的所有表格中的所有單元格設定top padding = 2
fort::table::default_props().set_cell_top_padding(2);

// 為第2行中的所有單元格設定行型別
table.row(2).set_cell_row_type(fort::row_type::header);

// 為第1列中的所有單元格設定文字對齊方式
table.column(1).set_cell_text_align(fort::text_align::center);

// 為單元格(0,0),設定最小寬度
table[0][0].set_cell_min_width(20);

下面是一個簡單的完整示例:

fort::char_table table;                                                        
// Fill table with data                                                   
table << fort::header                                                     
    << "Rank" << "Title" << "Year" << "Rating" << fort::endr              
    << "1" << "The Shawshank Redemption" << "1994" << "9.5" << fort::endr 
    << "2" << "12 Angry Men" << "1957" << "8.8" << fort::endr             
    << "3" << "It's a Wonderful Life" << "1946" << "8.6" << fort::endr    
    << fort::endr;                                                        
                                                                          
table[0][0].set_cell_min_width(20);                                      
table.column(1).set_cell_text_align(fort::text_align::center);        
table[3][3].set_cell_left_padding(15);                                   
                                                                          
std::cout << table.to_string() << std::endl;                              

輸出:

+--------------------+--------------------------+------+-------------------+
| Rank               |          Title           | Year | Rating            |
+--------------------+--------------------------+------+-------------------+
| 1                  | The Shawshank Redemption | 1994 | 9.5               |
| 2                  |       12 Angry Men       | 1957 | 8.8               |
| 3                  |  It's a Wonderful Life   | 1946 |               8.6 |
+--------------------+--------------------------+------+-------------------+

邊框的樣式

libfort有許多內建的邊框樣式。要更改libfort表的邊框樣式,可以使用set_default_bound_styleset_bound_style函式:

/* 更改將建立的所有libfort表的邊框樣式 */
bool set_default_border_style(struct ft_border_style *style);
/* 更改當前表格的邊框樣式 */
bool set_border_style(struct ft_border_style *style);

下面是一個建立表格和設定邊框樣式的簡單示例:

fort::char_table table;                                                       
/* Set table border style */                                             
table.set_border_style(FT_BASIC_STYLE);                                  
                                                                         
// Fill table with data                                                  
table << fort::header                                                    
    << "Rank" << "Title" << "Year" << "Rating" << fort::endr             
    << "1" << "The Shawshank Redemption" << "1994" << "9.5" << fort::endr
    << "2" << "12 Angry Men" << "1957" << "8.8" << fort::endr            
    << "3" << "It's a Wonderful Life" << "1946" << "8.6" << fort::endr   
    << fort::separator                                                   
    << "4" << "2001: A Space Odyssey" << "1968" << "8.5" << fort::endr   
    << "5" << "Blade Runner" << "1982" << "8.1" << fort::endr            
    << fort::endr;                                                       
                                                                         
table.column(0).set_cell_text_align(fort::text_align::center);
table.column(1).set_cell_text_align(fort::text_align::left);   
                                                                         
std::cout << table.to_string() << std::endl;                                      

輸出:

+------+--------------------------+------+--------+
| Rank | Title                    | Year | Rating |
+------+--------------------------+------+--------+
|  1   | The Shawshank Redemption | 1994 | 9.5    |
|  2   | 12 Angry Men             | 1957 | 8.8    |
|  3   | It's a Wonderful Life    | 1946 | 8.6    |
+------+--------------------------+------+--------+
|  4   | 2001: A Space Odyssey    | 1968 | 8.5    |
|  5   | Blade Runner             | 1982 | 8.1    |
+------+--------------------------+------+--------+

style可選值

FT_BASIC_STYLE

+------+--------------------------+------+--------+
| Rank | Title                    | Year | Rating |
+------+--------------------------+------+--------+
|  1   | The Shawshank Redemption | 1994 | 9.5    |
|  2   | 12 Angry Men             | 1957 | 8.8    |
|  3   | It's a Wonderful Life    | 1946 | 8.6    |
+------+--------------------------+------+--------+
|  4   | 2001: A Space Odyssey    | 1968 | 8.5    |
|  5   | Blade Runner             | 1982 | 8.1    |
+------+--------------------------+------+--------+

FT_BASIC2_STYLE

+------+--------------------------+------+--------+
| Rank | Title                    | Year | Rating |
+------+--------------------------+------+--------+
|  1   | The Shawshank Redemption | 1994 | 9.5    |
+------+--------------------------+------+--------+
|  2   | 12 Angry Men             | 1957 | 8.8    |
+------+--------------------------+------+--------+
|  3   | It's a Wonderful Life    | 1946 | 8.6    |
+------+--------------------------+------+--------+
|  4   | 2001: A Space Odyssey    | 1968 | 8.5    |
+------+--------------------------+------+--------+
|  5   | Blade Runner             | 1982 | 8.1    |
+------+--------------------------+------+--------+

FT_SIMPLE_STYLE

  Rank   Title                      Year   Rating  
 ------ -------------------------- ------ -------- 
   1     The Shawshank Redemption   1994   9.5     
   2     12 Angry Men               1957   8.8     
   3     It's a Wonderful Life      1946   8.6     
 ------ -------------------------- ------ -------- 
   4     2001: A Space Odyssey      1968   8.5     
   5     Blade Runner               1982   8.1     

FT_PLAIN_STYLE

 ------------------------------------------------- 
  Rank   Title                      Year   Rating  
 ------------------------------------------------- 
   1     The Shawshank Redemption   1994   9.5     
   2     12 Angry Men               1957   8.8     
   3     It's a Wonderful Life      1946   8.6     
 ------------------------------------------------- 
   4     2001: A Space Odyssey      1968   8.5     
   5     Blade Runner               1982   8.1      

FT_DOT_STYLE

...................................................
: Rank : Title                    : Year : Rating :
:......:..........................:......:........:
:  1   : The Shawshank Redemption : 1994 : 9.5    :
:  2   : 12 Angry Men             : 1957 : 8.8    :
:  3   : It's a Wonderful Life    : 1946 : 8.6    :
:......:..........................:......:........:
:  4   : 2001: A Space Odyssey    : 1968 : 8.5    :
:  5   : Blade Runner             : 1982 : 8.1    :
:......:..........................:......:........:

FT_EMPTY_STYLE

 Rank  Title                     Year  Rating 
  1    The Shawshank Redemption  1994  9.5    
  2    12 Angry Men              1957  8.8    
  3    It's a Wonderful Life     1946  8.6    
                                              
  4    2001: A Space Odyssey     1968  8.5    
  5    Blade Runner              1982  8.1    

FT_EMPTY2_STYLE

  Rank   Title                      Year   Rating  
   1     The Shawshank Redemption   1994   9.5     
   2     12 Angry Men               1957   8.8     
   3     It's a Wonderful Life      1946   8.6     
                                                   
   4     2001: A Space Odyssey      1968   8.5     
   5     Blade Runner               1982   8.1     

FT_SOLID_STYLE

┌──────┬──────────────────────────┬──────┬────────┐
│ Rank │ Title                    │ Year │ Rating │
├──────┼──────────────────────────┼──────┼────────┤
│  1   │ The Shawshank Redemption │ 1994 │ 9.5    │
│  2   │ 12 Angry Men             │ 1957 │ 8.8    │
│  3   │ It's a Wonderful Life    │ 1946 │ 8.6    │
├──────┼──────────────────────────┼──────┼────────┤
│  4   │ 2001: A Space Odyssey    │ 1968 │ 8.5    │
│  5   │ Blade Runner             │ 1982 │ 8.1    │
└──────┴──────────────────────────┴──────┴────────╯ 

FT_SOLID_ROUND_STYLE

╭──────┬──────────────────────────┬──────┬────────╮
│ Rank │ Title                    │ Year │ Rating │
├──────┼──────────────────────────┼──────┼────────┤
│  1   │ The Shawshank Redemption │ 1994 │ 9.5    │
│  2   │ 12 Angry Men             │ 1957 │ 8.8    │
│  3   │ It's a Wonderful Life    │ 1946 │ 8.6    │
├──────┼──────────────────────────┼──────┼────────┤
│  4   │ 2001: A Space Odyssey    │ 1968 │ 8.5    │
│  5   │ Blade Runner             │ 1982 │ 8.1    │
╰──────┴──────────────────────────┴──────┴────────╯

FT_DOUBLE_STYLE

╔══════╦══════════════════════════╦══════╦════════╗
║ Rank ║ Title                    ║ Year ║ Rating ║
╠══════╬══════════════════════════╬══════╬════════╣
║  1   ║ The Shawshank Redemption ║ 1994 ║ 9.5    ║
║  2   ║ 12 Angry Men             ║ 1957 ║ 8.8    ║
║  3   ║ It's a Wonderful Life    ║ 1946 ║ 8.6    ║
╠══════╬══════════════════════════╬══════╬════════╣
║  4   ║ 2001: A Space Odyssey    ║ 1968 ║ 8.5    ║
║  5   ║ Blade Runner             ║ 1982 ║ 8.1    ║
╚══════╩══════════════════════════╩══════╩════════╝

FT_DOUBLE2_STYLE

╔══════╤══════════════════════════╤══════╤════════╗
║ Rank │ Title                    │ Year │ Rating ║
╠══════╪══════════════════════════╪══════╪════════╣
║  1   │ The Shawshank Redemption │ 1994 │ 9.5    ║
╟──────┼──────────────────────────┼──────┼────────╢
║  2   │ 12 Angry Men             │ 1957 │ 8.8    ║
╟──────┼──────────────────────────┼──────┼────────╢
║  3   │ It's a Wonderful Life    │ 1946 │ 8.6    ║
╠══════╪══════════════════════════╪══════╪════════╣
║  4   │ 2001: A Space Odyssey    │ 1968 │ 8.5    ║
╟──────┼──────────────────────────┼──────┼────────╢
║  5   │ Blade Runner             │ 1982 │ 8.1    ║
╚══════╧══════════════════════════╧══════╧════════╝

FT_BOLD_STYLE

┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━┓
┃ Rank ┃ Title                    ┃ Year ┃ Rating ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━┫
┃  1   ┃ The Shawshank Redemption ┃ 1994 ┃ 9.5    ┃
┃  2   ┃ 12 Angry Men             ┃ 1957 ┃ 8.8    ┃
┃  3   ┃ It's a Wonderful Life    ┃ 1946 ┃ 8.6    ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━┫
┃  4   ┃ 2001: A Space Odyssey    ┃ 1968 ┃ 8.5    ┃
┃  5   ┃ Blade Runner             ┃ 1982 ┃ 8.1    ┃
┗━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━┻━━━━━━━━┛

FT_BOLD2_STYLE

┏━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━┓
┃ Rank │ Title                    │ Year │ Rating ┃
┣━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┿━━━━━━━━┫
┃  1   │ The Shawshank Redemption │ 1994 │ 9.5    ┃
┠──────┼──────────────────────────┼──────┼────────┨
┃  2   │ 12 Angry Men             │ 1957 │ 8.8    ┃
┠──────┼──────────────────────────┼──────┼────────┨
┃  3   │ It's a Wonderful Life    │ 1946 │ 8.6    ┃
┣━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┿━━━━━━━━┫
┃  4   │ 2001: A Space Odyssey    │ 1968 │ 8.5    ┃
┠──────┼──────────────────────────┼──────┼────────┨
┃  5   │ Blade Runner             │ 1982 │ 8.1    ┃
┗━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━┛

FT_FRAME_STYLE

▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ Rank ┃ Title                    ┃ Year ┃ Rating ▐
▌━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━▐
▌  1   ┃ The Shawshank Redemption ┃ 1994 ┃ 9.5    ▐
▌  2   ┃ 12 Angry Men             ┃ 1957 ┃ 8.8    ▐
▌  3   ┃ It's a Wonderful Life    ┃ 1946 ┃ 8.6    ▐
▌━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━▐
▌  4   ┃ 2001: A Space Odyssey    ┃ 1968 ┃ 8.5    ▐
▌  5   ┃ Blade Runner             ┃ 1982 ┃ 8.1    ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟

相關文章