std::ostream manipulators
One way of using this library is to use the ansi::manip namespace which provides std::ostream manipulators (like std::endl and co.).
The manipulators are named based on the effect they have on text, so the ansi::manip::bold manipulator boldens text.
Certain manipulators take parameters and return a wrapper struct with a lambda so the global namespace isn’t polluted with an overload for lambdas with that kind of type signature.
Some example code:
#include <iostream>
#incldue <Ansi>
int main() {
std::cout << ansi::manip::bold << "Bold text" << std::endl;
std::cout << ansi::manip::color(ansi::Red) << "Red and bold text" << std::endl;
std::cout << ansi::manip::reset << "Normal text" << std::endl;
}
- The supported text effects are:
bold
faint
italic
underline
blink (usually not supported by terminals)
reverse
hidden
strikethrough
Individual text effects can be reset by prefixing the function name with no, ie. ansi::manip::nobold. Or all effects (including color) can be reset using ansi::manip::reset.
The manip namespace has the following members:
-
namespace ansi::manip
Manipulators for
std::ostream
Contains various manipulators for ANSI Compliant terminals. Each one applies a certain modifier to all following text (ie. bold, underline), or moves the cursor around and erases characters. These are meant to be used with the operator<< to change the output stream of stdout
Some examples:
#include <iostream> #include <Ansi> int main() { std::cout << ansi::manip::bold << ansi::manip::color(ansi::Red) << std::endl; }
Functions
-
auto modifier(TextModifier mod, bool reset = false) -> ManipulatorFunc
Creates a manipulator for the desired text styling.
- Parameters
mod – Text styling
reset – Should the styling be reset
- Returns
The manipulator to be used with operator<<
-
auto color(Color c, bool is_background = false) -> ManipulatorFunc
Creates a manipulator for the desired color.
- Parameters
c – The color in question
is_background – Is the color for the background color of the terminal or the foreground. Defaults to foreground
- Returns
The manipulator to be used with operator<<
-
auto color(uint8_t color, bool is_background = false) -> ManipulatorFunc
Creates a manipulator for the desired color.
- Parameters
color – The 256 term color to use
is_background – Is the color for the background color of the terminal or the foreground. Defaults to foreground
- Returns
The manipulator to be used with operator<<
-
auto color(uint8_t r, uint8_t g, uint8_t b, bool is_background = false) -> ManipulatorFunc
Creates a manipulator for the desired color.
- Parameters
r – Red channel
g – Green channel
b – Blue channel
is_background – Is the color for the background color of the terminal or the foreground. Defaults to foreground
- Returns
The manipulator to be used with operator<<
-
auto move_cursor(CursorDirection direction, uint8_t amount) -> ManipulatorFunc
Creates a manipulator to move the cursor.
- Parameters
direction – The direction of the desired movement
amount – The amount to move in that direction
- Returns
The manipulator to be used with operator<<
-
auto move_cursor(uint8_t x, uint8_t y) -> ManipulatorFunc
Creates a manipulator to move the cursor to the desired coordinates.
- Parameters
x – X coord of the movement
y – Y coord of the movement
- Returns
The manipulator to be used with operator<<
-
auto move_cursor_home(std::ostream &os) -> std::ostream&
Moves the cursor to home (0, 0)
- Parameters
os – The stream to manipulate
- Returns
The manipulated stream
-
auto modifier(TextModifier mod, bool reset = false) -> ManipulatorFunc