xaizek / dit (License: GPLv3) (since 2018-12-07)
Command-line task keeper that remembers all old values and is meant to combine several orthogonal features to be rather flexible in managing items.
<root> / src / ItemTable.hpp (d69e4b2f2ce5f66ad705af1f992e0d2bc69dfbad) (3,406B) (mode 100644) [raw]
// Copyright (C) 2015 xaizek <xaizek@posteo.net>
//
// This file is part of dit.
//
// dit is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU Affero General Public
// License as published by the Free Software Foundation.
//
// dit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with dit.  If not, see <http://www.gnu.org/licenses/>.

#ifndef DIT__ITEMTABLE_HPP__
#define DIT__ITEMTABLE_HPP__

#include <functional>
#include <iosfwd>
#include <string>
#include <vector>

class Item;

struct ColorRule;

/**
 * @brief Item table formatter and printer.
 *
 * Format and sorting are configurable via constructor's parameters.
 */
class ItemTable
{
    class Column;

public:
    /**
     * @brief Constructs the table formatter.
     *
     * @param fmt Format specification: <field>,<field>...
     * @param colorSpec Colorization specification: <dec>... <cond>... ; ...
     * @param sort Multi-key sorting specification: <field>,<field>...
     * @param maxWidth Maximum allowed table width.
     *
     * @throws std::runtime_error On failed parsing of @p colorSpec.
     */
    ItemTable(const std::string &fmt, const std::string &colorSpec,
              std::string sort, unsigned int maxWidth);
    /**
     * @brief To emit destructing code in corresponding source file.
     */
    ~ItemTable();

public:
    /**
     * @brief Adds item to the table.
     *
     * @param item Item to add.
     */
    void append(Item &item);
    /**
     * @brief Prints table on standard output.
     *
     * @param os Output stream.
     */
    void print(std::ostream &os);

private:
    /**
     * @brief Ensures that items are in correct order.
     */
    void sortItems();
    /**
     * @brief Populates columns with items' data.
     */
    void fillColumns();
    /**
     * @brief Ensures that columns fit into required width limit.
     *
     * @returns @c true on successful shrinking or @c false on failure.
     */
    bool adjustColumnsWidths();
    /**
     * @brief Print table heading.
     *
     * @param os Output stream.
     */
    void printTableHeader(std::ostream &os);
    /**
     * @brief Prints table lines.
     *
     * @param os Output stream.
     */
    void printTableRows(std::ostream &os);
    /**
     * @brief Applies item/structure-specific decorators to a stream.
     *
     * @param os Stream to be decorated.
     * @param item Item for which stream should be decorated or @c nullptr for
     *             headers.
     *
     * @returns @p os
     */
    std::ostream & decorate(std::ostream &os, Item *item);

private:
    /**
     * @brief Sorting specification.
     */
    const std::string sort;
    /**
     * @brief Maximum allowed table width.
     */
    const unsigned int maxWidth;
    /**
     * @brief List of columns of the table (built from the format).
     */
    std::vector<Column> cols;
    /**
     * @brief List of items to display.
     */
    std::vector<std::reference_wrapper<Item>> items;
    /**
     * @brief Rules for table colorization.
     */
    std::vector<ColorRule> colorRules;
};

#endif // DIT__ITEMTABLE_HPP__
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/dit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/dit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master