Skip to content

Commit

Permalink
parse: add feature to add multiple recursive tags inside another one
Browse files Browse the repository at this point in the history
example: $<disk($(echo /)).disk>

we getting out of the hood with this one

also modify some descriptions
  • Loading branch information
Toni500github committed Oct 4, 2024
1 parent 9ad21de commit 318bf8a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ flatpak-dirs = ["/var/lib/flatpak/app/", "~/.local/share/flatpak/app/"]
apk-files = ["/var/lib/apk/db/installed"]
# GUI options
# note: customfetch needs to be compiled with GUI_MODE=1 (check with "cufetch --version")
# note: customfetch needs to be compiled with GUI_MODE=1 (check with "cufetch --version" if GUI mode was enabled)
[gui]
enable = false
Expand Down
7 changes: 2 additions & 5 deletions include/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
/* Set module members values to a systemInfo_t map.
* If the name of said module matches any module name, it will be added
* else, error out.
* @param sysInfo The systemInfo_t map
* @param moduleName The module name
* @param moduleMemberName The module member name
* @param config The config
* @param colors The colors
* @param parsingLayout If we are parsing the layout or not (default true)
* @param parse_args The parse() like arguments
*/
void addValueFromModule(const std::string& moduleName, const std::string& moduleMemberName,
parse_args_t& parse_args);
Expand All @@ -53,7 +50,7 @@ std::string getInfoFromName(const systemInfo_t& systemInfo, const std::string_vi
template <typename... Styles>
void append_styles(fmt::text_style& current_style, Styles&&... styles)
{
current_style = current_style | (styles | ...);
current_style |= (styles | ...);
}


Expand Down
10 changes: 4 additions & 6 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ std::string Display::detect_distro(const Config& config)
}
}

