From f874e3792d82adb2ec16bef39ca3b4a6f86db6e1 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sun, 29 Sep 2024 15:36:41 +0200 Subject: [PATCH] parse: add more color option modes also fix compilation with GUI_MODE=1 --- cufetch.1 | 37 ++++++++++++++----------------------- include/config.hpp | 12 ++++++++---- src/display.cpp | 1 - src/parse.cpp | 42 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 58 insertions(+), 34 deletions(-) diff --git a/cufetch.1 b/cufetch.1 index 6eb9744..bfbcc03 100644 --- a/cufetch.1 +++ b/cufetch.1 @@ -13,54 +13,45 @@ How does it work? We use the \fBconfig.toml\fR file, in there we got an array variable called "layout". That's the variable where you customize how the infos should be displayed. .PP You have 5 tags: -.br +.nf * \fB$\fR - Used for printing the value of a member of a module. -.br * \fB${color}\fR - Used for displaying text in a specific color. -.br * \fB$(bash command)\fR - Used to execute bash commands and print the output. -.br * \fB$[something,equalToSomething,ifTrue,ifFalse]\fR - Conditional tag to display different outputs based on the comparison. -.br +.fi * \fB$%n1,n2%\fR - Used to print the percentage and print with colors .PP They can be used in the ascii art text file and layout, but how to use them? .br Here's a simple bare-minimal example: -.br +.nf layout = [ -.br "My OS is $, and username is $", -.br "The color of the following text will be ${red}red", -.br "This is a $(echo \\"bash command\\")", -.br "Check if \\\\$ is correct: $[$,toni,indeed,it's not toni but $(echo $USER)]", -.br "The discount between 50$ and 100$ is $%50,100%" -.br ] +.fi .PP * The \fBInfo tag $<>\fR will prints the value of a member of a module. .br All the modules and their members are listed in the `--list-modules` argument .PP * The \fBColor tag ${}\fR is used for printing the text of a certain color. -.br +.nf The colors can be predefined such as: \fIblack\fR, \fIred\fR, \fIgreen\fR, \fIblue\fR, \fIcyan\fR, \fIyellow\fR, \fImagenta\fR, \fIwhite\fR and they can be configured in the config file. -.br They can have hexcodes colors (e.g "#5522dd"). -.br You can apply special effects to colors by using the following symbols before the '#' in hex codes: -.br - * \fBb\fR for background color. -.br - * \fBu\fR for underlining the text. -.br - * \fB!\fR for bold text. -.br - * \fBi\fR for italic text. + \fBTerminal and GUI\fR \fBGUI Only\fR + * \fBb\fR for background color. * \fBo\fR for overline + * \fBu\fR to underline the text * \fBa(value)\fR for fg alpha (either a plain integer between 1 and 65536 or a percentage value like `50%`) + * \fB!\fR for bold text * \fBL(value)\fR to underline with a style (`none`, `single`, `double`, `low`, `error`) + * \fBi\fR for italic text * \fBU(value)\fR for choosing the underline color (hexcode without #) + * \fBB(value)\fR for bg color text (hexcode without #) + \fBTerminal Only\fR + * \fBl\fR for blinking text +.fi .PP Alternatively, ANSI escape codes can be used, e.g "\\e[1;31m" and "\\e[38;5;160m", but \fBnote\fR that 256-color ANSI escape codes (those that starts with \\[38 or \\[48) cannot be used in GUI mode. .PP diff --git a/include/config.hpp b/include/config.hpp index 43859f7..79071e0 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -123,10 +123,14 @@ inline constexpr std::string_view AUTOCONFIG = R"#([config] # # They can have hexcodes colors (e.g "#5522dd"). # You can apply special effects to colors by using the following symbols before the '#' in hex codes: -# * b for background color. -# * u for underline the text -# * ! for bold text -# * i for italic text +# Terminal and GUI GUI Only +# * b for background color. * o for overline +# * u to underline the text * a(value) for fg alpha (either a plain integer between 1 and 65536 or a percentage value like `50%`) +# * ! for bold text * L(value) to underline the text with a style (`none`, `single`, `double`, `low`, `error`) +# * i for italic text * U(value) for choosing the underline color (hexcode without #) +# * B(value) for bg color text (hexcode without #) +# Terminal Only +# * l for blinking text # # Alternatively, ANSI escape codes can be used, e.g ${\e[1;32m} or ${\e[0;34m}. # NOTE: 256-color ANSI escape codes (those that starts with \\[38 or \\[48) cannot be used in GUI mode. diff --git a/src/display.cpp b/src/display.cpp index 8be42e6..f3722db 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -2,7 +2,6 @@ #include "display.hpp" -#define STB_IMAGE_IMPLEMENTATION #include #include #include diff --git a/src/parse.cpp b/src/parse.cpp index 0e864a2..a14bc9f 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -242,7 +242,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s dollarSignIndex--; } - std::string command; + std::string command; // what's inside the tag size_t endBracketIndex = -1; char type = ' '; // ' ' = undefined, ')' = shell exec, 2 = ')' asking for a module @@ -470,8 +470,35 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s if (opt_clr.find('i') != std::string::npos) tagfmt += "style='italic' "; - tagfmt.pop_back(); + if (opt_clr.find('o') != std::string::npos) + tagfmt += "overline='single' "; + + size_t argmode_pos = 0; + const auto& append_argmode = [&](const std::string_view fmt, const std::string_view error) + { + if (argmode_pos != std::string::npos && opt_clr.at(argmode_pos + 1) == '(') + { + const size_t closebrak = opt_clr.find(')', argmode_pos); + if (closebrak == std::string::npos) + die("{} mode in color {} doesn't have close bracket", error, str_clr); + + tagfmt += fmt.data() + opt_clr.substr(argmode_pos + 2, closebrak - argmode_pos - 2) + "' "; + } + }; + + argmode_pos = opt_clr.find('a'); + append_argmode("fgalpha='", "fgalpha"); + + argmode_pos = opt_clr.find('L'); + append_argmode("underline='", "underline option"); + argmode_pos = opt_clr.find('U'); + append_argmode("underline_color='#", "colored underline"); + + argmode_pos = opt_clr.find('B'); + append_argmode("bgcolor='#", "bgcolor"); + + tagfmt.pop_back(); output.replace(dollarSignIndex, output.length() - dollarSignIndex, fmt::format("<{}>{}", tagfmt, output.substr(endBracketIndex + 1))); } @@ -537,6 +564,9 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s if (opt_clr.find('i') != std::string::npos) append_styles(style, fmt::emphasis::italic); + if (opt_clr.find('l') != std::string::npos) + append_styles(style, fmt::emphasis::blink); + formatted_replacement_string = fmt::format(style, "{}", output.substr(endBracketIndex + 1)); } @@ -809,11 +839,11 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co // clang-format off case "colors"_fnv1a16: - SYSINFO_INSERT(parse("${\033[40m} ${\033[41m} ${\033[42m} ${\033[43m} ${\033[44m} ${\033[45m} ${\033[46m} ${\033[47m} \033[0m", sysInfo, _, config, colors, parsingLayout)); + SYSINFO_INSERT(parse("${\033[40m} ${\033[41m} ${\033[42m} ${\033[43m} ${\033[44m} ${\033[45m} ${\033[46m} ${\033[47m} ${0}", sysInfo, _, config, colors, parsingLayout)); break; case "colors_light"_fnv1a16: - SYSINFO_INSERT(parse("${\033[100m} ${\033[101m} ${\033[102m} ${\033[103m} ${\033[104m} ${\033[105m} ${\033[106m} ${\033[107m} \033[0m", sysInfo, _, config, colors, parsingLayout)); + SYSINFO_INSERT(parse("${\033[100m} ${\033[101m} ${\033[102m} ${\033[103m} ${\033[104m} ${\033[105m} ${\033[106m} ${\033[107m} ${0}", sysInfo, _, config, colors, parsingLayout)); break; default: @@ -832,7 +862,7 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co debug("symbol = {}", symbol); SYSINFO_INSERT( - parse(fmt::format("${{\033[30m}} {0} ${{\033[31m}} {0} ${{\033[32m}} {0} ${{\033[33m}} {0} ${{\033[34m}} {0} ${{\033[35m}} {0} ${{\033[36m}} {0} ${{\033[37m}} {0} \033[0m", + parse(fmt::format("${{\033[30m}} {0} ${{\033[31m}} {0} ${{\033[32m}} {0} ${{\033[33m}} {0} ${{\033[34m}} {0} ${{\033[35m}} {0} ${{\033[36m}} {0} ${{\033[37m}} {0} ${{0}}", symbol), sysInfo, _, config, colors, parsingLayout)); } else if (hasStart(moduleMemberName, "colors_light_symbol")) @@ -849,7 +879,7 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co debug("symbol = {}", symbol); SYSINFO_INSERT( - parse(fmt::format("${{\033[90m}} {0} ${{\033[91m}} {0} ${{\033[92m}} {0} ${{\033[93m}} {0} ${{\033[94m}} {0} ${{\033[95m}} {0} ${{\033[96m}} {0} ${{\033[97m}} {0} \033[0m", + parse(fmt::format("${{\033[90m}} {0} ${{\033[91m}} {0} ${{\033[92m}} {0} ${{\033[93m}} {0} ${{\033[94m}} {0} ${{\033[95m}} {0} ${{\033[96m}} {0} ${{\033[97m}} {0} ${{0}}", symbol), sysInfo, _, config, colors, parsingLayout)); } }