From bacc189d997ff1b9fb9c37ec5172896657931833 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Mon, 13 Dec 2021 12:52:03 +0100 Subject: [PATCH] Added iomanipulators for all effects --- src/iomanip.cpp | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ src/iomanip.hpp | 80 +++++++++++++++++++++ src/utils.cpp | 84 +++++++++------------- src/utils.hpp | 3 - 4 files changed, 303 insertions(+), 52 deletions(-) create mode 100644 src/iomanip.cpp create mode 100644 src/iomanip.hpp diff --git a/src/iomanip.cpp b/src/iomanip.cpp new file mode 100644 index 0000000..5198d01 --- /dev/null +++ b/src/iomanip.cpp @@ -0,0 +1,188 @@ +#include "iomanip.hpp" + +static std::ostream& gray(std::ostream& out, int val, int code) { + if (val < 0 || val > 24) { + throw std::invalid_argument("gray value out of range"); + } + out << "\x1b[" << code << ";5;" << 232 + val << "m"; + return out; +} + +static std::ostream& rgb(std::ostream& out, int r, int g, int b, int code) { + if (r < 0 || r > 5 || g < 0 || g > 5 || b < 0 || b > 5) { + throw std::invalid_argument("rgb color component out of range"); + } + out << "\x1b[" << code << ";5;" << (16 + 36 * r + 6 * g + b) << "m"; + return out; +} + +std::ostream& sc::io::hide_cursor(std::ostream& out) { + out << "\x1b[?25l"; + return out; +} + +std::ostream& sc::io::show_cursor(std::ostream& out) { + out << "\x1b[?25h"; + return out; +} + +std::ostream& sc::io::reset(std::ostream& out) { + out << "\x1b[m"; + return out; +} + +std::ostream& sc::io::bold(std::ostream& out) { + out << "\x1b[1m"; + return out; +} + +std::ostream& sc::io::italic(std::ostream& out) { + out << "\x1b[3m"; + return out; +} + +std::ostream& sc::io::underline(std::ostream& out) { + out << "\x1b[4m"; + return out; +} + +std::ostream& sc::io::overline(std::ostream& out) { + out << "\x1b[53m"; + return out; +} + +std::ostream& sc::io::strikethru(std::ostream& out) { + out << "\x1b[9m"; + return out; +} + +std::ostream& sc::io::blinkslow(std::ostream& out) { + out << "\x1b[5m"; + return out; +} + +std::ostream& sc::io::blinkfast(std::ostream& out) { + out << "\x1b[6m"; + return out; +} + +std::ostream& sc::io::reverse(std::ostream& out) { + out << "\x1b[7m"; + return out; +} + +std::ostream& sc::io::blackf(std::ostream& out) { + out << "\x1b[30m"; + return out; +} + +std::ostream& sc::io::redf(std::ostream& out) { + out << "\x1b[31m"; + return out; +} + +std::ostream& sc::io::greenf(std::ostream& out) { + out << "\x1b[32m"; + return out; +} + +std::ostream& sc::io::yellowf(std::ostream& out) { + out << "\x1b[33m"; + return out; +} + +std::ostream& sc::io::bluef(std::ostream& out) { + out << "\x1b[34m"; + return out; +} + +std::ostream& sc::io::magentaf(std::ostream& out) { + out << "\x1b[35m"; + return out; +} + +std::ostream& sc::io::cyanf(std::ostream& out) { + out << "\x1b[36m"; + return out; +} + +std::ostream& sc::io::whitef(std::ostream& out) { + out << "\x1b[37m"; + return out; +} + +std::ostream& sc::io::defaultf(std::ostream& out) { + out << "\x1b[39m"; + return out; +} + +std::ostream& sc::io::blackb(std::ostream& out) { + out << "\x1b[40m"; + return out; +} + +std::ostream& sc::io::redb(std::ostream& out) { + out << "\x1b[41m"; + return out; +} + +std::ostream& sc::io::greenb(std::ostream& out) { + out << "\x1b[42m"; + return out; +} + +std::ostream& sc::io::yellowb(std::ostream& out) { + out << "\x1b[43m"; + return out; +} + +std::ostream& sc::io::blueb(std::ostream& out) { + out << "\x1b[44m"; + return out; +} + +std::ostream& sc::io::magentab(std::ostream& out) { + out << "\x1b[45m"; + return out; +} + +std::ostream& sc::io::cyanb(std::ostream& out) { + out << "\x1b[46m"; + return out; +} + +std::ostream& sc::io::whiteb(std::ostream& out) { + out << "\x1b[47m"; + return out; +} + +std::ostream& sc::io::defaultb(std::ostream& out) { + out << "\x1b[49m"; + return out; +} + +sc::io::grayf::grayf(int val) : _val {val} {} + +std::ostream& sc::io::operator<<(std::ostream &out, const grayf& obj) { + return gray(out, obj._val, 38); +} + +sc::io::grayb::grayb(int val) : _val {val} {} + +std::ostream& sc::io::operator<<(std::ostream &out, const grayb& obj) { + return gray(out, obj._val, 48); +} + +sc::io::rgbf::rgbf(int r, int g, int b) : + _r {r}, _g {g}, _b {b} {} + +std::ostream& sc::io::operator<<(std::ostream &out, const rgbf& obj) { + return rgb(out, obj._r, obj._g, obj._b, 38); +} + +sc::io::rgbb::rgbb(int r, int g, int b) : + _r {r}, _g {g}, _b {b} {} + +std::ostream& sc::io::operator<<(std::ostream &out, const rgbb& obj) { + return rgb(out, obj._r, obj._g, obj._b, 48); +} diff --git a/src/iomanip.hpp b/src/iomanip.hpp new file mode 100644 index 0000000..b161d68 --- /dev/null +++ b/src/iomanip.hpp @@ -0,0 +1,80 @@ +#ifndef IOMANIP_H_ +#define IOMANIP_H_ + +#include + +namespace sc { + namespace io { + std::ostream& hide_cursor(std::ostream& out); + std::ostream& show_cursor(std::ostream& out); + + std::ostream& reset(std::ostream& out); + + std::ostream& bold(std::ostream& out); + std::ostream& italic(std::ostream& out); + std::ostream& underline(std::ostream& out); + std::ostream& overline(std::ostream& out); + std::ostream& strikethru(std::ostream& out); + std::ostream& blinkslow(std::ostream& out); + std::ostream& blinkfast(std::ostream& out); + std::ostream& reverse(std::ostream& out); + + std::ostream& blackf(std::ostream& out); + std::ostream& redf(std::ostream& out); + std::ostream& greenf(std::ostream& out); + std::ostream& yellowf(std::ostream& out); + std::ostream& bluef(std::ostream& out); + std::ostream& magentaf(std::ostream& out); + std::ostream& cyanf(std::ostream& out); + std::ostream& whitef(std::ostream& out); + std::ostream& defaultf(std::ostream& out); + + std::ostream& blackb(std::ostream& out); + std::ostream& redb(std::ostream& out); + std::ostream& greenb(std::ostream& out); + std::ostream& yellowb(std::ostream& out); + std::ostream& blueb(std::ostream& out); + std::ostream& magentab(std::ostream& out); + std::ostream& cyanb(std::ostream& out); + std::ostream& whiteb(std::ostream& out); + std::ostream& defaultb(std::ostream& out); + + class grayf { + int _val; + + public: + grayf(int val); + friend std::ostream& operator<<(std::ostream& out, const grayf& obj); + }; + + class grayb { + int _val; + + public: + grayb(int val); + friend std::ostream& operator<<(std::ostream& out, const grayb& obj); + }; + + class rgbf { + int _r; + int _g; + int _b; + + public: + rgbf(int r, int g, int b); + friend std::ostream& operator<<(std::ostream& out, const rgbf& obj); + }; + + class rgbb { + int _r; + int _g; + int _b; + + public: + rgbb(int r, int g, int b); + friend std::ostream& operator<<(std::ostream& out, const rgbb& obj); + }; + } +} + +#endif // IOMANIP_H_ diff --git a/src/utils.cpp b/src/utils.cpp index fa7ad33..b309c07 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "iomanip.hpp" using namespace sc; @@ -52,155 +53,140 @@ void term::progress(int prefixlen, *_out << bar[steps % 8 - 1]; fill_len--; } - reset(); - *_out << std::string(fill_len, ' '); + *_out << io::reset << std::string(fill_len, ' '); *_out << ' ' << std::setw(3) << perc << '%'; } void term::hide_cursor() const { - *_out << "\x1b[?25l"; + *_out << io::hide_cursor; } void term::show_cursor() const { - *_out << "\x1b[?25h"; + *_out << io::show_cursor; } void term::reset() const { - *_out << "\x1b[m"; + *_out << io::reset; } void term::bold() const { - *_out << "\x1b[1m"; + *_out << io::bold; } void term::italic() const { - *_out << "\x1b[3m"; + *_out << io::italic; } void term::underline() const { - *_out << "\x1b[4m"; + *_out << io::underline; } void term::overline() const { - *_out << "\x1b[53m"; + *_out << io::overline; } void term::strikethru() const { - *_out << "\x1b[9m"; + *_out << io::strikethru; } void term::blinkslow() const { - *_out << "\x1b[5m"; + *_out << io::blinkslow; } void term::blinkfast() const { - *_out << "\x1b[6m"; + *_out << io::blinkfast; } void term::reverse() const { - *_out << "\x1b[7m"; + *_out << io::reverse; } void term::blackf() const { - *_out << "\x1b[30m"; + *_out << io::blackf; } void term::redf() const { - *_out << "\x1b[31m"; + *_out << io::redf; } void term::greenf() const { - *_out << "\x1b[32m"; + *_out << io::greenf; } void term::yellowf() const { - *_out << "\x1b[33m"; + *_out << io::yellowf; } void term::bluef() const { - *_out << "\x1b[34m"; + *_out << io::bluef; } void term::magentaf() const { - *_out << "\x1b[35m"; + *_out << io::magentaf; } void term::cyanf() const { - *_out << "\x1b[36m"; + *_out << io::cyanf; } void term::whitef() const { - *_out << "\x1b[37m"; + *_out << io::whitef; } void term::defaultf() const { - *_out << "\x1b[39m"; + *_out << io::defaultf; } void term::grayf(int val) const { - gray(val, 38); + *_out << io::grayf(val); } void term::rgbf(int r, int g, int b) const { - rgb(r, g, b, 38); + *_out << io::rgbf(r, g, b); } void term::blackb() const { - *_out << "\x1b[40m"; + *_out << io::blackb; } void term::redb() const { - *_out << "\x1b[41m"; + *_out << io::redb; } void term::greenb() const { - *_out << "\x1b[42m"; + *_out << io::greenb; } void term::yellowb() const { - *_out << "\x1b[43m"; + *_out << io::yellowb; } void term::blueb() const { - *_out << "\x1b[44m"; + *_out << io::blueb; } void term::magentab() const { - *_out << "\x1b[45m"; + *_out << io::magentab; } void term::cyanb() const { - *_out << "\x1b[46m"; + *_out << io::cyanb; } void term::whiteb() const { - *_out << "\x1b[47m"; + *_out << io::whiteb; } void term::defaultb() const { - *_out << "\x1b[49m"; + *_out << io::defaultb; } void term::grayb(int val) const { - gray(val, 48); + *_out << io::grayb(val); } void term::rgbb(int r, int g, int b) const { - rgb(r, g, b, 48); -} - -void term::gray(int val, int code) const { - if (val < 0 || val > 24) { - throw std::invalid_argument("gray value out of range"); - } - *_out << "\x1b[" << code << ";5;" << 232 + val << "m"; -} - -void term::rgb(int r, int g, int b, int code) const { - if (r < 0 || r > 5 || g < 0 || g > 5 || b < 0 || b > 5) { - throw std::invalid_argument("rgb color component out of range"); - } - *_out << "\x1b[" << code << ";5;" << (16 + 36 * r + 6 * g + b) << "m"; + *_out << io::rgbb(r, g, b); } // ------------------------------------------------------------------------ diff --git a/src/utils.hpp b/src/utils.hpp index 7b0557c..0d3315b 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -13,9 +13,6 @@ namespace sc { struct winsize _ws; std::ostream* _out {nullptr}; - void gray(int val, int code) const; - void rgb(int r, int g, int b, int code) const; - public: term(int fd);