xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
<root> / src / decoration.hpp (1742ecf0d04291e502f7317d0e3b6594bbe0c54c) (12KiB) (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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
// Copyright (C) 2017 xaizek <xaizek@posteo.net>
//
// This file is part of zograscope.
//
// zograscope 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.
//
// zograscope 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 zograscope.  If not, see <http://www.gnu.org/licenses/>.

#ifndef ZOGRASCOPE_DECORATION_HPP_
#define ZOGRASCOPE_DECORATION_HPP_

#include <functional>
#include <iosfwd>
#include <memory>
#include <type_traits>
#include <vector>

#include <boost/variant.hpp>

class Tests;

/**
 * @brief Terminal control manipulators for output streams.
 *
 * Usage example:
 * @code
 * std::cout << decor::bold << "This is bold text." << decor::def;
 * @endcode
 *
 * Equivalent example with scope:
 * @code
 * std::cout << (decor::bold << "This is bold text.");
 * @endcode
 */
namespace decor {

/**
 * @brief Type of argument passed to extra decorators.
 */
using arg_t = boost::variant<int, std::string>;

/**
 * @brief Class describing single decoration or a combination of them.
 */
class Decoration
{
    using decorFunc = std::ostream & (*)(std::ostream &os);
    using extDecorFunc = std::ostream & (*)(std::ostream &os, arg_t arg);

public:
    /**
     * @brief Constructs possibly empty decoration.
     *
     * @param decorator Decorating function.
     */
    explicit Decoration(decorFunc decorator = nullptr);
    /**
     * @brief Constructs decoration from an unary function.
     *
     * @param extDecorator Decorating function.
     * @param arg Parameter for decorating function.
     */
    Decoration(extDecorFunc extDecorator, arg_t arg);
    /**
     * @brief Constructs a (deep) copy of a decoration.
     *
     * @param rhs Original object to be copied.
     */
    Decoration(const Decoration &rhs);
    /**
     * @brief Constructs a decoration that is a combination of others.
     *
     * @param lhs First decoration to combine.
     * @param rhs Second decoration to combine.
     */
    Decoration(const Decoration &lhs, const Decoration &rhs);
    /**
     * @brief Defaulted move constructor.
     *
     * @param rhs Object to move from.
     */
    Decoration(Decoration &&rhs) = default;

public:
    /**
     * @brief Assigns one decoration to another.
     *
     * @param rhs Assignment operand.
     *
     * @returns @c *this.
     */
    Decoration & operator=(const Decoration &rhs);

    /**
     * @brief Defaulted move assignment operator.
     *
     * @param rhs Assignment operand.
     *
     * @returns @c *this.
     */
    Decoration & operator=(Decoration &&rhs) = default;

    /**
     * @brief Actually performs the decoration of a stream.
     *
     * @param os Stream to decorate.
     *
     * @returns @p os.
     */
    std::ostream & decorate(std::ostream &os) const;

    /**
     * @brief Whether this decoration does nothing.
     *
     * @returns @c true if so, @c false otherwise.
     */
    bool isEmpty() const;

    /**
     * @brief Sets prefix decoration.
     *
     * @param dec Prefix decoraiton.
     *
     * @returns @c *this.
     */
    Decoration & prefix(Decoration dec);

    /**
     * @brief Sets suffix decoration.
     *
     * @param dec Prefix decoraiton.
     *
     * @returns @c *this.
     */
    Decoration & suffix(Decoration dec);

    /**
     * @brief Retrieves prefix decoration.
     *
     * @returns The decoration or @c nullptr.
     */
    const Decoration * getPrefix() const;

    /**
     * @brief Retrieves suffix decoration.
     *
     * @returns The decoration or @c nullptr.
     */
    const Decoration * getSuffix() const;

private:
    /**
     * @brief Decoration function (can be nullptr).
     */
    decorFunc decorator = nullptr;
    /**
     * @brief Unary decoration function (can be nullptr).
     */
    extDecorFunc extDecorator = nullptr;
    /**
     * @brief Parameter for extDecorator.
     */
    arg_t arg;
    /**
     * @brief One of two decorations that compose this object.
     */
    std::unique_ptr<Decoration> lhs;
    /**
     * @brief Second decoration that composes this object.
     */
    std::unique_ptr<Decoration> rhs;
    /**
     * @brief Prefix decoration (used by ScopedDecoration).
     */
    std::unique_ptr<Decoration> pfx;
    /**
     * @brief Suffix decoration (used by ScopedDecoration).
     */
    std::unique_ptr<Decoration> sfx;
};

/**
 * @brief Combines two decorations to create a new one.
 *
 * @param lhs First decoration to combine.
 * @param rhs Second decoration to combine.
 *
 * @returns The combination.
 */
inline Decoration
operator+(const Decoration &lhs, const Decoration &rhs)
{
    return Decoration(lhs, rhs);
}

/**
 * @brief A thunk for decoration use with @c std::ostream.
 *
 * @param os Stream to output to.
 * @param d Decoration to apply.
 *
 * @returns @p os.
 */
inline std::ostream &
operator<<(std::ostream &os, const Decoration &d)
{
    return d.decorate(os);
}

/**
 * @brief A scoped decoration container.
 *
 * Collects data that should be output to a stream and later replays it.
 */
class ScopedDecoration
{
public:
    /**
     * @brief Constructs an object from decoration and stream action.
     *
     * @param decoration Decoration of the scope.
     * @param app First application in the scope.
     */
    ScopedDecoration(const Decoration &decoration,
                     std::function<void(std::ostream&)> &&app)
        : decoration(decoration)
    {
        apps.reserve(1U);
        apps.emplace_back(std::move(app));
    }
    /**
     * @brief Constructs an object from scope and additional stream action.
     *
     * @param scoped Pre-existing scoped decoration which is being extended.
     * @param app Additional application in the scope.
     */
    ScopedDecoration(ScopedDecoration &&scoped,
                     std::function<void(std::ostream&)> &&app)
        : decoration(scoped.decoration), apps(std::move(scoped.apps))
    {
        apps.reserve(apps.size() + 1U);
        apps.emplace_back(std::move(app));
    }

public:
    /**
     * @brief Actually performs the decoration of a stream.
     *
     * @param os Stream to decorate.
     *
     * @returns @p os.
     */
    std::ostream & decorate(std::ostream &os) const;

private:
    /**
     * @brief Decoration object of this scope.
     */
    const Decoration &decoration;
    /**
     * @brief Stream actions in this scope.
     */
    std::vector<std::function<void(std::ostream&)>> apps;
};

/**
 * @brief Constructs scoped decoration.
 *
 * @tparam T Type of stream action.
 * @param d Decoration of the scope.
 * @param v Stream action.
 *
 * @returns Scoped decoration constructed.
 */
template <typename T>
typename std::enable_if<std::is_function<T>::value, ScopedDecoration>::type
operator<<(const Decoration &d, const T &v)
{
    const T *val = v;
    return ScopedDecoration(d, [val](std::ostream &os) { os << val; });
}

/**
 * @brief Constructs scoped decoration.
 *
 * @tparam T Type of stream data.
 * @param d Decoration of the scope.
 * @param v Stream data.
 *
 * @returns Scoped decoration constructed.
 */
template <typename T>
typename std::enable_if<!std::is_function<T>::value, ScopedDecoration>::type
operator<<(const Decoration &d, const T &val)
{
    return ScopedDecoration(d, [val](std::ostream &os) { os << val; });
}

/**
 * @brief Constructs scoped decoration.
 *
 * @tparam T Type of stream data.
 * @param d Decoration of the scope.
 * @param v Stream data.
 *
 * @returns Scoped decoration constructed.
 */
template <typename T>
typename std::enable_if<!std::is_function<T>::value, ScopedDecoration>::type
operator<<(Decoration &&d, const T &val)
{
    return ScopedDecoration(std::move(d),
                            [val](std::ostream &os) { os << val; });
}

/**
 * @brief Appends to scoped decoration.
 *
 * @tparam T Type of stream data.
 * @param sd Scoped decoration.
 * @param v Stream data.
 *
 * @returns Scoped decoration constructed.
 */
template <typename T>
ScopedDecoration
operator<<(ScopedDecoration &&sd, const T &val)
{
    return ScopedDecoration(std::move(sd),
                            [val](std::ostream &os) { os << val; });
}

/**
 * @brief A thunk for scoped decoration use with @c std::ostream.
 *
 * @param os Stream to output to.
 * @param sd Scoped decoration to apply.
 *
 * @returns @p os.
 */
inline std::ostream &
operator<<(std::ostream &os, const ScopedDecoration &sd)
{
    return sd.decorate(os);
}

/**
 * @{
 * @name Generic attributes
 */

/**
 * @brief Convenient attribute that does nothing.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration none;
/**
 * @brief Enables bold attribute.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration bold;
/**
 * @brief Enables color inversion attribute.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration inv;
/**
 * @brief Restores default attribute of the stream.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration def;

/**
 * @}
 *
 * @{
 * @name Foreground colors
 */

/**
 * @brief Picks black as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration black_fg;
/**
 * @brief Picks red as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration red_fg;
/**
 * @brief Picks green as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration green_fg;
/**
 * @brief Picks yellow as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration yellow_fg;
/**
 * @brief Picks blue as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration blue_fg;
/**
 * @brief Picks magenta as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration magenta_fg;
/**
 * @brief Picks cyan as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration cyan_fg;
/**
 * @brief Picks white as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration white_fg;

/**
 * @}
 *
 * @{
 * @name Background colors
 */

/**
 * @brief Picks black as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration black_bg;
/**
 * @brief Picks red as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration red_bg;
/**
 * @brief Picks green as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration green_bg;
/**
 * @brief Picks yellow as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration yellow_bg;
/**
 * @brief Picks blue as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration blue_bg;
/**
 * @brief Picks magenta as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration magenta_bg;
/**
 * @brief Picks cyan as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration cyan_bg;
/**
 * @brief Picks white as foreground color.
 *
 * @param os Stream to operate on.
 *
 * @returns @p os
 */
extern const Decoration white_bg;

/**
 * @}
 *
 * @{
 * @name Control
 */

/**
 * @brief Forces enabled state of decorations.
 */
void enableDecorations();

/**
 * @brief Forces disabled state of decorations.
 */
void disableDecorations();

/**
 * @}
 */

// TODO: document this
namespace literals {

    std::ostream & fg256(std::ostream &os, arg_t arg);
    std::ostream & bg256(std::ostream &os, arg_t arg);

    std::ostream & lit(std::ostream &os, arg_t arg);

    inline Decoration operator""_fg(unsigned long long n)
    {
        return Decoration(&fg256, n);
    }

    inline Decoration operator""_bg(unsigned long long n)
    {
        return Decoration(&bg256, n);
    }

    inline Decoration operator""_lit(const char s[], std::size_t)
    {
        return Decoration(&lit, s);
    }

}

}

#endif // ZOGRASCOPE_DECORATION_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/zograscope

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

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