Skip to content

Commit 537f3de

Browse files
committed
query: auto: add new auto module name + change battery.capacity to battery.perc
finally fastfetch added auto detections like any other sysfetchs worth of note
1 parent d43dd9d commit 537f3de

File tree

13 files changed

+274
-81
lines changed

13 files changed

+274
-81
lines changed

include/config.hpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ class Config
8787

8888
// Variables of config file for
8989
// modules specific configs
90-
// [uptime]
90+
// [auto]
91+
std::string auto_disks_fmt;
92+
93+
// [os.uptime]
9194
std::string uptime_d_fmt;
9295
std::string uptime_h_fmt;
9396
std::string uptime_m_fmt;
9497
std::string uptime_s_fmt;
9598

96-
// [pkgs]
99+
// [os.pkgs]
97100
std::vector<std::string> pkgs_managers;
98101
std::vector<std::string> pacman_dirs;
99102
std::vector<std::string> flatpak_dirs;
@@ -141,11 +144,11 @@ class Config
141144
* @param fallback Default value if couldn't retrive value
142145
*/
143146
template <typename T>
144-
T getValue(const std::string_view value, const T&& fallback) const
147+
T getValue(const std::string_view value, const T&& fallback, bool dont_expand_var = false) const
145148
{
146149
std::optional<T> ret = this->tbl.at_path(value).value<T>();
147150
if constexpr (toml::is_string<T>) // if we want to get a value that's a string
148-
return ret ? expandVar(ret.value()) : expandVar(fallback);
151+
return ret ? expandVar(ret.value(), dont_expand_var) : expandVar(fallback, dont_expand_var);
149152
else
150153
return ret.value_or(fallback);
151154
}
@@ -267,14 +270,10 @@ layout = [
267270
"${auto}Font: $<theme-gtk-all.font>",
268271
"${auto}Cursor: $<theme.cursor>",
269272
"${auto}WM: $<user.wm_name>",
270-
"${auto}DE: $<user.de_name>",
271-
"${auto}Disk (/): $<disk(/)>",)#"
272-
#else
273-
R"#(
274-
"${auto}Disk (/): $<disk(/)>",
275-
"${auto}Disk (/sdcard): $<disk(/storage/emulated/0)>",)#"
273+
"${auto}DE: $<user.de_name>",)#"
276274
#endif
277275
R"#(
276+
"$<auto.disk>",
278277
"${auto}Swap: $<swap>",
279278
"${auto}CPU: $<cpu>",
280279
"${auto}GPU: $<gpu>",
@@ -380,6 +379,19 @@ wrap-lines = false
380379
# e.g. falling back to gsettings when we can't find the config file for GTK
381380
slow-query-warnings = false
382381
382+
# $<auto> config
383+
[auto]
384+
# Format for displaying the auto detected disks infos
385+
# %1 = mount directory
386+
# %2 = device path
387+
# %3 = type of filesystem
388+
# %4 = total amount of storage
389+
# %5 = free amount of storage
390+
# %6 = used amount of storage
391+
# %7 = percentage of used storage
392+
# %8 = percentage of free storage
393+
disk-fmt = "${auto}Disk (%1): $<disk(%1)>"
394+
383395
# $<os.uptime> config
384396
[os.uptime]
385397
# how to display the name of the uptime

include/parse.hpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,55 @@
2727
#define _PARSE_HPP
2828

2929
#include <string>
30+
#include <unordered_map>
31+
#include <variant>
32+
#include <vector>
3033

3134
#include "config.hpp"
32-
#include "query.hpp"
35+
36+
// from query.hpp
37+
using systemInfo_t =
38+
std::unordered_map<std::string, std::unordered_map<std::string, std::variant<std::string, size_t, double>>>;
3339

3440
/* The additional args that parse() needs for getting the necessary infos/configs.
3541
* Only used for making the argument passing more clear.
3642
* Always pass it non-const and by reference
3743
*/
3844
struct parse_args_t
3945
{
40-
systemInfo_t& systemInfo;
41-
std::string& pureOutput;
42-
const Config& config;
43-
const colors_t& colors;
44-
bool parsingLayout;
45-
bool firstrun_clr = true;
46-
bool no_more_reset = false;
47-
std::string endspan = ""; // only for ANDROID_APP
46+
systemInfo_t& systemInfo;
47+
std::string& pureOutput;
48+
std::vector<std::string>& layout;
49+
std::vector<std::string>& tmp_layout;
50+
const Config& config;
51+
const colors_t& colors;
52+
bool parsingLayout;
53+
bool firstrun_clr = true;
54+
bool no_more_reset = false;
55+
std::string endspan = ""; // only for ANDROID_APP
4856
};
4957

