Skip to content

Border styles (C API)

Seleznev Anton edited this page Nov 4, 2018 · 10 revisions

Change border style

libfort has a number of built-in border styles. To change border style of libfort tables one can use ft_set_default_border_style, ft_set_border_style functions:

/* Change border style of all libfort tables that will be created */
int ft_set_default_border_style(const struct ft_border_style *style);
/* Change border style of a particular table */
int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style);

/**
 * Structure describing border appearance.
 */
struct ft_border_chars {
    const char *top_border_ch;
    const char *separator_ch;
    const char *bottom_border_ch;
    const char *side_border_ch;
    const char *out_intersect_ch;
    const char *in_intersect_ch;
};

/**
 * Structure describing border style.
 */
struct ft_border_style {
    struct ft_border_chars border_chs;
    struct ft_border_chars header_border_chs;
    const char *hor_separator_char;
};

Here is a simple example of creating table and setting border style:

ft_table_t *table = ft_create_table();   
/* Set border style */
ft_set_border_style(table, FT_BASIC_STYLE);
                                       
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);  
                                                                                
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);   
ft_write_ln(table, "Rank", "Title", "Year", "Rating");                          
                                                                                
ft_write_ln(table, "1", "The Shawshank Redemption", "1994", "9.5");             
ft_write_ln(table, "2", "12 Angry Men", "1957", "8.8");                         
ft_write_ln(table, "3", "It's a Wonderful Life", "1946", "8.6");                
ft_add_separator(table);                                                        
ft_write_ln(table, "4", "2001: A Space Odyssey", "1968", "8.5");                
ft_write_ln(table, "5", "Blade Runner", "1982", "8.1");   

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

Output:

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

Custom border styles

ft_set_default_border_style, ft_set_border_style can be used

Built-in border styles

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_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    ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