ComPPare 1.0.0
Loading...
Searching...
No Matches
ansi.hpp File Reference

This file contains utilities for applying ANSI styles and colors to console output. More...

Include dependency graph for ansi.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  comppare::internal::ansi::ScopedAnsiWrapper< T >
 Applying ANSI styles/colors to a value in a scope. More...
 

Namespaces

namespace  comppare::internal::ansi
 Utilities for applying ANSI styles and colors to console output.
 
namespace  comppare
 ComPPare framework main namespace.
 
namespace  comppare::internal
 

Macros

#define ANSI_DEFINE(NAME, ON, OFF)
 Macro to define ANSI escape codes for text styling and colors.
 

Detailed Description

This file contains utilities for applying ANSI styles and colors to console output.

Author
Leong Fan FUNG (funglf) stanl.nosp@m.eyfu.nosp@m.nglf@.nosp@m.gmai.nosp@m.l.com
Date
2025
See also
LICENSE For full license text.

Usage

This file supports two usage patterns for formatting console output with ANSI escape codes:

1. Persistent toggles

  • Applied with stream insertion (<<).
  • Remain active until explicitly turned off with the corresponding _OFF code.
  • Examples: BOLD / BOLD_OFF, RED_ / RED_OFF.
std::cout << comppare::internal::ansi::BOLD
<< comppare::internal::ansi::RED
<< "Bold Red"
<< comppare::internal::ansi::RED_OFF
<< comppare::internal::ansi::BOLD_OFF
<< " Back to normal";

2. Scoped wrappers

  • Applied with function-call syntax, e.g. RED("Red").
  • Wrap the string with an "on" and "off" code automatically.
  • Useful for one-off highlighted messages.
std::cout << "Normal Colour"
<< comppare::internal::ansi::RED("Red")
<< "Normal Colour" << "\n";

3. Reset code

  • RESET can be used to reset all styles and colors to default. std::cout << comppare::internal::ansi::BOLD << comppare::internal::ansi::RED << "Bold Red" << comppare::internal::ansi::RESET << " Back to normal";

Macro Definition Documentation

◆ ANSI_DEFINE

#define ANSI_DEFINE (   NAME,
  ON,
  OFF 
)

Macro to define ANSI escape codes for text styling and colors.

This macro generates a pair of classes and its instances to represent an ANSI style or color. The resulting API supports both:

  • Persistent toggles (turn style/color on or off in the stream)
  • Scoped wrappers (apply ON/OFF just around a single value)

Implementation Details

Example with the style BOLD:

  1. On Off codes
  2. ON class (NAME##_ON_t)

    • operator<< — inserts the ON code directly into an ostream
    • operator() — wraps a value in a ScopedAnsiWrapper, which streams ON + value + OFF automatically
    class BOLD_ON_t {
    public:
    // Toggle on: std::cout << BOLD << "bold";
    friend std::ostream& operator<<(std::ostream&, BOLD_ON_t);
    // Scoped: std::cout << BOLD("bold");
    template<Streamable T>
    auto operator()(T&& v) const;
    };

    Instance:

    inline constexpr BOLD_ON_t BOLD{};

    Users actually call this instance, not the class itself:

    std::cout << BOLD; // BOLD_ON_t::operator<<
    std::cout << BOLD("msg"); // BOLD_ON_t::operator()
  3. OFF class (NAME##_OFF_t)

    • operator<< — inserts the OFF code directly into an ostream
    class BOLD_OFF_t {
    public:
    // Toggle off: std::cout << BOLD_OFF << "normal";
    friend std::ostream& operator<<(std::ostream&, BOLD_OFF_t);
    };

    Instance:

    inline constexpr BOLD_OFF_t BOLD_OFF{};

    Usage:

    std::cout << BOLD_OFF; // BOLD_OFF_t::operator<<
Parameters
NAMEThe identifier to generate (BOLD, RED, etc.)
ONNumeric ANSI code to enable
OFFNumeric ANSI code to disable/reset