ComPPare 1.0.0
Loading...
Searching...
No Matches
comppare::internal::ansi::ScopedAnsiWrapper< T > Class Template Reference

Applying ANSI styles/colors to a value in a scope. More...

#include <ansi.hpp>

Collaboration diagram for comppare::internal::ansi::ScopedAnsiWrapper< T >:
[legend]

Public Member Functions

 ScopedAnsiWrapper (const char *on, const char *off, T v)
 

Private Attributes

const char * on_
 ANSI "on" code.
 
const char * off_
 ANSI "off" code.
 
val_
 The value to be wrapped.
 

Friends

std::ostream & operator<< (std::ostream &os, ScopedAnsiWrapper const &w)
 Overloaded operator<< to stream the value with ANSI codes.
 

Detailed Description

template<comppare::internal::concepts::Streamable T>
class comppare::internal::ansi::ScopedAnsiWrapper< T >

Applying ANSI styles/colors to a value in a scope.

The ScopedAnsiWrapper class applies an ANSI "on" code before the value and an "off" code immediately after. This makes styles and colors scoped to the wrapped value, unlike persistent toggles.

Example:

std::cout << comppare::internal::ansi::BOLD("Bold Text")
<< " normal text";

The text "Bold Text" is printed in bold, and the style is automatically reset afterwards.

Template Parameters
TThe type of the value to be wrapped. Must satisfy Streamable (i.e. it can be inserted into a std::ostream).

Implementation Details

Private Data Members:

  1. on_ – ANSI code to enable the style/color
  2. off_ – ANSI code to disable it
  3. val_ – the wrapped value

    operator<<:

  1. Saves the current formatting state of the stream with copyfmt
  2. Streams the value val_ into a temporary std::ostringstream
  3. Writes on_ + value + off_ to the original stream.
  4. Restores the saved formatting state so it does not affect formatting.

Friend operator<< The insertion operator is declared as a friend so it can access the wrapper’s private members on_, off_, and val_.:

friend std::ostream &operator<<(std::ostream &os,
ScopedAnsiWrapper const &w);
Applying ANSI styles/colors to a value in a scope.
Definition ansi.hpp:130
friend std::ostream & operator<<(std::ostream &os, ScopedAnsiWrapper const &w)
Overloaded operator<< to stream the value with ANSI codes.
Definition ansi.hpp:149

This allows syntax like:

std::cout << RED("Red") << " Normal";

Constructor & Destructor Documentation

◆ ScopedAnsiWrapper()

comppare::internal::ansi::ScopedAnsiWrapper< T >::ScopedAnsiWrapper ( const char *  on,
const char *  off,
v 
)
inline

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
ScopedAnsiWrapper< T > const &  w 
)
friend

Overloaded operator<< to stream the value with ANSI codes.

Parameters
osThe output stream
wThe ScopedAnsiWrapper instance
Returns
std::ostream& The modified output stream

Member Data Documentation

◆ off_

ANSI "off" code.

◆ on_

ANSI "on" code.

◆ val_

The value to be wrapped.


The documentation for this class was generated from the following file: