Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/knz/align

Align: a command-line utility and C++ library to format tables.
https://github.com/knz/align

Last synced: 26 days ago
JSON representation

Align: a command-line utility and C++ library to format tables.

Awesome Lists containing this project

README

        

=======
Align
=======

Automatic column alignment for streamed tables
----------------------------------------------

**Align** is both a **C++ library** and a **command-line utility**.

- The C++ library **io::align** provides a simple interface to align
columns in tabular output performed through a standard C++ stream
(std::ostream).
- The utility **align** reads tabular data, aligns columns, and writes
the aligned table.

Both tools perform alignment while *streaming*: alignment occurs
line-by-line without buffering the entire table.

The tool **align** uses the library **io::align**.

.. contents::

When to use **align**
=====================

- To quickly reformat tabular data on the command line during
programming tasks or system administration.

- To format tabular data in C++ programs when the number of
rows is not known in advance.

- To approximate aligned output for large tables when the amount of
memory is limited, eg in embedded environments.

How to use **align**
====================

1. run ``configure``
2. run ``make``
3. run ``make install``
4. use ``align`` from the command line.

Example uses::

align -t: /etc/passwd

align -t: -T "name:pw:uid:gid:geos:home:shell" -p
int main()
{
std::ofstream os("/dev/stdout");

os << "Multiplication table modulo 11:" << std::endl;

// Create the align data structure.
io::align a;
// Create the output proxy.
io::align_proxy o = a.attach(os);

// Set the column widths.
for (int i = 1; i <= 10; ++i)
o << io::head("", 2);
o << io::endr;

// Produce a horizontal rule.
o << io::hline;

// Fill the table.
for (int i = 1; i <= 10; ++i)
for (int j = 1; j <= 10; ++j)
o << (i * j % 11) << io::next;

// Produce another horizontal rule.
o << io::hline;

return 0;
}

This program produces the following output::

Multiplication table modulo 11:
-- -- -- -- -- -- -- -- -- --
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 1 3 5 7 9
3 6 9 1 4 7 10 2 5 8
4 8 1 5 9 2 6 10 3 7
5 10 4 9 3 8 2 7 1 6
6 1 7 2 8 3 9 4 10 5
7 3 10 6 2 9 5 1 8 4
8 5 2 10 7 4 1 9 6 3
9 7 5 3 1 10 8 6 4 2
10 9 8 7 6 5 4 3 2 1
-- -- -- -- -- -- -- -- -- --

An API documentation is provided; check the ``doc`` subdirectory after
building the package with ``make doxygen-doc``.

Performance
===========

Using the enclosed ``bench.cc`` on a Core 2 Duo @ 2.4GHz and
optimization level ``-O3``, the following was measured:

========================= ==================== ======================= ==================
C++ toolchain Formatter Performance (rows/sec) Slowdown
========================= ==================== ======================= ==================
g++ 4.8 / libstdc++ none 1149950 N/A
g++ 4.8 / libstdc++ io::align 566160 50% (2x slower)

clang++ 3.3 / libc++ none 701035 N/A
clang++ 3.3 / libc++ io::align 411945 41% (1.7x slower)
========================= ==================== ======================= ==================

The memory usage should be related to the length of the longest row by
a constant factor, and does not grow with the number of rows.

Licensing
=========

**align** and **io::align** are free software. See the accompanying
file ``LICENSE`` for details.