Go to file
2018-07-29 11:24:05 +03:00
docs/images [A] Added image 2018-05-07 21:21:19 +03:00
example [F] Moved to c++11 2018-07-29 10:33:21 +03:00
lib [F] Fixed compiler warnings 2018-07-29 11:24:05 +03:00
src [F] Fixed compiler warnings 2018-07-29 11:24:05 +03:00
tests [F] Fixed cmake error: added test_table_basic 2018-07-26 22:25:22 +03:00
.travis.yml [A] Added xcode to the CI 2018-05-09 13:29:29 +03:00
amalgamate.py [F] Fixed style errors 2018-05-06 17:08:32 +03:00
appveyor.yml [C] Changed test architecture 2018-05-06 16:21:45 +03:00
CMakeLists.txt [F] Moved to c++11 2018-07-29 10:33:21 +03:00
LICENSE Create LICENSE 2018-03-31 19:10:59 +03:00
README.md [F] Fixed compiler warnings 2018-07-29 11:24:05 +03:00

libfort (Library to create FORmatted Tables)

Build Status Build status Coverage Status Try online License: MIT

libfort - a simple library to create formatted ASCII tables. libfort is written in C language and can be used in C and C++ projects. For C++ projects that use compiler with c++11 support there are also availabe convenient c++ wrappers around c functions (see fort.hpp in lib direrctory).

Features:

  • Easy to integrate (only 2 files: fort.c and fort.h from lib direrctory)
  • Customization of appearance (various border styles and row/column/cell options for indentation, alignment, padding)
  • A number of functions to fill the table (add content by adding separate cells, rows or use printf like functions)
  • Support of multiple lines in cells
  • Support of wide characters

Getting Started

The common libfort usage pattern:

  • create a table (ft_create_table);
  • fill it with data (ft_write_ln, fr_ptrintf_ln, ft_row_write, ...);
  • modify basic table appearance (ft_set_cell_option, ft_set_border_style ...)
  • convert table to string representation (ft_to_string);
  • destroy the table (ft_destroy_table)

Here are some examples:

Basic example

#include <stdio.h>
#include "fort.h"
int main(void)
{
    ft_table_t *table = ft_create_table();
    /* Set "header" type for the first row */
    ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
    ft_write_ln(table, "N", "Planet", "Speed, km/s");

    ft_write_ln(table, "1", "Mercury", "47.362");
    ft_write_ln(table, "2", "Venus", "35.02");
    ft_write_ln(table, "3", "Earth", "29.78");

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

Output:

+---+---------+-------------+
| N | Planet  | Speed, km/s |
+---+---------+-------------+
| 1 | Mercury | 47.362      |
| 2 | Venus   | 35.02       |
| 3 | Earth   | 29.78       |
+---+---------+-------------+

Customize table appearance

ft_table_t *table = ft_create_table();
ft_set_border_style(table, FT_DOUBLE2_STYLE);
/* Set center alignment for the 1st column */
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_COPT_TEXT_ALIGN, FT_ALIGNED_CENTER);
/* Set center alignment for the 3rd column */
ft_set_cell_option(table, FT_ANY_ROW, 3, FT_COPT_TEXT_ALIGN, FT_ALIGNED_CENTER);

/* Set "header" type for the first row */
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "Movie title", "Director", "Year", "Rating");

ft_write_ln(table, "The Shawshank Redemption", "Frank Darabont", "1994", "9.5");
ft_write_ln(table, "The Godfather", "Francis Ford Coppola", "1972", "9.2");
ft_write_ln(table, "12 Angry Men", "Sidney Lumet", "1957", "8.8");
ft_write_ln(table, "2001: A Space Odyssey", "Stanley Kubrick", "1968", "8.5");

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

Output:

╔══════════════════════════╤══════════════════════╤══════╤════════╗
║ Movie title              │       Director       │ Year │ Rating ║
╠══════════════════════════╪══════════════════════╪══════╪════════╣
║ The Shawshank Redemption │    Frank Darabont    │ 1994 │  9.5   ║
╟──────────────────────────┼──────────────────────┼──────┼────────╢
║ The Godfather            │ Francis Ford Coppola │ 1972 │  9.2   ║
╟──────────────────────────┼──────────────────────┼──────┼────────╢
║ 12 Angry Men             │     Sidney Lumet     │ 1957 │  8.8   ║
╟──────────────────────────┼──────────────────────┼──────┼────────╢
║ 2001: A Space Odyssey    │   Stanley Kubrick    │ 1968 │  8.5   ║
╚══════════════════════════╧══════════════════════╧══════╧════════╝

Installation

Copy 2 files fort.c and fort.h from lib direrctory to your project. Include fort.h in all sources where you will use libfort and compile with fort.c.

Supported platforms and compilers

The following compilers are currently used in continuous integration at Travis and AppVeyor:

Compiler Operating System
GCC 4.8.4 Ubuntu 14.04.3
GCC 4.9.4 Ubuntu 14.04.3
GCC 5.5.0 Ubuntu 14.04.3
GCC 6.4.0 Ubuntu 14.04.3
GCC 7.3.0 Ubuntu 14.04.3
Clang 5.0.0 Ubuntu 14.04.3
AppleClang 7.3.0 Darwin Kernel Version 15.6.0
AppleClang 8.1.0 Darwin Kernel Version 16.7.0
AppleClang 9.1.0 Darwin Kernel Version 17.4.0
Visual Studio 2017 Windows Server 2016

Pease note:

  • In case of clang on OS X before using libfort with wchar_t with real unicode symbols it may be necessary to set setlocale(LC_CTYPE, ""); because otherwise standard function swprintf, that libfort uses internally, may fail and ft_to_string will return error.

License

The class is licensed under the MIT License:

Copyright © 2017 - 2018 Seleznev Anton

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.