diff --git a/include/basic_color_printer.tpp b/include/basic_color_printer.tpp index 4b521c0..55ed45b 100644 --- a/include/basic_color_printer.tpp +++ b/include/basic_color_printer.tpp @@ -31,12 +31,16 @@ template constexpr basic_color_printer::basic_color_printer(const cmd_args& args)noexcept: - color_printer_base>(args), + color_printer_base>(args, args.invert ? L"\033[38;5;0m\033[48;5;%sm" : L"\033[38;5;%sm"), m_randomness(rand()%color_lookup_len){} template basic_color_printer& basic_color_printer::_print(const wchar_t s)noexcept{ if(s == L'\n'){ inc_line(); + if(this->m_invert) + this->reset(); + putwchar(L'\n'); + return *this; }else{ int width = wcwidth(s); if(width > 0) @@ -49,7 +53,7 @@ basic_color_printer& basic_color_printer::_print(const wchar_t s)noexcept{ new_index += color_lookup_len; if(new_index != m_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); return *this; diff --git a/include/cmd.hpp b/include/cmd.hpp index cae872f..556fd64 100644 --- a/include/cmd.hpp +++ b/include/cmd.hpp @@ -45,11 +45,12 @@ struct cmd_args{ std::vector filenames; //lolcat options + long seed = 0; float freq = 0.2f; float spread = 0.3f; - long seed = 0; unsigned truecol:1; unsigned color:2; + unsigned invert:1; //GNU cat options unsigned number:2; diff --git a/include/color_printer_base.hpp b/include/color_printer_base.hpp index 7eb3d7e..ffdbfd2 100644 --- a/include/color_printer_base.hpp +++ b/include/color_printer_base.hpp @@ -27,12 +27,15 @@ template class color_printer_base : public printer_base { protected: + const wchar_t* const m_color_str; const float m_freq; const float m_spread; + const bool m_invert; 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(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){ wprintf(L"\033[0m"); return static_cast(*this); diff --git a/include/color_printer_true.hpp b/include/color_printer_true.hpp index 9776f89..fcfd8f4 100644 --- a/include/color_printer_true.hpp +++ b/include/color_printer_true.hpp @@ -96,7 +96,7 @@ private: }m_line_color = {}, m_color = m_line_color; public: constexpr color_printer_true(const cmd_args& args)noexcept: - color_printer_base(args){} + color_printer_base(args, args.invert ? L"\033[38;5;0m\033[48;2;%s;%s;%sm" : L"\033[38;2;%s;%s;%sm"){} protected: color_printer_true& _print(const wchar_t s)noexcept{ @@ -104,6 +104,10 @@ protected: if(s == L'\n'){ inc_line(); updated = true; + if(m_invert) + reset(); + putwchar(L'\n'); + return *this; }else{ int width = wcwidth(s); if(width > 0) @@ -112,10 +116,13 @@ protected: updated = inc_color(1); } 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); return *this; } + color_printer_true& _reset(void){ + return color_printer_base::_reset(); + } constexpr void inc_line(void){ int amount = m_spread*(float{color_max_amount}/4); _do_inc_color(amount, m_line_color); diff --git a/src/cmd.cpp b/src/cmd.cpp index 679b7d7..e6a5357 100644 --- a/src/cmd.cpp +++ b/src/cmd.cpp @@ -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 FORCE_COLOR_LONG_OPTION[] = "force"; 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_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 FREQ_SHORT_OPTION[] = "F"; 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 DISABLE_COLOR_SHORT_OPTION[] = "N"; 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 DISABLE_COLOR_DESC_BASE[] = "Suppress color even when stdout is a tty, overrides -"; static constexpr const char* DISABLE_COLOR_DESC = concat_many_strs::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_DURATION_DESC = IGNORED_DESC; @@ -274,6 +277,9 @@ cmd_args process_cmd_args(int argc, char** argv){ if(ret.color == COLOR_DEFAULT) ret.color = COLOR_FORCE; break; + case SHORT_OPT(INVERT): + ret.invert = 1; + break; case SHORT_OPT(SEED): CHECK_VALID_SHORT_INT_ARG(SEED, 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)){ if(ret.color == COLOR_DEFAULT) ret.color = COLOR_FORCE; + }else if(CHECK_LONG_OPTION(INVERT, arg)){ + ret.invert = 1; }else if(CHECK_LONG_OPTION_WITH_ARG(SEED, arg)){ constexpr size_t offset = strlen_pre_eq(SEED_LONG_OPTION); CHECK_VALID_LONG_INT_ARG(SEED, offset, arg); @@ -381,6 +389,7 @@ const cmd_options cmd_arguments_list[] = { OPTION(IGNORE_ANIMATE), OPTION(IGNORE_DURATION), OPTION(IGNORE_SPEED), + OPTION(INVERT), {nullptr,0,nullptr}, OPTION(USAGE), OPTION(VERSION) diff --git a/tmp b/tmp new file mode 100644 index 0000000..935603f --- /dev/null +++ b/tmp @@ -0,0 +1,34 @@ +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸 +😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸😸