GithubHelp home page GithubHelp logo

phochmann / ctable Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 3.0 114 KB

C library to print nicely formatted tables

License: GNU General Public License v3.0

Makefile 1.40% C 98.60%
table console c ascii span align alignment

ctable's Introduction

build License: GPL v3

To do

  • Write documentation
  • More assertions and checks (bad calls should not lead to segfaults)
  • Remove MAX_COLS restriction (currently 11)

ctable

Library to print nicely formatted tables to stdout. Supports...

  • Cells spanning over multiple columns or rows
  • ANSI color sequences
  • Newlines in cell content
  • Alignment of numbers under decimal dot

Currently not supported...

  • wchars
  • Special chars like \t

How to use it

Include src/table.h to use it. Invoke make to run tests.

First, get a new table with get_empty_table(). Its current column and current row are set to 0. Cell insertions into the table always occur at the current column and current row. After a cell insertion, the current column advances. When styling a cell, call a setting-changing function before the cell insertion.

Example

Generated by running the tests:
Example Image
Generated by running the tests of ccalc:
Example Image

Functions

Data and printing

The following functions are used to obtain a new table, print it, and free it after use.

Table get_empty_table()

Returns a new table without any lines or set cells. All cells are styled to be left-aligned. Insertion begins at the upper left corner. You don't need to look into a Table directly, it suffices to use the following functions to manipulate it.

void print_table(Table *table)

Prints a table to stdout. This function is equivalent to fprint_table(table, stdout).

void fprint_table(Table *table, FILE *stream)

Prints a table to a specified stream.

void free_table(Table *table)

Frees all dynamic memory allocated for this table. It may not be used any more.

Control

The following functions change the position of next cell insertion.

void set_position(Table *table, size_t x, size_t y)

Sets position of next cell insertion. x = 0 is leftmost column, y = 0 is first row. Passing a large value for y may malloc a lot of rows, so use with care!

void next_row(Table *table)

Sets position of next cell insertion to the first column of next row relative to current insertion position.

Cell insertion

These functions insert a cell at the current position and advances the position to the next column (in the same row). When MAX_COLS many cells have been inserted into a row, next_row needs to be called. An inserted cell can not be changed any more.

void add_empty_cell(Table *table)

Adds an empty cell without any content.

void add_cell(Table *table, char *text)

Adds a cell with a specified text. You have to ensure that the passed pointer is still valid when the table is printed. If you don't want to maintain the buffer yourself, use add_cell_fmt.

void add_cell_gc(Table *table, char *text)

The same as add_cell, but frees the passed pointer on free_table, so use with care!

void add_cell_fmt(Table *table, char *fmt, ...)

Adds a cell with a text specified as if printed by printf. Buffer allocation and cleanup will be taken care of.

void add_cell_vfmt(Table *table, char *fmt, va_list args)

Flavor of add_cell_fmt that allows for va_lists, e.g. for a wrapper function.

void add_cells_from_array(Table *table, size_t width, size_t height, char **array)

Adds multiple cells with contents specified by a memory-contiguous 2D-Array. Insertion begins at current position, next position of insertion will be right to set cells in the same row. Strings will not be copied, so take care that pointers within the array are valid when the table is printed!

Cell styling

These functions style the cell that is added by next insertion (in the following called current cell). Already set cells can not be styled any more.

BorderStyle Description
BORDER_SINGLE A single line
BORDER_DOUBLE A double line
BORDER_NONE No line, useful for removing a line for a specific cell
TextAlignment Description
ALIGN_LEFT left-aligned
ALIGN_RIGHT right-aligned
ALIGN_CENTER centered (rounded to the left)
ALIGN_NUMBERS aligned under decimal dot and padding zeros

void set_default_alignments(Table *table, size_t num_alignments, TextAlignment *alignments)

Sets the default text alignment for each column.

void override_alignment(Table *table, TextAlignment alignment)

Overrides text alignment for current cell.

void override_alignment_of_row(Table *table, TextAlignment alignment)

Overrides text alignment for all cells of current row.

void set_hline(Table *table, BorderStyle style)

Inserts a horizontal line above the current row.

void set_vline(Table *table, size_t index, BorderStyle style)

Inserts a vertical line left to current column,

void make_boxed(Table *table, BorderStyle style)

Encloses the table in its current state by a box. Make sure to call next_row after last row to include it in box.

void set_all_vlines(Table *table, BorderStyle style)

Inserts vertical lines between all currently non-empty columns.

void override_left_border(Table *table, BorderStyle style)

Sets the left border of the current cell independently of default value for current column.

void override_above_border(Table *table, BorderStyle style)

Sets the above border for the current cell independently of default value for current row.

void set_span(Table *table, size_t span_x, size_t span_y)

Sets span for current cell. span_x denotes the number of columns to span over, span_y denotes the number of rows to span over.

ctable's People

Contributors

phochmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ctable's Issues

vec_create function does not check malloc return value for NULL

The function vec_create calls malloc but does not check the return value for NULL:

https://github.com/PhilippHochmann/ctable/blob/e9ac72d79177474be4b28f7584f2e5aefc5f6f2e/src/vector.c#L25-L38

vec_create is called by strbuilder_create is called by add_cell_vfmt which can pass the NULL as the text argument to add_cell_internal where it is assigned to the text member of a Cell struct (last line below):

https://github.com/PhilippHochmann/ctable/blob/e9ac72d79177474be4b28f7584f2e5aefc5f6f2e/src/table.c#L15-L24

This seems to create a large separation between where the problem is created (NULL returned from malloc) and where the problem will be noticed.

Possible extension: fprint_table

The function void print_table(Table *table) prints to stdout.
stdout is like any FILE pointer though.
Possible new function could be void fprint_table(FILE *stream, Table *table) that does the same as print_table but a different FILE pointer can be specified.

The naming mirrors the relationship of printf and fprintf where fprintf is the same but allows any FILE pointer to be used.

Return value from `malloc_row` is not checked for NULL

Example source

The README should mention that the code to generate the tables in the example images can be seen in tests/test_table.c

Even better would be showing example usage code in the README.md or a standalone example file.

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.