Table layout library for command line.
- string and integer cell content
- left and right cell content alignment
- strong arity checks
- very high-level code (no loops used!)
Create a table of user names and their IDs from /etc/passwd
.
#include <stdlib.h>
#include <pwd.h>
#include <m_list.h>
int
main(void)
{
struct passwd* pwd;
struct tabl t;
struct m_list values;
tabl_init(&t);
tabl_add_column(&t, "UID", TABL_CONTENT_DECIMAL, TABL_ALIGN_RIGHT);
tabl_add_column(&t, "Name", TABL_CONTENT_STRING, TABL_ALIGN_LEFT);
m_list_init(&values);
while ((pwd = getpwent()) != NULL) {
m_list_clear(&values);
m_list_append(&values, M_LIST_COPY_DEEP, &pwd->pw_uid, sizeof(uid_t));
m_list_append(&values, M_LIST_COPY_DEEP, pwd->pw_name, strlen(pwd->pw_name)+1);
tabl_add_row(&t, &values);
}
endpwent();
tabl_render(&t);
return EXIT_SUCCESS;
}
Compile & run:
$ clang -o passwd passwd.c -ltabl
$ ./passwd
UID Name
0 root
8 news
9 man
22 sshd
25 smmsp
26 mailnull
68 pop
78 auditdistd
80 www
845 hast
65534 nobody
The usage of libtabl
is completely linear. The whole workflow has four steps:
initialisation, column creation, row insertion and rendering of the table. The
order of tasks must be preserved.
Use the tabl_init(struct tabl* t)
function to initialise a already allocated
table.
Each column is specified by the name
displayed in the header, the content
of the data in the column and the align
ment of the data. To append a new
column to the table, use the tabl_add_column(struct tabl* t, const char* name, uint8_t content, uint8_t align)
. Appending a new column after already
appending some rows will result in an runtime error.
Content can be one of the following:
TABL_CONTENT_STRING
which expect the data to be aNULL
-terminated stringTABL_CONTENT_DECIMAL
which expect a singleint
of data that will be printed in the decimal format
Alignment can be one of the following:
TABL_ALIGN_LEFT
align content to the leftTABL_ALIGN_RIGHT
align content to the right
Each row is represented as a separate m_list
. As a security measure, the
number of columns and number of elements in the row is compared, to ensure the
arity consistence. To append a row, use the tabl_add_row(struct tabl* t, struct m_list* row)
function.
To render the finished table onto the stdout
, use the tabl_render(struct tabl* t)
function.
- FreeBSD 10.0 with Clang 3.3
If a platform does not appear to be in the previous list, it does not mean that
libtabl
will not work in such environment. It only means that nobody tested
it - you are encouraged to do so and report either success or failure.
$ ninja
2-clause BSD license. For more information please consult the license file. In the case that you need a different license, feel free to contact me.
Daniel Lovasko (lovasko@freebsd.org)