xaizek / uncov (License: AGPLv3+) (since 2018-12-07)
Uncov(er) is a tool that collects and processes code coverage reports.
<root> / src / printing.cpp (5d6bd2cef0abd70b0ea0ac728b9aca74f86d5fcd) (14KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
// 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 version 3 of the GNU Affero General Public License as
// published by the Free Software Foundation.
//
// 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/>.

#include "printing.hpp"

#include <cstddef>

#include <functional>
#include <iomanip>
#include <memory>
#include <ostream>
#include <string>
#include <unordered_map>
#include <utility>

#include "utils/time.hpp"
#include "ColorCane.hpp"
#include "colors.hpp"
#include "decoration.hpp"

namespace {

/**
 * @brief Settings for this unit.
 *
 * Must be set before using the unit.
 */
std::shared_ptr<PrintingSettings> settings;

//! Specification of terminal color scheme.
const std::unordered_map<std::string, decor::Decoration> highlightGroups = {
    { "linesbad",      decor::bold + decor::red_fg   },
    { "linesok",       decor::bold + decor::black_fg },
    { "linesgood",     decor::bold + decor::green_fg },
    { "lineschanged",  decor::yellow_fg },
    { "covbad",        decor::bold + decor::red_fg },
    { "covok",         decor::bold + decor::black_fg },
    { "covnormal",     decor::bold + decor::yellow_fg },
    { "covgood",       decor::bold + decor::green_fg },
    { "lineno",        decor::white_bg + decor::black_fg },
    { "missed",        decor::red_fg + decor::inv + decor::bold },
    { "covered",       decor::green_fg + decor::inv + decor::bold },
    { "silentmissed",  decor::red_fg + decor::bold },
    { "silentcovered", decor::green_fg + decor::bold },
    { "added",         decor::green_fg + decor::bold },
    { "removed",       decor::red_fg + decor::bold },
    { "retained",      decor::none },
    { "note",          decor::none },
    { "header",        decor::white_fg + decor::black_bg + decor::bold +
                       decor::inv },
    { "error",         decor::red_bg + decor::inv + decor::bold },
    { "label",         decor::bold },
    { "revision",      decor::none },
    { "time",          decor::none },
    { "hitcount",      decor::none },
};

/**
 * @brief Decorates text with highlighting (either ASCII codes or HTML classes).
 */
class Highlight
{
public:
    /**
     * @brief Initializes highlighting with a name of highlight group.
     *
     * @param groupName Name of the group.
     */
    explicit Highlight(std::string groupName) : groupName(std::move(groupName))
    {
    }

    /**
     * @brief Constructs a new object which extends previous one by one action.
     *
     * @param hi  Object to be extend.
     * @param app Action to append to list of actions.
     */
    Highlight(Highlight &&hi, std::function<void(std::ostream&)> app)
        : groupName(std::move(hi.groupName)), apps(std::move(hi.apps))
    {
        apps.push_back(app);
    }

public:
    /**
     * @brief Prints decorated output into the stream.
     *
     * @param os Output stream.
     *
     * @returns @p os
     */
    std::ostream & decorate(std::ostream &os) const
    {
        const bool isHtmlOutput = settings->isHtmlOutput();

        if (isHtmlOutput) {
            const auto width = os.width({});
            os << "<span class=\"" << groupName << "\">";
            static_cast<void>(os.width(width));
        } else {
            os << highlightGroups.at(groupName);
        }

        for (const auto &app : apps) {
            app(os);
        }

        if (isHtmlOutput) {
            const auto width = os.width({});
            os << "</span>";
            static_cast<void>(os.width(width));
        } else {
            os << decor::def;
        }

        return os;
    }

private:
    const std::string groupName;                          //!< Highlight group.
    std::vector<std::function<void(std::ostream&)>> apps; //!< List of actions.
};

/**
 * @brief Appends element to the list of things to highlight.
 *
 * @tparam T Type of the element.
 *
 * @param hi  Highlight object.
 * @param val Element to append.
 *
 * @returns New highlight object.
 */
template <typename T>
Highlight
operator<<(Highlight &&hi, const T &val)
{
    return Highlight(std::move(hi), [val](std::ostream &os) { os << val; });
}

/**
 * @brief Outputs highlighted element into the stream.
 *
 * @param os Output stream.
 * @param hi Highlight object.
 *
 * @returns @p os
 */
inline std::ostream &
operator<<(std::ostream &os, const Highlight &hi)
{
    return hi.decorate(os);
}

/**
 * @brief Prints decorated number of hits.
 *
 * @param os     Output stream.
 * @param hits   Number of hits.
 * @param width  Width.
 * @param silent Whether to dim colors.
 *
 * @returns @p os
 */
std::ostream &
printHits(std::ostream &os, int hits, int width, bool silent)
{
    std::string prefix = silent ? "silent" : "";

    os << std::setw(width);

    if (hits == 0) {
        return os << (Highlight("hitcount") <<
                      (Highlight(prefix + "missed") << "x0" << ' '));
    } else if (hits > 0) {
        // 'x' and number must be output as a single unit here so that field
        // width applies correctly.
        return os << (Highlight("hitcount") <<
                      (Highlight(prefix + "covered")
                       << 'x' + std::to_string(hits) << ' '));
    } else {
        return os << (Highlight("hitcount") << "") << ' ';
    }
}

/**
 * @brief Prints decorated number of hits.
 *
 * @param cc     Output.
 * @param hits   Number of hits.
 * @param width  Width.
 * @param silent Whether to dim colors.
 *
 * @returns @p cc
 */
ColorCane &
printHits(ColorCane &cc, int hits, int width, bool silent)
{
    std::string value;
    ColorGroup group = ColorGroup::Irrelevant;

    if (hits == 0) {
        value = "x0";
        group = (silent ? ColorGroup::SilentMissed : ColorGroup::Missed);
    } else if (hits > 0) {
        value = 'x' + std::to_string(hits);
        group = (silent ? ColorGroup::SilentCovered : ColorGroup::Covered);
    }

    if (static_cast<int>(value.size()) < width) {
        value.insert(0U, width - value.size(), ' ');
    }

    value += ' ';
    cc.append(value, group);
    return cc;
}

}

void
PrintingSettings::set(std::shared_ptr<PrintingSettings> settings)
{
    ::settings = std::move(settings);
}

std::ostream &
operator<<(std::ostream &os, const CLinesChange &change)
{
    if (change.data < 0) {
        // XXX: highlight this as "ok" if not covered lines were not changed and
        //      relevant lines reduced by the same amount?
        return os << (Highlight("linesbad") << change.data);
    } else if (change.data == 0) {
        return os << (Highlight("linesok") << change.data);
    } else {
        return os << std::showpos
                  << (Highlight("linesgood") << change.data)
                  << std::noshowpos;
    }
}

std::ostream &
operator<<(std::ostream &os, const MLinesChange &change)
{
    if (change.data > 0) {
        return os << std::showpos
                  << (Highlight("linesbad") << change.data)
                  << std::noshowpos;
    } else if (change.data == 0) {
        return os << (Highlight("linesok") << change.data);
    } else {
        return os << (Highlight("linesgood") << change.data);
    }
}

std::ostream &
operator<<(std::ostream &os, const RLinesChange &change)
{
    if (change.data > 0) {
        os << std::showpos;
    }
    return os << (Highlight("lineschanged") << change.data) << std::noshowpos;
}

std::ostream &
operator<<(std::ostream &os, const CoverageChange &change)
{
    if (change.data < 0) {
        return os << (Highlight("covbad") << change.data << '%');
    } else if (change.data == 0) {
        return os << (Highlight("covok") << change.data << '%');
    } else {
        return os << std::showpos
                  << (Highlight("covgood") << change.data << '%')
                  << std::noshowpos;
    }
}

std::ostream &
operator<<(std::ostream &os, const Coverage &coverage)
{
    if (coverage.data < settings->getMedLimit()) {
        return os << (Highlight("covbad") << coverage.data << '%');
    } else if (coverage.data < settings->getHiLimit()) {
        return os << (Highlight("covnormal") << coverage.data << '%');
    } else {
        return os << (Highlight("covgood") << coverage.data << '%');
    }
}

std::ostream &
operator<<(std::ostream &os, const Label &label)
{
    return os << (Highlight("label") << label.data);
}

std::ostream &
operator<<(std::ostream &os, const ErrorMsg &errMsg)
{
    return os << (Highlight("error") << errMsg.data);
}

std::ostream &
operator<<(std::ostream &os, const TableHeader &th)
{
    return os << (Highlight("header") << th.data);
}

std::ostream &
operator<<(std::ostream &os, const LineNo &lineNo)
{
    os << std::setw(lineNo.data.width);
    return os << (Highlight("lineno") << std::to_string(lineNo.data.lineNo)
                                      << ' ');
}

std::ostream &
operator<<(std::ostream &os, const LineRetained &line)
{
    return os << (Highlight("retained") << ' ') << line.data;
}

std::ostream &
operator<<(std::ostream &os, const LineAdded &line)
{
    return os << (Highlight("added") << '+') << line.data;
}

std::ostream &
operator<<(std::ostream &os, const LineRemoved &line)
{
    return os << (Highlight("removed") << '-') << line.data;
}

std::ostream &
operator<<(std::ostream &os, const NoteMsg &line)
{
    return os << (Highlight("note") << " <<< " << line.data << " >>> ");
}

std::ostream &
operator<<(std::ostream &os, const HitsCount &hits)
{
    return printHits(os, hits.data.hits, hits.data.width, false);
}

std::ostream &
operator<<(std::ostream &os, const SilentHitsCount &hits)
{
    return printHits(os, hits.data.hits, hits.data.width, true);
}

std::ostream &
operator<<(std::ostream &os, const Revision &rev)
{
    return os << (Highlight("revision") << rev.data);
}

std::ostream &
operator<<(std::ostream &os, const Time &t)
{
    return os << (Highlight("time")
              << put_time(std::localtime(&t.data),
                          settings->getTimeFormat().c_str()));
}

ColorCane &
operator<<(ColorCane &cc, const ErrorMsg &errMsg)
{
    cc.append(errMsg.data, ColorGroup::ErrorMsg);
    return cc;
}

ColorCane &
operator<<(ColorCane &cc, const LineNo &lineNo)
{
    std::string value = (lineNo.data.lineNo == 0)
                      ? "-"
                      : std::to_string(lineNo.data.lineNo);
    if (static_cast<int>(value.size()) < lineNo.data.width) {
        value.insert(0U, lineNo.data.width - value.size(), ' ');
    }

    auto group = lineNo.data.original ? ColorGroup::OldLineNo
                                      : ColorGroup::NewLineNo;
    value += ' ';
    cc.append(value, group);
    return cc;
}

ColorCane &
operator<<(ColorCane &cc, const LineRetained &line)
{
    cc.append(boost::string_ref(), ColorGroup::RetainedMark);
    cc.append(line.data, ColorGroup::Pre);
    return cc;
}

ColorCane &
operator<<(ColorCane &cc, const LineAdded &line)
{
    cc.append(boost::string_ref(), ColorGroup::AddedMark);
    cc.append(line.data, ColorGroup::Pre);
    return cc;
}

ColorCane &
operator<<(ColorCane &cc, const LineRemoved &line)
{
    cc.append(boost::string_ref(), ColorGroup::RemovedMark);
    cc.append(line.data, ColorGroup::Pre);
    return cc;
}

ColorCane &
operator<<(ColorCane &cc, const NoteMsg &line)
{
    cc.append(line.data, ColorGroup::NoteMsg);
    return cc;
}

ColorCane &
operator<<(ColorCane &cc, const HitsCount &hits)
{
    return printHits(cc, hits.data.hits, hits.data.width, false);
}

ColorCane &
operator<<(ColorCane &cc, const SilentHitsCount &hits)
{
    return printHits(cc, hits.data.hits, hits.data.width, true);
}

std::ostream &
operator<<(std::ostream &os, const ColorCane &cc)
{
    for (const ColorCanePiece &piece : cc) {
        os << piece;
    }
    return os;
}

std::ostream &
operator<<(std::ostream &os, const ColorCanePiece &piece)
{
    // The code below sort of repeats code of operators above.  This is due to
    // types being different, which makes it harder to reuse the code.
    switch (piece.hi) {
        case ColorGroup::Pre:
            os << piece.text;
            break;
        case ColorGroup::OldLineNo:
        case ColorGroup::NewLineNo:
            os << (Highlight("lineno") << piece.text);
            break;
        case ColorGroup::AddedMark:
            os << (Highlight("added") << '+') << piece.text;
            break;
        case ColorGroup::RemovedMark:
            os << (Highlight("removed") << '-') << piece.text;
            break;
        case ColorGroup::RetainedMark:
            os << (Highlight("retained") << ' ') << piece.text;
            break;
        case ColorGroup::Missed:
            os << (Highlight("hitcount") <<
                   (Highlight("missed") << piece.text));
            break;
        case ColorGroup::SilentMissed:
            os << (Highlight("hitcount") <<
                   (Highlight("silentmissed") << piece.text));
            break;
        case ColorGroup::Covered:
            os << (Highlight("hitcount") <<
                   (Highlight("covered") << piece.text));
            break;
        case ColorGroup::SilentCovered:
            os << (Highlight("hitcount") <<
                   (Highlight("silentcovered") << piece.text));
            break;
        case ColorGroup::Irrelevant:
            os << (Highlight("hitcount") << piece.text);
            break;
        case ColorGroup::NoteMsg:
            os << (Highlight("note") << " <<< " << piece.text << " >>> ");
            break;
        case ColorGroup::ErrorMsg:
            os << (Highlight("error") << piece.text);
            break;
    }
    return os;
}
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