Skip to content

acassen/ascii-table

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny ASCII Table library

When you're working on the command-line interface (also called VTY), you're dealing with text formatted output. Most of the CLI available on network equipments are pretty straightforward, and honestly, most of the time, they're a bit boring. That's not a reason to hold back on proposing more fancy stuff.

In this attempt to conquer something that some might call "useless", we're introducing a source code implementing a tiny, simple, and straightforward ASCII table API. There are a lot of libraries out there on this topic, but most of them depend on massive code bases and are over-engineered. Most of us just need something with around 300 lines of code.

This C source code is simple enough to integrate into any existing codebase without any dependencies.

Content

* main.c   : Demo program
* Makefile : ...
* table.c  : Library source code file
* table.h  : Library header file

Example

#include <stdio.h>
#include <stdlib.h>
#include "table.h"

int main(int argc, char **argv)
{
	unsigned char buffer[4096] = {};
	struct table *tbl;

	tbl = table_init(3, STYLE_SINGLE_LINE_ROUNDED);
	table_set_column(tbl, "#", "Column 1", "Bigfoot Column 2");
	table_set_header_align(tbl, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER);
	table_set_column_align(tbl, ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT);
	table_add_row(tbl, "1", "str1", "str11 very long long 3");
	table_add_row(tbl, "2", "str2", "str21");
	table_add_row(tbl, "3", "str3", "str31");
	table_add_row_fmt(tbl, "%d|str%d|str%d1", 4, 4, 4);
	table_format(tbl, buffer, 4096);
	table_destroy(tbl);
	printf("%s\n", buffer);

    exit(0);
}

Give it a try

$ make
$ ./tiny-ascii-table
-=[ Style & Aligmnent demo ]=-
╭───┬──────────┬────────────────────────╮
│ # │ Column 1 │    Bigfoot Column 2    │
├───┼──────────┼────────────────────────┤
│ 1 │ str1     │ str11 very long long 3 │
│ 2 │ str2     │                  str21 │
│ 3 │ str3     │                  str31 │
│ 4 │ str4     │                  str41 │
│ 5 │ str5     │                  str51 │
│ 6 │ str6     │                  str61 │
│ 7 │ str7     │                  str71 │
╰───┴──────────┴────────────────────────╯

-=[ Built-in Styles available ]=-

-=[ Table Style : 0 ]=-
+---+----------+------------------------+
| # | Column 1 | Bigfoot Column 2       |
+---+----------+------------------------+
| 1 | str1     | str11 very long long 3 |
| 2 | str2     | str21                  |
| 3 | str3     | str31                  |
| 4 | str4     | str41                  |
| 5 | str5     | str51                  |
| 6 | str6     | str61                  |
| 7 | str7     | str71                  |
+---+----------+------------------------+


-=[ Table Style : 1 ]=-
.........................................
: # : Column 1 : Bigfoot Column 2       :
.........................................
: 1 : str1     : str11 very long long 3 :
: 2 : str2     : str21                  :
: 3 : str3     : str31                  :
: 4 : str4     : str41                  :
: 5 : str5     : str51                  :
: 6 : str6     : str61                  :
: 7 : str7     : str71                  :
.........................................


-=[ Table Style : 2 ]=-
┌───┬──────────┬────────────────────────┐
│ # │ Column 1 │ Bigfoot Column 2       │
├───┼──────────┼────────────────────────┤
│ 1 │ str1     │ str11 very long long 3 │
│ 2 │ str2     │ str21                  │
│ 3 │ str3     │ str31                  │
│ 4 │ str4     │ str41                  │
│ 5 │ str5     │ str51                  │
│ 6 │ str6     │ str61                  │
│ 7 │ str7     │ str71                  │
└───┴──────────┴────────────────────────┘


-=[ Table Style : 3 ]=-
╭───┬──────────┬────────────────────────╮
│ # │ Column 1 │ Bigfoot Column 2       │
├───┼──────────┼────────────────────────┤
│ 1 │ str1     │ str11 very long long 3 │
│ 2 │ str2     │ str21                  │
│ 3 │ str3     │ str31                  │
│ 4 │ str4     │ str41                  │
│ 5 │ str5     │ str51                  │
│ 6 │ str6     │ str61                  │
│ 7 │ str7     │ str71                  │
╰───┴──────────┴────────────────────────╯


-=[ Table Style : 4 ]=-

 # │ Column 1 │ Bigfoot Column 2       
───┼──────────┼────────────────────────
 1 │ str1     │ str11 very long long 3 
 2 │ str2     │ str21                  
 3 │ str3     │ str31                  
 4 │ str4     │ str41                  
 5 │ str5     │ str51                  
 6 │ str6     │ str61                  
 7 │ str7     │ str71                  