5058
/* Parse input, in-place, with data from systemInfo.
5159
* Documentation on formatting is in the default config.toml file or the customfetch.1 manual.
5260
* @param input The string to parse
5361
* @param systemInfo The system infos
5462
* @param pureOutput The output of the string but without tags
63+
* @param layout The layout of customfetch
64+
* @param tmp_layout The temponary layout to be used for $<auto> modules
5565
* @param config The config
5666
* @param colors The colors
5767
* @param parsingLayout If we are parsing layout or not
5868
* @param no_more_reset If we are recursively parsing, e.g we are inside tags
5969
*/
60-
std::string parse(std::string input, systemInfo_t& systemInfo, std::string& pureOutput, const Config& config,
61-
const colors_t& colors, const bool parsingLayout, bool& no_more_reset);
70+
std::string parse(std::string input, systemInfo_t& systemInfo, std::string& pureOutput,
71+
std::vector<std::string>& layout, std::vector<std::string>& tmp_layout,
72+
const Config& config, const colors_t& colors, const bool parsingLayout, bool& no_more_reset);
6273

6374
// parse() for parse_args_t& arguments
64-
std::string parse(const std::string_view input, parse_args_t& parse_args);
75+
std::string parse(const std::string& input, parse_args_t& parse_args);
6576
// some times we don't want to use the original pureOutput,
6677
// so we have to create a tmp string just for the sake of the function arguments
67-
std::string parse(const std::string_view input, std::string& _, parse_args_t& parse_args);
78+
std::string parse(const std::string& input, std::string& _, parse_args_t& parse_args);
6879

6980
/* Set module members values to a systemInfo_t map.
7081
* If the name of said module matches any module name, it will be added
@@ -85,14 +96,18 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
8596
void addValueFromModule(const std::string& moduleName, parse_args_t& parse_args);
8697

8798
/*
88-
* Return a module member value
99+
* Return an info module member value
89100
* @param systemInfo The systemInfo_t map
90101
* @param moduleName The module name
91102
* @param moduleMemberName The module member name
92103
*/
93104
std::string getInfoFromName(const systemInfo_t& systemInfo, const std::string_view moduleName,
94105
const std::string_view moduleMemberName);
95106

107+
108+
std::string get_and_color_percentage(const float& n1, const float& n2, parse_args_t& parse_args,
109+
const bool invert = false);
110+
96111
// Function to combine multiple fmt::text_style arguments
97112
template <typename... Styles>
98113
void append_styles(fmt::text_style& current_style, Styles&&... styles)

include/query.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#include <string>
3232
#include <unordered_map>
3333
#include <variant>
34+
#include <vector>
3435

3536
#include "config.hpp"
37+
#include "parse.hpp"
3638
#include "util.hpp"
3739

3840
extern "C" {
@@ -254,7 +256,7 @@ class Battery
254256
std::string technology{ UNKNOWN };
255257
std::string capacity_level{ UNKNOWN };
256258
double temp{ 0 };
257-
double capacity{ 0 };
259+
double perc{ 0 };
258260
};
259261

260262
Battery();
@@ -264,7 +266,7 @@ class Battery
264266
std::string& status() noexcept;
265267
std::string& technology() noexcept;
266268
std::string& capacity_level() noexcept;
267-
double& capacity() noexcept;
269+
double& perc() noexcept;
268270
double& temp() noexcept;
269271

270272
private:
@@ -285,7 +287,8 @@ class Disk
285287
std::string mountdir;
286288
};
287289

288-
Disk(const std::string& path, systemInfo_t& queried_paths);
290+
Disk(const std::string& path, systemInfo_t& queried_paths, parse_args_t& parse_args,
291+
const bool auto_module = false);
289292

