• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmake/H23-Jun-2021-64

docs/H23-Jun-2021-4,2523,287

examples/H03-May-2022-866632

lib/H03-May-2022-10,1166,716

src/H03-May-2022-9,9106,744

tests/H03-May-2022-6,9825,945

.cirrus.ymlH A D23-Jun-20211.8 KiB6857

.clang-tidyH A D23-Jun-20211.7 KiB4339

.drone.ymlH A D23-Jun-20212.1 KiB8573

.travis.ymlH A D23-Jun-20218.1 KiB314274

CONTRIBUTING.mdH A D23-Jun-2021799 1912

ChangeLog.mdH A D23-Jun-20213.3 KiB16492

LICENSEH A D23-Jun-20211.1 KiB2217

README.mdH A D23-Jun-202116.7 KiB378300

amalgamate.pyH A D23-Jun-20214.2 KiB12087

appveyor.ymlH A D23-Jun-20213.7 KiB123104

README.md

1# libfort (Library to create FORmatted Tables)
2
3[![Build Status](https://travis-ci.com/seleznevae/libfort.svg?branch=master)](https://travis-ci.com/seleznevae/libfort)
4[![Build Status](https://api.cirrus-ci.com/github/seleznevae/libfort.svg)](https://cirrus-ci.com/github/seleznevae/libfort)
5[![Build status](https://ci.appveyor.com/api/projects/status/ll1qygb56pho95xw/branch/master?svg=true)](https://ci.appveyor.com/project/seleznevae/libfort/branch/master)
6[![Build Status](https://cloud.drone.io/api/badges/seleznevae/libfort/status.svg?ref=refs/heads/master)](https://cloud.drone.io/seleznevae/libfort)
7[![Coverage Status](https://coveralls.io/repos/github/seleznevae/libfort/badge.svg?branch=master)](https://coveralls.io/github/seleznevae/libfort?branch=master)
8[![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/h566MmC2bfdsZZDr)
9[![Documentation](https://img.shields.io/badge/docs-doxygen-blue.svg)](http://seleznevae.github.io/libfort)
10[![Doc](https://img.shields.io/badge/doc-wiki-blue.svg)](https://github.com/seleznevae/libfort/wiki)
11[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
12
13**libfort** is a simple crossplatform library to create formatted text tables.
14
15![TableSample](https://github.com/seleznevae/libfort/blob/master/docs/images/color_table.png)
16
17**Features:**
18- Easy to integrate (only 2 files)
19- Customization of appearance (various [border styles](https://github.com/seleznevae/libfort/wiki/Border-styles-(C-API)) and row/column/cell [properties](https://github.com/seleznevae/libfort/wiki/Cell--and-table-properties-(C-API)) for indentation, alignment, padding)
20- A number of functions to fill the table (add content by adding separate cells, rows or use `printf` like functions)
21- Support of multiple lines in cells
22- Support of UTF-8 and wide characters
23
24## Design goals
25- **Portability**. All main OSes (Linux, Windows, macOS, FreeBSD) and compilers are supported.
26- **Maintainability and robustness**. **libfort** is written in C because it is much less complicated than C++ and it can be used in both C and C++ projects and even on platforms without C++ compiler.
27- **Trivial integration.** Therefore all source code files are amalgamed in only 2 files.
28- **Heavy testing.** The goal is to cover 100% of the code (it is not reached yet) and to run tests on all major compilers and platforms.
29
30## Integration
31Add 2 files ( [`fort.c`](https://github.com/seleznevae/libfort/blob/master/lib/fort.c)  and [`fort.h`](https://github.com/seleznevae/libfort/blob/master/lib/fort.h) from [**lib**](https://github.com/seleznevae/libfort/tree/master/lib) directory) to your C or C++ project and include
32```C
33#include "fort.h"
34```
35in your source code where you will use **libfort** functions.
36
37For C++ projects that use compiler with C++11 support (and later) there are also availabe convenient C++ wrappers around C functions (see [`fort.hpp`](https://github.com/seleznevae/libfort/blob/master/lib/fort.hpp) in **lib** direrctory). In that case instead of _fort.h_ you will need to include
38```CPP
39#include "fort.hpp"
40```
41
42### Integration with cmake
43
44In case **libfort** is installed on the host system it should be sufficient to use `find_package`:
45```cmake
46find_package(libfort)
47target_link_libraries(your_target PRIVATE libfort::fort)
48```
49
50In case you downloaded **libfort** sources and embedded them in your project (e.g. put all sources in directory `third-party/libfort`) you can use `add_subdirectory`:
51```cmake
52# Disable building tests and examples in libfort project
53set(FORT_ENABLE_TESTING OFF CACHE INTERNAL "")
54
55add_subdirectory(third-party/libfort)
56target_link_libraries(your_target PRIVATE fort)
57```
58
59## Documentation
60See guide in [tutorial](https://github.com/seleznevae/libfort/blob/develop/docs/tutorial/Tutorial.md) of the library and doxygen [API documentation](http://seleznevae.github.io/libfort).
61
62## Getting Started
63
64The common libfort usage pattern (C API):
65- create a table (`ft_create_table`);
66- fill it with data (`ft_write_ln`, `fr_ptrintf_ln`, `ft_row_write`, ...);
67- modify basic table appearance (`ft_set_cell_prop`, `ft_set_border_style` ...)
68- convert table to string representation (`ft_to_string`);
69- destroy the table (`ft_destroy_table`)
70
71Here are some examples:
72
73### Basic example
74
75
76```C
77/* C API */
78#include <stdio.h>
79#include "fort.h"
80int main(void)
81{
82    ft_table_t *table = ft_create_table();
83    /* Set "header" type for the first row */
84    ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
85
86    ft_write_ln(table, "N", "Driver", "Time", "Avg Speed");
87
88    ft_write_ln(table, "1", "Ricciardo", "1:25.945", "222.128");
89    ft_write_ln(table, "2", "Hamilton", "1:26.373", "221.027");
90    ft_write_ln(table, "3", "Verstappen", "1:26.469", "220.782");
91
92    printf("%s\n", ft_to_string(table));
93    ft_destroy_table(table);
94}
95```
96
97```C++
98/* C++ API */
99#include <iostream>
100#include "fort.hpp"
101int main(void)
102{
103    fort::char_table table;
104    table << fort::header
105        << "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
106        << "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr
107        << "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr
108        << "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr;
109
110    std::cout << table.to_string() << std::endl;
111}
112```
113
114Output:
115```text
116+---+------------+----------+-----------+
117| N | Driver     | Time     | Avg Speed |
118+---+------------+----------+-----------+
119| 1 | Ricciardo  | 1:25.945 | 47.362    |
120| 2 | Hamilton   | 1:26.373 | 35.02     |
121| 3 | Verstappen | 1:26.469 | 29.78     |
122+---+------------+----------+-----------+
123```
124
125### Customize table appearance
126
127```C
128/* C API */
129#include <stdio.h>
130#include "fort.h"
131int main(void)
132{
133    ft_table_t *table = ft_create_table();
134    /* Change border style */
135    ft_set_border_style(table, FT_DOUBLE2_STYLE);
136
137    /* Set "header" type for the first row */
138    ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
139    ft_write_ln(table, "Movie title", "Director", "Year", "Rating");
140
141    ft_write_ln(table, "The Shawshank Redemption", "Frank Darabont", "1994", "9.5");
142    ft_write_ln(table, "The Godfather", "Francis Ford Coppola", "1972", "9.2");
143    ft_write_ln(table, "2001: A Space Odyssey", "Stanley Kubrick", "1968", "8.5");
144
145    /* Set center alignment for the 1st and 3rd columns */
146    ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
147    ft_set_cell_prop(table, FT_ANY_ROW, 3, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
148
149    printf("%s\n", ft_to_string(table));
150    ft_destroy_table(table);
151}
152```
153
154```C++
155/* C++ API */
156#include <iostream>
157#include "fort.hpp"
158int main(void)
159{
160    fort::char_table table;
161    /* Change border style */
162    table.set_border_style(FT_DOUBLE2_STYLE);
163
164    table << fort::header
165        << "Movie title" << "Director" << "Year" << "Rating" << fort::endr
166        << "The Shawshank Redemption" << "Frank Darabont" << "1994" << "9.5" << fort::endr
167        << "The Godfather" << "Francis Ford Coppola" << "1972" << "9.2" << fort::endr
168        << "2001: A Space Odyssey" << "Stanley Kubrick" << "1968" << "8.5" << fort::endr;
169
170    /* Set center alignment for the 1st and 3rd columns */
171    table.column(1).set_cell_text_align(fort::text_align::center);
172    table.column(3).set_cell_text_align(fort::text_align::center);
173
174    std::cout << table.to_string() << std::endl;
175}
176```
177
178Output:
179```text
180╔══════════════════════════╤══════════════════════╤══════╤════════╗
181║ Movie title              │       Director       │ Year │ Rating ║
182╠══════════════════════════╪══════════════════════╪══════╪════════╣
183║ The Shawshank Redemption │    Frank Darabont    │ 1994 │  9.5   ║
184╟──────────────────────────┼──────────────────────┼──────┼────────╢
185║ The Godfather            │ Francis Ford Coppola │ 1972 │  9.2   ║
186╟──────────────────────────┼──────────────────────┼──────┼────────╢
187║ 2001: A Space Odyssey    │   Stanley Kubrick    │ 1968 │  8.5   ║
188╚══════════════════════════╧══════════════════════╧══════╧════════╝
189```
190
191### Different ways to fill table with data
192
193```C
194/* C API */
195#include <stdio.h>
196#include "fort.h"
197int main(void)
198{
199    ft_table_t *table = ft_create_table();
200    /* Set "header" type for the first row */
201    ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
202    ft_write_ln(table, "N", "Planet", "Speed, km/s", "Temperature, K");
203
204    /* Fill row with printf like function */
205    ft_printf_ln(table, "1|%s|%6.3f|%d", "Mercury", 47.362, 340);
206
207    /* Fill row explicitly with strings */
208    ft_write_ln(table, "2", "Venus", "35.02", "737");
209
210    /* Fill row with the array of strings */
211    const char *arr[4] = {"3", "Earth", "29.78", "288"};
212    ft_row_write_ln(table, 4, arr);
213
214    printf("%s\n", ft_to_string(table));
215    ft_destroy_table(table);
216}
217```
218
219```C++
220/* C++ API */
221#include <iostream>
222#include "fort.hpp"
223int main(void)
224{
225    fort::char_table table;
226    table << fort::header;
227    /* Fill each cell with operator[] */
228    table [0][0] = "N";
229    table [0][1] = "Planet";
230    table [0][2] = "Speed, km/s";
231    table [0][3] = "Temperature, K";
232    table << fort::endr;
233
234    /* Fill with iostream operator<< */
235    table << 1 << "Mercury" << 47.362 << 340 << fort::endr;
236
237    /* Fill row explicitly with strings */
238    table.write_ln("2", "Venus", "35.02", "737");
239
240    /* Fill row with data from the container */
241    std::vector<std::string> arr = {"3", "Earth", "29.78", "288"};
242    table.range_write_ln(std::begin(arr), std::end(arr));
243
244    std::cout << table.to_string() << std::endl;
245}
246```
247
248Output:
249```text
250+---+---------+-------------+----------------+
251| N | Planet  | Speed, km/s | Temperature, K |
252+---+---------+-------------+----------------+
253| 1 | Mercury | 47.362      | 340            |
254| 2 | Venus   | 35.02       | 737            |
255| 3 | Earth   | 29.78       | 288            |
256+---+---------+-------------+----------------+
257```
258
259### Working with multibyte-character-strings
260`libfort` supports `wchar_t` and utf-8 strings. Here are simple examples of working with utf-8 strings:
261
262
263```C
264/* C API */
265#include <stdio.h>
266#include "fort.h"
267int main(void)
268{
269    ft_table_t *table = ft_create_table();
270    ft_set_border_style(table, FT_NICE_STYLE);
271    ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
272    ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);
273    ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
274
275    ft_u8write_ln(table, "Ранг", "Название", "Год", "Рейтинг");
276    ft_u8write_ln(table, "1", "Побег из Шоушенка", "1994", "9.5");
277    ft_u8write_ln(table, "2", "12 разгневанных мужчин", "1957", "8.8");
278    ft_u8write_ln(table, "3", "Космическая одиссея 2001 года", "1968", "8.5");
279    ft_u8write_ln(table, "4", "Бегущий по лезвию", "1982", "8.1");
280
281    printf("%s\n", (const char *)ft_to_u8string(table));
282    ft_destroy_table(table);
283}
284```
285
286```C++
287/* C++ API */
288#include <iostream>
289#include "fort.hpp"
290int main(void)
291{
292    fort::utf8_table table;
293    table.set_border_style(FT_NICE_STYLE);
294    table.column(0).set_cell_text_align(fort::text_align::center);
295    table.column(1).set_cell_text_align(fort::text_align::center);
296
297    table << fort::header
298          << "Ранг" << "Название" << "Год" << "Рейтинг" << fort::endr
299          << "1" << "Побег из Шоушенка" << "1994" << "9.5"<< fort::endr
300          << "2" << "12 разгневанных мужчин" << "1957" << "8.8" << fort::endr
301          << "3" << "Космическая одиссея 2001 года" << "1968" << "8.5" << fort::endr
302          << "4" << "Бегущий по лезвию" << "1982" << "8.1" << fort::endr;
303    std::cout << table.to_string() << std::endl;
304}
305```
306
307Output:
308```text
309╔══════╦═══════════════════════════════╦══════╦═════════╗
310║ Ранг ║           Название            ║ Год  ║ Рейтинг ║
311╠══════╬═══════════════════════════════╬══════╬═════════╣
312║  1   ║       Побег из Шоушенка       ║ 1994 ║ 9.5     ║
313║  2   ║    12 разгневанных мужчин     ║ 1957 ║ 8.8     ║
314║  3   ║ Космическая одиссея 2001 года ║ 1968 ║ 8.5     ║
315║  4   ║       Бегущий по лезвию       ║ 1982 ║ 8.1     ║
316╚══════╩═══════════════════════════════╩══════╩═════════╝
317```
318
319Please note:
320-  `libfort` internally has a very simple logic to compute visible width of utf-8
321strings. It considers that each codepoint will occupy one position on the
322terminal in case of monowidth font (some east asians wide and fullwidth
323characters (see http://www.unicode.org/reports/tr11/tr11-33.html) will occupy
3242 positions). This logic is very simple and covers wide range of cases. But
325obviously there a lot of cases when it is not sufficient. In such cases user
326should use some external libraries and provide an appropriate function to
327`libfort` via `ft_set_u8strwid_func` function.
328
329## Supported platforms and compilers
330
331The following compilers are currently used in continuous integration at [Travis](https://travis-ci.org/seleznevae/libfort), [AppVeyor](https://ci.appveyor.com/project/seleznevae/libfort) and [Cirrus](https://cirrus-ci.com/github/seleznevae/libfort):
332
333| Compiler           | Operating System             |
334|--------------------|------------------------------|
335| GCC 5.5.0          | Ubuntu 16.04.11              |
336| GCC 4.9.4          | Ubuntu 16.04.11              |
337| GCC 5.5.0          | Ubuntu 16.04.11              |
338| GCC 6.5.0          | Ubuntu 16.04.11              |
339| GCC 7.5.0          | Ubuntu 16.04.11              |
340| GCC 8.4.0          | Ubuntu 16.04.11              |
341| GCC 9.3.0          | Ubuntu 16.04.11              |
342| Clang 5.0.0        | Ubuntu 16.04.11              |
343| AppleClang 7.3.0   | Darwin Kernel Version 15.6.0 |
344| AppleClang 8.1.0   | Darwin Kernel Version 16.7.0 |
345| AppleClang 9.1.0   | Darwin Kernel Version 17.4.0 |
346| Clang 8.0.1        | FreeBSD 12.1                 |
347| Clang 11.0.1       | FreeBSD 13.0                 |
348| Visual Studio 2017 | Windows Server 2016          |
349
350
351Please note:
352- 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.
353
354
355## Contributing to libfort
356
357See the [contribution guidelines](https://github.com/seleznevae/libfort/blob/develop/CONTRIBUTING.md) for more information.
358
359
360## License
361
362<img align="right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png">
363
364The class is licensed under the [MIT License](http://opensource.org/licenses/MIT):
365
366Copyright &copy; 2017 - 2020 Seleznev Anton
367
368Permission 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:
369
370The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
371
372THE 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.
373
374
375
376
377
378