Added iomanipulators for all effects

This commit is contained in:
Bob Polis 2021-12-13 12:52:03 +01:00
parent 3e9c27de28
commit bacc189d99
4 changed files with 303 additions and 52 deletions

188
src/iomanip.cpp Normal file
View File

@ -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);
}

80
src/iomanip.hpp Normal file
View File

@ -0,0 +1,80 @@
#ifndef IOMANIP_H_
#define IOMANIP_H_
#include <iostream>
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_

View File

@ -5,6 +5,7 @@
#include <iomanip> #include <iomanip>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include "iomanip.hpp"
using namespace sc; using namespace sc;
@ -52,155 +53,140 @@ void term::progress(int prefixlen,
*_out << bar[steps % 8 - 1]; *_out << bar[steps % 8 - 1];
fill_len--; fill_len--;
} }
reset(); *_out << io::reset << std::string(fill_len, ' ');
*_out << std::string(fill_len, ' ');
*_out << ' ' << std::setw(3) << perc << '%'; *_out << ' ' << std::setw(3) << perc << '%';
} }
void term::hide_cursor() const { void term::hide_cursor() const {
*_out << "\x1b[?25l"; *_out << io::hide_cursor;
} }
void term::show_cursor() const { void term::show_cursor() const {
*_out << "\x1b[?25h"; *_out << io::show_cursor;
} }
void term::reset() const { void term::reset() const {
*_out << "\x1b[m"; *_out << io::reset;
} }
void term::bold() const { void term::bold() const {
*_out << "\x1b[1m"; *_out << io::bold;
} }
void term::italic() const { void term::italic() const {
*_out << "\x1b[3m"; *_out << io::italic;
} }
void term::underline() const { void term::underline() const {
*_out << "\x1b[4m"; *_out << io::underline;
} }
void term::overline() const { void term::overline() const {
*_out << "\x1b[53m"; *_out << io::overline;
} }
void term::strikethru() const { void term::strikethru() const {
*_out << "\x1b[9m"; *_out << io::strikethru;
} }
void term::blinkslow() const { void term::blinkslow() const {
*_out << "\x1b[5m"; *_out << io::blinkslow;
} }
void term::blinkfast() const { void term::blinkfast() const {
*_out << "\x1b[6m"; *_out << io::blinkfast;
} }
void term::reverse() const { void term::reverse() const {
*_out << "\x1b[7m"; *_out << io::reverse;
} }
void term::blackf() const { void term::blackf() const {
*_out << "\x1b[30m"; *_out << io::blackf;
} }
void term::redf() const { void term::redf() const {
*_out << "\x1b[31m"; *_out << io::redf;
} }
void term::greenf() const { void term::greenf() const {
*_out << "\x1b[32m"; *_out << io::greenf;
} }
void term::yellowf() const { void term::yellowf() const {
*_out << "\x1b[33m"; *_out << io::yellowf;
} }
void term::bluef() const { void term::bluef() const {
*_out << "\x1b[34m"; *_out << io::bluef;
} }
void term::magentaf() const { void term::magentaf() const {
*_out << "\x1b[35m"; *_out << io::magentaf;
} }
void term::cyanf() const { void term::cyanf() const {
*_out << "\x1b[36m"; *_out << io::cyanf;
} }
void term::whitef() const { void term::whitef() const {
*_out << "\x1b[37m"; *_out << io::whitef;
} }
void term::defaultf() const { void term::defaultf() const {
*_out << "\x1b[39m"; *_out << io::defaultf;
} }
void term::grayf(int val) const { void term::grayf(int val) const {
gray(val, 38); *_out << io::grayf(val);
} }
void term::rgbf(int r, int g, int b) const { 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 { void term::blackb() const {
*_out << "\x1b[40m"; *_out << io::blackb;
} }
void term::redb() const { void term::redb() const {
*_out << "\x1b[41m"; *_out << io::redb;
} }
void term::greenb() const { void term::greenb() const {
*_out << "\x1b[42m"; *_out << io::greenb;
} }
void term::yellowb() const { void term::yellowb() const {
*_out << "\x1b[43m"; *_out << io::yellowb;
} }
void term::blueb() const { void term::blueb() const {
*_out << "\x1b[44m"; *_out << io::blueb;
} }
void term::magentab() const { void term::magentab() const {
*_out << "\x1b[45m"; *_out << io::magentab;
} }
void term::cyanb() const { void term::cyanb() const {
*_out << "\x1b[46m"; *_out << io::cyanb;
} }
void term::whiteb() const { void term::whiteb() const {
*_out << "\x1b[47m"; *_out << io::whiteb;
} }
void term::defaultb() const { void term::defaultb() const {
*_out << "\x1b[49m"; *_out << io::defaultb;
} }
void term::grayb(int val) const { void term::grayb(int val) const {
gray(val, 48); *_out << io::grayb(val);
} }
void term::rgbb(int r, int g, int b) const { void term::rgbb(int r, int g, int b) const {
rgb(r, g, b, 48); *_out << io::rgbb(r, g, b);
}
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";
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -13,9 +13,6 @@ namespace sc {
struct winsize _ws; struct winsize _ws;
std::ostream* _out {nullptr}; std::ostream* _out {nullptr};
void gray(int val, int code) const;
void rgb(int r, int g, int b, int code) const;
public: public:
term(int fd); term(int fd);