xaizek / uncov (License: AGPLv3+) (since 2018-12-07)
Uncov(er) is a tool that collects and processes code coverage reports.
<root> / src / FilePrinter.hpp (713b3274c77fa2fd0d84c6c80b9ed7068b147175) (4,811B) (mode 100644) [raw]
// Copyright (C) 2016 xaizek <xaizek@posteo.net>
//
// This file is part of uncov.
//
// uncov is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// uncov 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with uncov.  If not, see <http://www.gnu.org/licenses/>.

#ifndef UNCOV__FILEPRINTER_HPP__
#define UNCOV__FILEPRINTER_HPP__

#include <srchilite/sourcehighlight.h>
#include <srchilite/langmap.h>

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

/**
 * @file FilePrinter.hpp
 *
 * @brief Printing files or their diffs annotated with coverage information.
 */

class FileComparator;

/**
 * @brief FilePrinter-specific settings.
 */
class FilePrinterSettings
{
public:
    /**
     * @brief Enable polymorphic destruction.
     */
    virtual ~FilePrinterSettings() = default;

public:
    /**
     * @brief Retrieves number of spaces that comprise a tabulation.
     *
     * @returns The number.
     */
    virtual int getTabSize() const = 0;

    /**
     * @brief Retrieves information about availability of color processing.
     *
     * @returns @c true if output can be colorized, @c false otherwise.
     */
    virtual bool isColorOutputAllowed() const = 0;

    /**
     * @brief Retrieves information about required output format.
     *
     * @returns @c true if output is HTML, @c false otherwise.
     */
    virtual bool isHtmlOutput() const = 0;
};

/**
 * @brief Prints highlighted files or their diffs annotated with coverage.
 */
class FilePrinter
{
public:
    /**
     * @brief Constructs an object performing some highlighting preparations.
     *
     * @param settings FilePrinter settings.
     */
    explicit FilePrinter(const FilePrinterSettings &settings);

public:
    /**
     * @brief Prints highlighted file with optional folding of covered lines.
     *
     * @param os Stream to print output to.
     * @param path Name of the file (for highlighting detection).
     * @param contents Contents of the file.
     * @param coverage Coverage information per line of contents.
     * @param leaveMissedOnly Fold lines which are covered or not relevant.
     *
     * @note @c coverage.size() should match lines in @p contents.
     */
    void print(std::ostream &os, const std::string &path,
               const std::string &contents, const std::vector<int> &coverage,
               bool leaveMissedOnly = false);

    /**
     * @brief Finds and prints differences between two versions of a file.
     *
     * Implements solution for longest common subsequence problem that matches
     * modified finding of edit distance (substitution operation excluded) with
     * backtracking afterward to compose result.  Requires lots of memory for
     * very big files.
     *
     * @param os Stream to print output to.
     * @param path Name of the file (for highlighting detection).
     * @param oText Old version of the file.
     * @param oCov Coverage of old version.
     * @param nText New version of the file.
     * @param nCov Coverage of new version.
     * @param comparator Object with loaded file diffing results.
     *
     * @note `oText.size() == oCov.size() && nText.size() == nCov.size()` is
     *       assumed.
     */
    void printDiff(std::ostream &os, const std::string &path,
                   std::istream &oText, const std::vector<int> &oCov,
                   std::istream &nText, const std::vector<int> &nCov,
                   const FileComparator &comparator);

private:
    /**
     * @brief Derives language from path.
     *
     * @param path Path to analyze.
     *
     * @returns Determined language.
     */
    std::string getLang(const std::string &path);
    /**
     * @brief Highlights source code.
     *
     * @param ss Stream for highlighted output.
     * @param text Code to highlight.
     * @param lang Language in which the code is written.
     * @param ranges Ranges of lines to be highlighted.
     */
    void highlight(std::stringstream &ss, std::istream &text,
                   const std::string &lang,
                   srchilite::LineRanges *ranges = nullptr);

private:
    //! Whether code highlighting is enabled.
    const bool colorizeOutput;
    //! Code highlighting object.
    srchilite::SourceHighlight highlighter;
    //! Loaded language map.
    srchilite::LangMap langMap;
};

#endif // UNCOV__FILEPRINTER_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/uncov

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

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