290293
double& total_amount() noexcept;
291294
double& free_amount() noexcept;
@@ -294,7 +297,11 @@ class Disk
294297
std::string& device() noexcept;
295298
std::string& mountdir() noexcept;
296299

300+
std::vector<std::string>& disks_formats() noexcept
301+
{ return m_disks_formats; }
302+
297303
private:
304+
std::vector<std::string> m_disks_formats;
298305
static struct statvfs m_statvfs;
299306
static Disk_t m_disk_infos;
300307
};

include/util.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ void ctrl_d_handler(const std::istream& cin);
176176

177177
/* Replace special symbols such as ~ and $ (at the begging) in std::string
178178
* @param str The string
179+
* @param dont Don't do it
179180
* @return The modified string
180181
*/
181-
std::string expandVar(std::string ret);
182+
std::string expandVar(std::string ret, bool dont = false);
182183

183184
/* Executes commands with execvp() and keep the program running without existing
184185
* @param cmd_str The command to execute

src/config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
8888
this->font = getValue<std::string>("gui.font", "Liberation Mono Normal 12");
8989
this->gui_bg_image = getValue<std::string>("gui.bg-image", "disable");
9090

91+
this->auto_disks_fmt = getValue<std::string>("auto.disk-fmt", "${auto}Disk (%1): $<disk(%1)>", true);
92+
9193
this->uptime_d_fmt = getValue<std::string>("os.uptime.days", " days");
9294
this->uptime_h_fmt = getValue<std::string>("os.uptime.hours", " hours");
9395
this->uptime_m_fmt = getValue<std::string>("os.uptime.mins", " mins");

src/display.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
// Implementation of the system behind displaying/rendering the information
2727

2828
#include "display.hpp"
29+
#include <cstddef>
2930

3031
#ifndef GUI_MODE
3132
# define STB_IMAGE_IMPLEMENTATION
@@ -97,13 +98,23 @@ static std::vector<std::string> render_with_image(systemInfo_t& systemInfo, std:
9798
stbi_image_free(img);
9899

99100
std::string _;
100-
parse_args_t parse_args{ systemInfo, _, config, colors, true };
101-
for (std::string& line : layout)
101+
std::vector<std::string> tmp_layout;
102+
parse_args_t parse_args{ systemInfo, _, layout, tmp_layout, config, colors, true };
103+
for (size_t i = 0; i < layout.size(); ++i)
102104
{
103-
line = parse(line, parse_args);
104-
if (!config.m_disable_colors)
105-
line.insert(0, NOCOLOR);
105+
layout[i] = parse(layout[i], parse_args);
106106
parse_args.no_more_reset = false;
107+
if (!config.gui && !config.m_disable_colors)
108+
{
109+
layout[i].insert(0, NOCOLOR);
110+
}
111+
112+
if (!tmp_layout.empty())
113+
{
114+
layout.erase(layout.begin()+i);
115+
layout.insert(layout.begin()+i, tmp_layout.begin(), tmp_layout.end());
116+
tmp_layout.clear();
117+
}
107118
}
108119

109120
// erase each element for each instance of MAGIC_LINE
@@ -116,9 +127,9 @@ static std::vector<std::string> render_with_image(systemInfo_t& systemInfo, std:
116127
const size_t height = image_height / font_height;
117128

118129
if (config.m_image_backend == "kitty")
119-
taur_exec({ "kitty", "+kitten", "icat", "--align",
120-
(config.logo_position == "top" ? "center" : config.logo_position), "--place",
121-
fmt::format("{}x{}@0x0", width, height), path });
130+
taur_exec({ "kitty", "+kitten", "icat",
131+
"--align", (config.logo_position == "top" ? "center" : config.logo_position),
132+
"--place", fmt::format("{}x{}@0x0", width, height), path });
122133
else if (config.m_image_backend == "viu")
123134
taur_exec({ "viu", "-t", "-w", fmt::to_string(width), "-h", fmt::to_string(height), path });
124135
else
@@ -243,7 +254,8 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
243254
// this is just for parse() to auto add the distro colors
244255
std::ifstream distro_file(distro_path);
245256
std::string line, _;
246-
parse_args_t parse_args{ systemInfo, _, config, colors, false };
257+
std::vector<std::string> tmp_layout;
258+
parse_args_t parse_args{ systemInfo, _, layout, tmp_layout, config, colors, false };
247259

248260
while (std::getline(distro_file, line))
249261
{
@@ -296,7 +308,8 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
296308
while (std::getline(file, line))
297309
{
298310
std::string pureOutput;
299-
parse_args_t parse_args{ systemInfo, pureOutput, config, colors, false };
311+
std::vector<std::string> tmp_layout;
312+
parse_args_t parse_args{ systemInfo, pureOutput, layout, tmp_layout, config, colors, false };
300313

301314
std::string asciiArt_s = parse(line, parse_args);
302315
parse_args.no_more_reset = false;
@@ -325,13 +338,23 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
325338
return asciiArt;
326339

327340
std::string _;
328-
parse_args_t parse_args{ systemInfo, _, config, colors, true };
329-
for (std::string& line : layout)
341+
std::vector<std::string> tmp_layout;
342+
parse_args_t parse_args{ systemInfo, _, layout, tmp_layout, config, colors, true };
343+
for (size_t i = 0; i < layout.size(); ++i)
330344
{
331-
line = parse(line, parse_args);
345+
layout[i] = parse(layout[i], parse_args);
332346
parse_args.no_more_reset = false;
333347
if (!config.gui && !config.m_disable_colors)
334-
line.insert(0, NOCOLOR);
348+
{
349+
layout[i].insert(0, NOCOLOR);
350+
}
351+
352+
if (!tmp_layout.empty())
353+
{
354+
layout.erase(layout.begin()+i);
355+
layout.insert(layout.begin()+i, tmp_layout.begin(), tmp_layout.end());
356+
tmp_layout.clear();
357+
}
335358
}
336359

337360
auto_colors.clear();

src/gui.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,30 @@ static std::vector<std::string> render_with_image(const Config& config, const co
8787
// this is just for parse() to auto add the distro colors
8888
std::ifstream file(path, std::ios::binary);
8989
std::string line, _;
90-
parse_args_t parse_args{ systemInfo, _, config, colors, false, true };
91-
90+
std::vector<std::string> tmp_layout;
91+
parse_args_t parse_args{ systemInfo, _, layout, tmp_layout, config, colors, false };
9292
while (std::getline(file, line))
9393
{
9494
parse(line, parse_args);
9595
parse_args.no_more_reset = false;
9696
}
9797

9898
parse_args.parsingLayout = true;
99-
for (std::string& line : layout)
99+
for (size_t i = 0; i < layout.size(); ++i)
100100
{
101-
line = parse(line, parse_args);
101+
layout[i] = parse(layout[i], parse_args);
102102
parse_args.no_more_reset = false;
103+
if (!config.gui && !config.m_disable_colors)
104+
{
105+
layout[i].insert(0, NOCOLOR);
106+
}
107+
108+
if (!tmp_layout.empty())
109+
{
110+
layout.erase(layout.begin()+i);
111+
layout.insert(layout.begin()+i, tmp_layout.begin(), tmp_layout.end());
112+
tmp_layout.clear();
113+
}
103114
}
104115

105116
// erase each element for each instance of MAGIC_LINE

src/main.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ disk(/path/to/fs):
181181
CPU model name with number of virtual processors and max freq [AMD Ryzen 5 5500 (12) @ 4.90 GHz]
182182
183183
battery:
184-
battery current capacity and status [50.00% [Discharging]]
184+
battery current percentage and status [50.00% [Discharging]]
185185
186186
title:
187187
user and hostname colored with ${{auto2}} [toni@arch2]
@@ -344,7 +344,7 @@ cpu
344344
battery
345345
name: battery model name
346346
temp: battery temperature (by the chosen unit)
347-
capacity: battery current capacity
347+
perc: battery current percentage
348348
vendor: battery manufacturer name
349349
status: battery current status [Discharging, AC Connected]
350350
technology: battery technology [Li-lion]
@@ -365,10 +365,7 @@ system
365365

366366
static bool str_to_bool(const std::string_view str)
367367
{
368-
if (str == "true" || str == "1" || str == "enable")
369-
return true;
370-
371-
return false;
368+
return (str == "true" || str == "1" || str == "enable");
372369
}
373370

374371
// clang-format off

0 commit comments

Comments
 (0)