-=[ Table Style : 5 ]=-
╔═══╦══════════╦════════════════════════╗
║ # ║ Column 1 ║ Bigfoot Column 2       ║
╠═══╬══════════╬════════════════════════╣
║ 1 ║ str1     ║ str11 very long long 3 ║
║ 2 ║ str2     ║ str21                  ║
║ 3 ║ str3     ║ str31                  ║
║ 4 ║ str4     ║ str41                  ║
║ 5 ║ str5     ║ str51                  ║
║ 6 ║ str6     ║ str61                  ║
║ 7 ║ str7     ║ str71                  ║
╚═══╩══════════╩════════════════════════╝


-=[ Table Style : 6 ]=-
╔═══╤══════════╤════════════════════════╗
║ # │ Column 1 │ Bigfoot Column 2       ║
╟───┼──────────┼────────────────────────╢
║ 1 │ str1     │ str11 very long long 3 ║
║ 2 │ str2     │ str21                  ║
║ 3 │ str3     │ str31                  ║
║ 4 │ str4     │ str41                  ║
║ 5 │ str5     │ str51                  ║
║ 6 │ str6     │ str61                  ║
║ 7 │ str7     │ str71                  ║
╚═══╧══════════╧════════════════════════╝


-=[ Table Style : 7 ]=-

 # ║ Column 1 ║ Bigfoot Column 2       
═══╬══════════╬════════════════════════
 1 ║ str1     ║ str11 very long long 3 
 2 ║ str2     ║ str21                  
 3 ║ str3     ║ str31                  
 4 ║ str4     ║ str41                  
 5 ║ str5     ║ str51                  
 6 ║ str6     ║ str61                  
 7 ║ str7     ║ str71                  



-=[ Table Style : 8 ]=-
┏━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ # ┃ Column 1 ┃ Bigfoot Column 2       ┃
┣━━━╋━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ 1 ┃ str1     ┃ str11 very long long 3 ┃
┃ 2 ┃ str2     ┃ str21                  ┃
┃ 3 ┃ str3     ┃ str31                  ┃
┃ 4 ┃ str4     ┃ str41                  ┃
┃ 5 ┃ str5     ┃ str51                  ┃
┃ 6 ┃ str6     ┃ str61                  ┃
┃ 7 ┃ str7     ┃ str71                  ┃
┗━━━┻━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛


-=[ Table Style : 9 ]=-

 # ┃ Column 1 ┃ Bigfoot Column 2       
━━━╋━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━
 1 ┃ str1     ┃ str11 very long long 3 
 2 ┃ str2     ┃ str21                  
 3 ┃ str3     ┃ str31                  
 4 ┃ str4     ┃ str41                  
 5 ┃ str5     ┃ str51                  
 6 ┃ str6     ┃ str61                  
 7 ┃ str7     ┃ str71                  



-=[ Table Style : 10 ]=-
┏━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ # │ Column 1 │ Bigfoot Column 2       ┃
┠───┼──────────┼────────────────────────┨
┃ 1 │ str1     │ str11 very long long 3 ┃
┃ 2 │ str2     │ str21                  ┃
┃ 3 │ str3     │ str31                  ┃
┃ 4 │ str4     │ str41                  ┃
┃ 5 │ str5     │ str51                  ┃
┃ 6 │ str6     │ str61                  ┃
┃ 7 │ str7     │ str71                  ┃
┗━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━┛


-=[ Table Style : 11 ]=-
┏━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ # ┃ Column 1 ┃ Bigfoot Column 2       ┃
┣━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ 1 │ str1     │ str11 very long long 3 ┃
┃ 2 │ str2     │ str21                  ┃
┃ 3 │ str3     │ str31                  ┃
┃ 4 │ str4     │ str41                  ┃
┃ 5 │ str5     │ str51                  ┃
┃ 6 │ str6     │ str61                  ┃
┃ 7 │ str7     │ str71                  ┃
┗━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━┛


-=[ Table Style : 12 ]=-
┏━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ # │ Column 1 │ Bigfoot Column 2       ┃
┣━━━┿━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ 1 │ str1     │ str11 very long long 3 ┃
┃ 2 │ str2     │ str21                  ┃
┃ 3 │ str3     │ str31                  ┃
┃ 4 │ str4     │ str41                  ┃
┃ 5 │ str5     │ str51                  ┃
┃ 6 │ str6     │ str61                  ┃
┃ 7 │ str7     │ str71                  ┃
┗━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━┛


-=[ Table Style : 13 ]=-
▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ # ┃ Column 1 ┃ Bigfoot Column 2       ▐
▌━━━╋━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━▐
▌ 1 ┃ str1     ┃ str11 very long long 3 ▐
▌ 2 ┃ str2     ┃ str21                  ▐
▌ 3 ┃ str3     ┃ str31                  ▐
▌ 4 ┃ str4     ┃ str41                  ▐
▌ 5 ┃ str5     ┃ str51                  ▐
▌ 6 ┃ str6     ┃ str61                  ▐
▌ 7 ┃ str7     ┃ str71                  ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟

Nothing more, nothing less...

About

Tiny Ascii Table formatter library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published