static std::vector<std::string> render_with_image(systemInfo_t& systemInfo, const Config& config, const colors_t& colors,
const std::string_view path, const std::uint16_t font_width,
const std::uint16_t font_height)
static std::vector<std::string> render_with_image(systemInfo_t& systemInfo, std::vector<std::string>& layout,
const Config& config, const colors_t& colors, const std::string_view path,
const std::uint16_t font_width, const std::uint16_t font_height)
{
std::vector<std::string> layout{ config.layout };

int image_width, image_height, channels;

// load the image and get its width and height
Expand Down Expand Up @@ -211,7 +209,7 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
get_pos(y, x);
fmt::print("\033[{};{}H", y, x);

return render_with_image(systemInfo, config, colors, path, font_width, font_height);
return render_with_image(systemInfo, layout, config, colors, path, font_width, font_height);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ theme-gtk-all
# example: if you want to show your 512MiB of used RAM in GiB
# use the `used-GiB` variant (they don't print the unit tho)
ram
ram : used and total amount of RAM (auto) [2.81 GiB / 15.88 GiB]
ram : used and total amount of RAM (auto) with used percentage [2.81 GiB / 15.88 GiB (5.34%)]
used : used amount of RAM (auto) [2.81 GiB]
free : available amount of RAM (auto) [10.46 GiB]
total : total amount of RAM (auto) [15.88 GiB]
Expand All @@ -182,7 +182,7 @@ ram
# same comments as RAM (above)
swap
swap : used and total amount of the swapfile (auto) [477.68 MiB / 512.00 MiB]
swap : used and total amount of the swapfile (auto) with used percentage [477.68 MiB / 512.00 MiB (88.45%)]
free : available amount of the swapfile (auto) [34.32 MiB]
total : total amount of the swapfile (auto) [512.00 MiB]
used : used amount of the swapfile (auto) [477.68 MiB]
Expand All @@ -194,7 +194,7 @@ swap
# or a filesystem path
# e.g disk(/) or disk(/dev/sda5)
disk(/path/to/fs)
disk : used and total amount of disk space (auto) with type of filesystem [360.02 GiB / 438.08 GiB - ext4]
disk : used and total amount of disk space (auto) with type of filesystem and used percentage [379.83 GiB / 438.08 GiB (86.70%) - ext4]
used : used amount of disk space (auto) [360.02 GiB]
free : available amount of disk space (auto) [438.08 GiB]
total : total amount of disk space (auto) [100.08 GiB]
Expand Down
64 changes: 46 additions & 18 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ static std::string parse(const std::string_view input, std::string& _, parse_arg
return parse(input, parse_args.systemInfo, _, parse_args.config, parse_args.colors, parse_args.parsingLayout);
}

static char gettype(const char opentag)
{
switch (opentag)
{
case '(': return ')';
case '<': return '>';
case '%': return '%';
case '[': return ']';
case '{': return '}';
}

return ' '; // neither of them
}

static std::string get_and_color_percentage(const float& n1, const float& n2, parse_args_t& parse_args,
const bool invert = false)
{
Expand Down Expand Up @@ -236,7 +250,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
retry:
if (dollarSignIndex == std::string::npos)
break;

// small workaround cuz idk how to fix this
else if (dollarSignIndex <= oldDollarSignIndex && start && !config.m_disable_colors)
{
dollarSignIndex = output.find('$', dollarSignIndex + 1);
Expand All @@ -254,7 +268,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
if (skip_bypass)
{
if (dollarSignIndex > 0 and
(output[dollarSignIndex - 1] == '\\' and (dollarSignIndex == 1 or output[dollarSignIndex - 2] != '\\')))
(output.at(dollarSignIndex - 1) == '\\' and (dollarSignIndex == 1 or output.at(dollarSignIndex - 2) != '\\')))
{
skip_bypass = false;
continue;
Expand All @@ -279,27 +293,41 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
command.reserve(256); // should be enough for not allocating over and over

size_t endBracketIndex = -1;
char type = ' '; // ' ' = undefined, ')' = shell exec, 2 = ')' asking for a module
const char opentag = output.at(dollarSignIndex + 1);

switch (opentag)
{
case '(': type = ')'; break;
case '<': type = '>'; break;
case '%': type = '%'; break;
case '[': type = ']'; break;
case '{': type = '}'; break;
default: // neither of them
break;
}


const char type = gettype(opentag); // ' ' = undefined, ')' = shell exec, 2 = ')' asking for a module
if (type == ' ')
continue;

// if we get a tag inside a tag, then let's skip until we skipped the subtag
// entirely
size_t skip_lenght = 0;

// let's get what's inside the brackets
for (size_t i = dollarSignIndex + 2; i < output.size(); i++)
{
if (output.at(i) == type && output.at(i - 1) != '\\')
if (skip_lenght > 0)
{
skip_lenght--;
continue;
}

if (output.at(i) == '$')
{
const char type = gettype(output.at(i + 1));
if (type != ' ')
{
const size_t pos = output.find(type, i);
if (pos == output.npos)
die("PARSER: Opened tag is not closed at index {} in string {}", dollarSignIndex, output);

const size_t len = (pos + 1) - i;
command += parse(output.substr(i, len), _, parse_args);
skip_lenght = len - 1;
continue;
}
}
else if (output.at(i) == type && output.at(i - 1) != '\\')
{
endBracketIndex = i;
break;
Expand Down Expand Up @@ -1171,8 +1199,8 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
{
switch (moduleMember_hash)
{
case "name"_fnv1a16: SYSINFO_INSERT(query_gpu.name()); break;
case "vendor"_fnv1a16: SYSINFO_INSERT(shorten_vendor_name(query_gpu.vendor())); break;
case "name"_fnv1a16: SYSINFO_INSERT(query_gpu.name()); break;
case "vendor"_fnv1a16: SYSINFO_INSERT(shorten_vendor_name(query_gpu.vendor())); break;
case "vendor_long"_fnv1a16: SYSINFO_INSERT(query_gpu.vendor()); break;
}
}
Expand Down

0 comments on commit 318bf8a

Please sign in to comment.