Skip to content

Commit

Permalink
parse: add conditional component $[]
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 3, 2024
1 parent 92babc4 commit 36de78b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ bg-image = "/tmp/idk.png"
```

We got the config.toml file, in there we got an array variable called "layout". That's the variable where you customize how the infos should be displayed.\
You have 3 components: `$<module.submod>`, `${color}`, `$(bash command)`. They can be used in the ascii art text file and layout, but how to use them?
You have 4 components: `$<module.submod>`, `${color}`, `$(bash command)`, `$[something,equalToSomethingElse,iftrue,ifalse]`. They can be used in the ascii art text file and layout, but how to use them?

* **The info component (`$<>`)** will print a member of a module\
e.g `$<user.name>` will print the username, `$<os.kernel_version>` will print the kernel version and so on.\
Expand All @@ -246,6 +246,12 @@ You have 3 components: `$<module.submod>`, `${color}`, `$(bash command)`. They c
you can even use pipes\
e.g `$(echo \"hello world\" | cut -d' ' -f2)` will only print world

* **The conditional component (`$[]`)** is used for equal conditional check.\
Syntax MUST be `$[something,equalToSomethingElse,iftrue,ifalse]` with no spaces between commas ','\
Each part can have a component or anything else.\
e.g `$[$<user.name>,$(echo $USER),the name is correct,the name is NOT correct]`\
This is useful when on some terminal or WM the detection can be different than others\

* **The color component (`${}`)** is used for which color to use for colorizing the text\
e.g `${red}hello world` will indeed print "hello world" in red (or the color you set in the variable).\
The colors can be: <ins>black</ins>, <ins>red</ins>, <ins>green</ins>, <ins>blue</ins>, <ins>cyan</ins>, <ins>yellow</ins>, <ins>magenta</ins>, <ins>white</ins> and they can be configured in the config file.\
Expand Down
14 changes: 12 additions & 2 deletions cufetch.1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ How does it work?
.PP
We got the config.toml file, in there we got an array variable called "layout". That's the variable where you customize how the infos should be displayed.
.br
You have 3 components: $<module.submod>, ${color}, $(bash command). They can be used in the ascii art text file and layout, but how to use them?
You have 4 components: $<module.submod>, ${color}, $(bash command), $[something,equalToSomething,ifTrue,ifFalse]. They can be used in the ascii art text file and layout, but how to use them?
.PP
Here's a simple bare-minimal example:
.br
Expand Down Expand Up @@ -47,11 +47,21 @@ It's possible to enable multiple options, put these symbols before '#':
OR They can have ANSI escape codes, e.g "\\e[1;31m" and "\\e[38;5;160m" (\fBNOTE\fR: 256 colors ANSI escape codes, those that have \\e[38 or \\e[48, can't be used in GUI)
.br
You can also use them inside the component, like ${!#343345} or ${\\e[1;31m}.
.PP
.br
To reset colors use ${0} or ${1} for reset and bold text.
.br
To instead use the colors that the ascii art logo uses, use ${auto} for getting the 1st color, ${auto4} for the 4th one and so on.
.PP
The \fBConditional component $[]\fR is used for equal conditional check.
.br
syntax \fBMUST\fR be $[something,equalToSomethingElse,ifTrue,ifFalse] with no spaces between commas ','
.br
Each part can have a component or anything else.
.br
e.g $[$<user.name>,$(echo $USER),the name is correct,the name is NOT correct]
.br
This is useful when on some terminal or WM the detection can be different than others
.PP
The \fBBash command component $()\fR speaks for itself. It can execute normal bash commands.
.PP
To escape any $ or bracket, just use \\
Expand Down
8 changes: 7 additions & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ inline constexpr std::string_view AUTOCONFIG = R"#([config]
# here is how it works:
# the variable "layout" is used for showing the infos
# as like as the user want, no limitation.
# inside here there are 3 "components": $<> $() ${}
# inside here there are 4 "components": $<> $() ${} $[]
# $<> lets you access a member of a module
# e.g $<user.name> will print the username, $<os.kernel_version> will print the kernel version and so on.
Expand All @@ -104,6 +104,12 @@ inline constexpr std::string_view AUTOCONFIG = R"#([config]
# you can even use pipes
# e.g $(echo \"hello world\" | cut -d' ' -f2) will only print world
# $[] is used for equal conditional check
# syntax MUST be $[something,equalToSomethingElse,iftrue,ifalse] with no spaces between commas ','
# Each part can have a component or anything else.
# e.g $[$<user.name>,$(echo $USER),the name is correct,the name is NOT correct]
# This is useful when on some terminal or WM the detection can be different than others
# ${} is used for which color to use for colorizing the text
# e.g "${red}hello world" will indeed print "hello world" in red (or the color you set in the variable)
# you can even put a custom hex color e.g: ${#ff6622}
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ A command-line system information tool (or neofetch like program), which its foc
-g, --gui Use GUI mode instead of priting in the terminal (use -V to check if it was enabled)
-o, --offset <num> Offset between the ascii art and the layout
-l. --list-modules Print the list of the components and its members
-l. --list-modules Print the list of the modules and its members
-h, --help Print this help menu
-L, --logo-only Print only the logo
-V, --version Print the version along with the git branch it was built
Expand Down Expand Up @@ -141,7 +141,6 @@ theme-gtkN
name : gtk theme name [Arc-Dark]
icons : gtk icons theme name [Qogir-Dark]
font : gtk font theme name [Noto Sans 10]
cursor : gtk cursor theme name [Qogir-Dark]
# basically as like as the "theme-gtkN" module above
# but with gtk{{2,3,4}} and auto format gkt version
Expand All @@ -150,7 +149,6 @@ theme-gtk-all
name : gtk theme name [Decay-Green [GTK2], Arc-Dark [GTK3/4]]
icons : gtk icons theme name [Papirus-Dark [GTK2/3], Qogir [GTK4]]
font : gtk font theme name [Cantarell 10 [GTK2], Noto Sans, 10 [GTK3], Noto Sans 10 [GTK4]]
cursor : gtk cursor theme name [Bibata-Modern-Ice [GTK2], Qogir-dark [GTK3], Qogir [GTK4]]
# note: these members are auto displayed in KiB, MiB, GiB and TiB.
# they all (except ram.ram and ram.swap) have a -KiB, -GiB and -MiB variant
Expand Down
47 changes: 44 additions & 3 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
{
case '(': type = ')'; break;
case '<': type = '>'; break;
case '[': type = ']'; break;
case '{': type = '}'; break;
default: // neither of them
break;
Expand Down Expand Up @@ -206,12 +207,14 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
case ')':
{
const std::string& shell_cmd = shell_exec(command);
output = output.replace(dollarSignIndex, (endBracketIndex + 1) - dollarSignIndex, shell_cmd);
output = output.replace(dollarSignIndex, (endBracketIndex + 1) - dollarSignIndex,
shell_cmd);

if (!parsingLaoyut && start_pos != std::string::npos)
pureOutput.replace(start_pos, command.length() + 3, shell_cmd);

} break;

case '>':
{
const size_t& dot_pos = command.find('.');
Expand All @@ -228,8 +231,46 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
if (!parsingLaoyut && start_pos != std::string::npos)
pureOutput.replace(start_pos, command.length() + 3,
getInfoFromName(systemInfo, moduleName, moduleValueName));
}
break;
} break;

case ']':
{
const size_t condition_comma = command.find(',');
if (condition_comma == command.npos)
die("condition component {} doesn't have a comma for separiting the condition", command);

const size_t equalto_comma = command.find(',', condition_comma + 1);
if (equalto_comma == command.npos)
die("condition component {} doesn't have a comma for separiting the equalto", command);

const size_t true_comma = command.find(',', equalto_comma + 1);
if (true_comma == command.npos)
die("condition component {} doesn't have a comma for separiting the true statment", command);

std::string _;
const std::string& condition = command.substr(0, condition_comma);
const std::string& equalto = command.substr(condition_comma + 1, equalto_comma - condition_comma - 1);
const std::string& true_statment = command.substr(equalto_comma + 1, true_comma - equalto_comma - 1);
const std::string& false_statment= command.substr(true_comma + 1);

const std::string& parsed_condition = parse(condition, systemInfo, _, config, colors, true);
const std::string& parsed_equalto = parse(equalto, systemInfo, _, config, colors, true);

if (parsed_condition == parsed_equalto)
{
const std::string& parsed_true_stam = parse(true_statment, systemInfo, _, config, colors, true);
output = output.replace(dollarSignIndex, (endBracketIndex + 1) - dollarSignIndex,
parsed_true_stam);
}
else
{
const std::string& parsed_false_stam = parse(false_statment, systemInfo, _, config, colors, true);
output = output.replace(dollarSignIndex, (endBracketIndex + 1) - dollarSignIndex,
parsed_false_stam);
}

} break;

case '}': // please pay very attention when reading this unreadable and godawful code

// if at end there a '$', it will make the end output "$</span>" and so it will confuse addValueFromModule()
Expand Down

0 comments on commit 36de78b

Please sign in to comment.