added invert option

This commit is contained in:
rexy712 2019-01-13 18:46:27 -08:00
parent 64ac375fff
commit 1e05197772
6 changed files with 65 additions and 7 deletions

View File

@ -31,12 +31,16 @@
template<int I> template<int I>
constexpr basic_color_printer<I>::basic_color_printer(const cmd_args& args)noexcept: constexpr basic_color_printer<I>::basic_color_printer(const cmd_args& args)noexcept:
color_printer_base<basic_color_printer<I>>(args), color_printer_base<basic_color_printer<I>>(args, args.invert ? L"\033[38;5;0m\033[48;5;%sm" : L"\033[38;5;%sm"),
m_randomness(rand()%color_lookup_len){} m_randomness(rand()%color_lookup_len){}
template<int I> template<int I>
basic_color_printer<I>& basic_color_printer<I>::_print(const wchar_t s)noexcept{ basic_color_printer<I>& basic_color_printer<I>::_print(const wchar_t s)noexcept{
if(s == L'\n'){ if(s == L'\n'){
inc_line(); inc_line();
if(this->m_invert)
this->reset();
putwchar(L'\n');
return *this;
}else{ }else{
int width = wcwidth(s); int width = wcwidth(s);
if(width > 0) if(width > 0)
@ -49,7 +53,7 @@ basic_color_printer<I>& basic_color_printer<I>::_print(const wchar_t s)noexcept{
new_index += color_lookup_len; new_index += color_lookup_len;
if(new_index != m_index){ if(new_index != m_index){
m_index = new_index; m_index = new_index;
wprintf(L"\033[38;5;%sm", color_lookup_table[m_index]); wprintf(this->m_color_str, color_lookup_table[m_index]);
} }
putwchar(s); putwchar(s);
return *this; return *this;

View File

@ -45,11 +45,12 @@ struct cmd_args{
std::vector<const char*> filenames; std::vector<const char*> filenames;
//lolcat options //lolcat options
long seed = 0;
float freq = 0.2f; float freq = 0.2f;
float spread = 0.3f; float spread = 0.3f;
long seed = 0;
unsigned truecol:1; unsigned truecol:1;
unsigned color:2; unsigned color:2;
unsigned invert:1;
//GNU cat options //GNU cat options
unsigned number:2; unsigned number:2;

View File

@ -27,12 +27,15 @@ template<class Derived>
class color_printer_base : public printer_base<Derived> class color_printer_base : public printer_base<Derived>
{ {
protected: protected:
const wchar_t* const m_color_str;
const float m_freq; const float m_freq;
const float m_spread; const float m_spread;
const bool m_invert;
protected: protected:
constexpr color_printer_base(const cmd_args& args)noexcept: constexpr color_printer_base(const cmd_args& args, const wchar_t* col_str)noexcept:
printer_base<Derived>(args.tabs ? SHOW_TABS_STRING : L" ", args), printer_base<Derived>(args.tabs ? SHOW_TABS_STRING : L" ", args),
m_freq(args.freq), m_spread(m_freq < 0 ? -args.spread : args.spread){} m_color_str(col_str),
m_freq(args.freq), m_spread(m_freq < 0 ? -args.spread : args.spread), m_invert(args.invert){}
Derived& _reset(void){ Derived& _reset(void){
wprintf(L"\033[0m"); wprintf(L"\033[0m");
return static_cast<Derived&>(*this); return static_cast<Derived&>(*this);

View File

@ -96,7 +96,7 @@ private:
}m_line_color = {}, m_color = m_line_color; }m_line_color = {}, m_color = m_line_color;
public: public:
constexpr color_printer_true(const cmd_args& args)noexcept: constexpr color_printer_true(const cmd_args& args)noexcept:
color_printer_base<color_printer_true>(args){} color_printer_base<color_printer_true>(args, args.invert ? L"\033[38;5;0m\033[48;2;%s;%s;%sm" : L"\033[38;2;%s;%s;%sm"){}
protected: protected:
color_printer_true& _print(const wchar_t s)noexcept{ color_printer_true& _print(const wchar_t s)noexcept{
@ -104,6 +104,10 @@ protected:
if(s == L'\n'){ if(s == L'\n'){
inc_line(); inc_line();
updated = true; updated = true;
if(m_invert)
reset();
putwchar(L'\n');
return *this;
}else{ }else{
int width = wcwidth(s); int width = wcwidth(s);
if(width > 0) if(width > 0)
@ -112,10 +116,13 @@ protected:
updated = inc_color(1); updated = inc_color(1);
} }
if(updated) if(updated)
wprintf(L"\033[38;2;%s;%s;%sm", lookup_table[m_color.r], lookup_table[m_color.g], lookup_table[m_color.b]); wprintf(m_color_str, lookup_table[m_color.r], lookup_table[m_color.g], lookup_table[m_color.b]);
putwchar(s); putwchar(s);
return *this; return *this;
} }
color_printer_true& _reset(void){
return color_printer_base<color_printer_true>::_reset();
}
constexpr void inc_line(void){ constexpr void inc_line(void){
int amount = m_spread*(float{color_max_amount}/4); int amount = m_spread*(float{color_max_amount}/4);
_do_inc_color(amount, m_line_color); _do_inc_color(amount, m_line_color);

View File

@ -82,6 +82,7 @@ static constexpr const char FREQ_LONG_OPTION[] = "freq" REQ_FLOAT_AR
static constexpr const char TRUECOLOR_LONG_OPTION[] = "truecolor"; static constexpr const char TRUECOLOR_LONG_OPTION[] = "truecolor";
static constexpr const char FORCE_COLOR_LONG_OPTION[] = "force"; static constexpr const char FORCE_COLOR_LONG_OPTION[] = "force";
static constexpr const char DISABLE_COLOR_LONG_OPTION[] = "no-color"; static constexpr const char DISABLE_COLOR_LONG_OPTION[] = "no-color";
static constexpr const char INVERT_LONG_OPTION[] = "invert";
static constexpr const char IGNORE_ANIMATE_LONG_OPTION[] = "animate"; static constexpr const char IGNORE_ANIMATE_LONG_OPTION[] = "animate";
static constexpr const char IGNORE_DURATION_LONG_OPTION[] = "duration" REQ_INTEGER_ARG; static constexpr const char IGNORE_DURATION_LONG_OPTION[] = "duration" REQ_INTEGER_ARG;
@ -96,6 +97,7 @@ static constexpr const char SHOW_ENDS_SHORT_OPTION[] = "E";
static constexpr const char FORCE_COLOR_SHORT_OPTION[] = "f"; static constexpr const char FORCE_COLOR_SHORT_OPTION[] = "f";
static constexpr const char FREQ_SHORT_OPTION[] = "F"; static constexpr const char FREQ_SHORT_OPTION[] = "F";
static constexpr const char USAGE_SHORT_OPTION[] = "h"; static constexpr const char USAGE_SHORT_OPTION[] = "h";
static constexpr const char INVERT_SHORT_OPTION[] = "i";
static constexpr const char NUMBER_LINES_SHORT_OPTION[] = "n"; static constexpr const char NUMBER_LINES_SHORT_OPTION[] = "n";
static constexpr const char DISABLE_COLOR_SHORT_OPTION[] = "N"; static constexpr const char DISABLE_COLOR_SHORT_OPTION[] = "N";
static constexpr const char SPREAD_SHORT_OPTION[] = "p"; static constexpr const char SPREAD_SHORT_OPTION[] = "p";
@ -135,6 +137,7 @@ static constexpr const char TRUECOLOR_DESC[] = "24-bit (truecolor)
static constexpr const char FORCE_COLOR_DESC[] = "Force color even when stdout is not a tty"; static constexpr const char FORCE_COLOR_DESC[] = "Force color even when stdout is not a tty";
static constexpr const char DISABLE_COLOR_DESC_BASE[] = "Suppress color even when stdout is a tty, overrides -"; static constexpr const char DISABLE_COLOR_DESC_BASE[] = "Suppress color even when stdout is a tty, overrides -";
static constexpr const char* DISABLE_COLOR_DESC = concat_many_strs<DISABLE_COLOR_DESC_BASE,FORCE_COLOR_SHORT_OPTION>::str; static constexpr const char* DISABLE_COLOR_DESC = concat_many_strs<DISABLE_COLOR_DESC_BASE,FORCE_COLOR_SHORT_OPTION>::str;
static constexpr const char INVERT_DESC[] = "Change background color instead of foreground";
static constexpr const char* IGNORE_ANIMATE_DESC = IGNORED_DESC; static constexpr const char* IGNORE_ANIMATE_DESC = IGNORED_DESC;
static constexpr const char* IGNORE_DURATION_DESC = IGNORED_DESC; static constexpr const char* IGNORE_DURATION_DESC = IGNORED_DESC;
@ -274,6 +277,9 @@ cmd_args process_cmd_args(int argc, char** argv){
if(ret.color == COLOR_DEFAULT) if(ret.color == COLOR_DEFAULT)
ret.color = COLOR_FORCE; ret.color = COLOR_FORCE;
break; break;
case SHORT_OPT(INVERT):
ret.invert = 1;
break;
case SHORT_OPT(SEED): case SHORT_OPT(SEED):
CHECK_VALID_SHORT_INT_ARG(SEED, argv[next_arg]); CHECK_VALID_SHORT_INT_ARG(SEED, argv[next_arg]);
ret.seed = atol(argv[next_arg]); ret.seed = atol(argv[next_arg]);
@ -329,6 +335,8 @@ cmd_args process_cmd_args(int argc, char** argv){
}else if(CHECK_LONG_OPTION(FORCE_COLOR, arg)){ }else if(CHECK_LONG_OPTION(FORCE_COLOR, arg)){
if(ret.color == COLOR_DEFAULT) if(ret.color == COLOR_DEFAULT)
ret.color = COLOR_FORCE; ret.color = COLOR_FORCE;
}else if(CHECK_LONG_OPTION(INVERT, arg)){
ret.invert = 1;
}else if(CHECK_LONG_OPTION_WITH_ARG(SEED, arg)){ }else if(CHECK_LONG_OPTION_WITH_ARG(SEED, arg)){
constexpr size_t offset = strlen_pre_eq(SEED_LONG_OPTION); constexpr size_t offset = strlen_pre_eq(SEED_LONG_OPTION);
CHECK_VALID_LONG_INT_ARG(SEED, offset, arg); CHECK_VALID_LONG_INT_ARG(SEED, offset, arg);
@ -381,6 +389,7 @@ const cmd_options cmd_arguments_list[] = {
OPTION(IGNORE_ANIMATE), OPTION(IGNORE_ANIMATE),
OPTION(IGNORE_DURATION), OPTION(IGNORE_DURATION),
OPTION(IGNORE_SPEED), OPTION(IGNORE_SPEED),
OPTION(INVERT),
{nullptr,0,nullptr}, {nullptr,0,nullptr},
OPTION(USAGE), OPTION(USAGE),
OPTION(VERSION) OPTION(VERSION)

34
tmp Normal file
View File

@ -0,0 +1,34 @@
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸
😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