libfort/README.md

138 lines
7.9 KiB
Markdown
Raw Normal View History

2018-05-07 19:42:04 +02:00
# libfort (Library to create FORmatted Tables)
2018-03-18 14:10:37 +01:00
2018-03-09 08:48:25 +01:00
[![Build Status](https://travis-ci.org/seleznevae/libfort.svg?branch=master)](https://travis-ci.org/seleznevae/libfort)
2018-03-18 09:55:54 +01:00
[![Build status](https://ci.appveyor.com/api/projects/status/ll1qygb56pho95xw/branch/master?svg=true)](https://ci.appveyor.com/project/seleznevae/libfort/branch/master)
2018-03-18 13:35:02 +01:00
[![Coverage Status](https://coveralls.io/repos/github/seleznevae/libfort/badge.svg?branch=master)](https://coveralls.io/github/seleznevae/libfort?branch=master)
2018-07-29 10:26:14 +02:00
[![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/tD15vwjIgeWmpPSX)
2018-03-18 09:54:09 +01:00
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
2018-03-18 14:03:23 +01:00
2018-08-06 22:27:10 +02:00
**libfort** is a simple crossplatform library to create formatted text 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 (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).
2018-05-07 19:42:04 +02:00
**Features:**
2018-08-06 22:27:10 +02:00
- Easy to integrate (only 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** direrctory)
2018-05-07 19:42:04 +02:00
- 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
2018-05-07 19:46:28 +02:00
- Support of wide characters
2018-05-07 19:42:04 +02:00
2018-05-09 09:46:35 +02:00
## 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
2018-05-08 21:13:52 +02:00
2018-05-07 20:19:53 +02:00
```C
2018-05-08 21:13:52 +02:00
#include <stdio.h>
2018-05-08 19:57:49 +02:00
#include "fort.h"
int main(void)
{
2018-05-08 20:12:20 +02:00
ft_table_t *table = ft_create_table();
/* Set "header" type for the first row */
2018-05-08 19:57:49 +02:00
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);
}
2018-05-07 20:19:53 +02:00
```
Output:
2018-05-08 19:57:49 +02:00
```text
+---+---------+-------------+
| N | Planet | Speed, km/s |
+---+---------+-------------+
| 1 | Mercury | 47.362 |
| 2 | Venus | 35.02 |
| 3 | Earth | 29.78 |
+---+---------+-------------+
```
2018-05-07 20:19:53 +02:00
2018-05-08 21:13:52 +02:00
### Customize table appearance
```C
2018-05-09 12:29:29 +02:00
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);
2018-05-08 21:13:52 +02:00
```
Output:
```text
2018-05-09 09:46:35 +02:00
╔══════════════════════════╤══════════════════════╤══════╤════════╗
║ 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 ║
╚══════════════════════════╧══════════════════════╧══════╧════════╝
2018-05-08 21:13:52 +02:00
```
2018-05-07 20:19:53 +02:00
2018-05-07 22:30:59 +02:00
## Installation
Copy 2 files **fort.c** and **fort.h** from [**lib**](https://github.com/seleznevae/libfort/tree/master/lib) direrctory to your project. Include **fort.h** in all sources where you will use libfort and compile with **fort.c**.
2018-05-07 19:42:04 +02:00
2018-03-18 14:03:23 +01:00
## Supported platforms and compilers
The following compilers are currently used in continuous integration at [Travis](https://travis-ci.org/seleznevae/libfort) and [AppVeyor](https://ci.appveyor.com/project/seleznevae/libfort):
| Compiler | Operating System |
|--------------------|------------------------------|
| GCC 4.8.4 | Ubuntu 14.04.3 |
2018-05-09 09:46:35 +02:00
| 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 |
2018-03-18 14:03:23 +01:00
| Clang 5.0.0 | Ubuntu 14.04.3 |
2018-05-09 12:46:42 +02:00
| 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 |
2018-05-09 09:46:35 +02:00
| Visual Studio 2017 | Windows Server 2016 |
2018-03-18 14:03:23 +01:00
2018-05-09 12:46:42 +02:00
Pease note:
2018-05-12 11:45:42 +02:00
- 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.
2018-05-09 12:46:42 +02:00
2018-03-18 14:03:23 +01:00
## License
<img align="right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png">
The class is licensed under the [MIT License](http://opensource.org/licenses/MIT):
Copyright &copy; 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.