Skip to content

Commit

Permalink
parse: add more color option modes
Browse files Browse the repository at this point in the history
also fix compilation with GUI_MODE=1
  • Loading branch information
Toni500github committed Sep 29, 2024
1 parent a120a87 commit f874e37
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
37 changes: 14 additions & 23 deletions cufetch.1
Original file line number Diff line number Diff line change
Expand Up @@ -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$<module.member>\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 $<os.name>, and username is $<user.name>",
.br
"The color of the following text will be ${red}red",
.br
"This is a $(echo \\"bash command\\")",
.br
"Check if \\\\$<user.name> is correct: $[$<user.name>,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
Expand Down
12 changes: 8 additions & 4 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "display.hpp"

#define STB_IMAGE_IMPLEMENTATION
#include <pty.h>
#include <termios.h>
#include <unistd.h>
Expand Down
42 changes: 36 additions & 6 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("<{}>{}</span>", tagfmt, output.substr(endBracketIndex + 1)));
}
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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:
Expand All @@ -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"))
Expand All @@ -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));
}
}
Expand Down

0 comments on commit f874e37

Please sign in to comment.