Git Product home page Git Product logo

tblr's Introduction

build Coverage Status

tblr (Tabular)

One header library for easy pretty printing of tables in C++. Supports markdown and latex. Use it by including tblr.h in your project.


  • Create a Table and stream cell by cell. Use endr to end a row.
Table t;
t << "animal" << "does what" << "when" << endr
  << "dog" << "barks" << "angry" << endr
  << "rooster" << "crows" << "in the morning" << endr
  << "computer" << "crashes" << "at night" << endr;
std::cout << t << std::endl;
animal   does what when
dog      barks     angry
rooster  crows     in the morning
computer crashes   at night
  • Stream multiply into the same cell by using Cell()
Table t;
t << "animal" << "does what" << "when" << endr;
for (int i = 0; i < 3; i ++) {
  t << (Cell() << "dog " << i)
    << (Cell() << "barks " << i << " times")
    << "angry" << endr;
}
std::cout << t << std::endl;
animal does what     when
dog 0  barks 0 times angry
dog 1  barks 1 times angry
dog 2  barks 2 times angry
  • Specify widths and alignments (justifications)
Table t;
t.aligns({Left, Center, Right})
 .widths({10, 10, 0}); // 0 is automatic
t << "animal" << "does what" << "when" << endr
  << "dog" << "barks" << "angry" << endr
  << "rooster" << "crows" << "in the morning" << endr
  << "computer" << "crashes" << "at night" << endr;
std::cout << t << std::endl;
animal     does what            when
dog          barks             angry
rooster      crows    in the morning
computer    crashes         at night
  • Specify what to do when content does not fit in width
Table t;
t.widths({10, 10, 7})
 .multiline(Space);
// t << ...;
std::cout << t << std::endl;
animal     does what  when
dog        barks      angry
rooster    crows      in the
                      morning
computer   crashes    at
                      night
t.multiline(Naive);
std::cout << t << std::endl;
animal     does what  when
dog        barks      angry
rooster    crows      in the
                      morning
computer   crashes    at nigh
                      t
t.multiline(SingleLine);
std::cout << t << std::endl;
animal     does what  when
dog        barks      angry
rooster    crows      in the
computer   crashes    at nigh
  • Specify layouts
Table t;
t.layout(markdown())
 .aligns({Left, Center, Right})
 .widths({10, 10, 0}); // 0 is automatic
// t << ...;
std::cout << t << std::endl;
| animal     | does what  |           when |
| ---------- | ---------- | -------------- |
| dog        |   barks    |          angry |
| rooster    |   crows    | in the morning |
| computer   |  crashes   |       at night |
t.layout(latex());
std::cout << t << std::endl;
\begin{tabular}{lcr}
\hline
animal     & does what  &           when \\
\hline
dog        &   barks    &          angry \\
rooster    &   crows    & in the morning \\
computer   &  crashes   &       at night \\
\hline
\end{tabular}
t.layout(unicode_box_light());
std::cout << t << std::endl;
┌──────────┬──────────┬──────────────┐
│animal    │does what │          when│
├──────────┼──────────┼──────────────┤
│dog       │  barks   │         angry│
├──────────┼──────────┼──────────────┤
│rooster   │  crows   │in the morning│
├──────────┼──────────┼──────────────┤
│computer  │ crashes  │      at night│
└──────────┴──────────┴──────────────┘
  • Customize layouts
auto layout = simple_border(/*left      */ "|| ",
                            /*mid       */ " | ",
                            /*right     */ " ||",
                            /*top       */ "^",
                            /*header mid*/ "-.",
                            /*mid       */ "-",
                            /*bottom    */ "vV");
Table t;
t.layout(layout)
 .aligns({Left, Center, Right})
 .widths({10, 10, 0}); // 0 is automatic
// t << ...;
std::cout << t << std::endl;
|| ^^^^^^^^^^ | ^^^^^^^^^^ | ^^^^^^^^^^^^^^ ||
|| animal     | does what  |           when ||
|| -.-.-.-.-. | -.-.-.-.-. | -.-.-.-.-.-.-. ||
|| dog        |   barks    |          angry ||
|| ---------- | ---------- | -------------- ||
|| rooster    |   crows    | in the morning ||
|| ---------- | ---------- | -------------- ||
|| computer   |  crashes   |       at night ||
|| vVvVvVvVvV | vVvVvVvVvV | vVvVvVvVvVvVvV ||

See tblr.h for finer grained layout customization.

  • UTF8
Table t;
 t.layout(markdown());
 t << "Op" << "Time" << endr
   << "f(x)" << "3ms" << endr
   << "∂f(x)/∂x" << "4μs" << endr;
 std::cout << t << std::endl;
| Op       | Time |
| -------- | ---- |
| f(x)     | 3ms  |
| ∂f(x)/∂x | 4μs  |
  • Tables within tables
Table t, mini;
mini.layout(markdown()).aligns({Right, Left}).widths({2, 2});
mini << "T" << "B" << endr
     << "L" << "R" << endr;

t.layout(extra_space());
t << mini << mini << endr
  << mini << mini << endr;

std::cout << t << std::endl;

  |  T | B  |  |  T | B  |
  | -- | -- |  | -- | -- |
  |  L | R  |  |  L | R  |

  |  T | B  |  |  T | B  |
  | -- | -- |  | -- | -- |
  |  L | R  |  |  L | R  |

  • ANSI color codes in tables
const auto green = "\033[1;32m";
const auto red = "\033[1;31m";
const auto blue = "\033[1;34m";
const auto underline = "\033[1;4m";
const auto clear = "\033[0m";

Table t;
t.layout(unicode_box_light())
    .widths({10, 10, 7})
    .aligns({Left, Left, Right})
    .multiline(Space);
t << "animal" << "does what" << "when" << endr
  << "dog" << "barks" << "angry" << endr
  << "rooster" << "crows"
               << (Cell() << green << "in the cool breezy morning" << clear)
               << endr
  << "computer" << "crashes"
                << (Cell() << underline << red << "at warm co"
                           << blue << "zy night" << clear)
                << endr;
std::cout << t << std::endl;

Limitations

  • Limited unicode support: UTF8 encoded unicode is generally supported but each character is assumed to be the same width as an ascii character. This is not true for zero-width or full-width characters. See demo.cpp, or above for a working example.

Building

make -j2 test demo

If you are using Visual Studio, you need the /permissive- option.

tblr's People

Contributors

oir avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.