Skip to content

Commit 3dd7898

Browse files
committed
misc: refactor module name and arg parsing + some fixes and stuff
1 parent f1dc7e5 commit 3dd7898

File tree

5 files changed

+171
-78
lines changed

5 files changed

+171
-78
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ all: genver fmt toml tpl getopt-port json libcufetch $(TARGET)
7474

7575
libcufetch: fmt tpl toml
7676
ifeq ($(wildcard $(BUILDDIR)/libcufetch.so),)
77-
$(MAKE) -C libcufetch BUILDDIR=$(BUILDDIR) GUI_APP=$(GUI_APP) CXXSTD=$(CXXSTD)
77+
$(MAKE) -C libcufetch BUILDDIR=$(BUILDDIR) GUI_APP=$(GUI_APP) CXXSTD=$(CXXSTD) DEBUG=$(DEBUG)
7878
endif
7979

8080
fmt:

libcufetch/Makefile

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
CXX ?= g++
2+
CXXSTD ?= c++20
3+
VARS ?= -DENABLE_NLS=1
4+
5+
DEBUG ?= 1
6+
GUI_APP ?= 0
7+
8+
# https://stackoverflow.com/a/1079861
9+
# WAY easier way to build debug and release builds
10+
ifeq ($(DEBUG), 1)
11+
BUILDDIR := build/debug
12+
LTO_FLAGS = -fno-lto
13+
CXXFLAGS := -ggdb3 -Wall -Wextra -pedantic -Wno-unused-parameter \
14+
-DDEBUG=1 -fno-omit-frame-pointer $(DEBUG_CXXFLAGS) $(CXXFLAGS)
15+
LDFLAGS += -fsanitize=address -fno-lto
16+
else
17+
# Check if an optimization flag is not already set
18+
ifneq ($(filter -O%,$(CXXFLAGS)),)
19+
$(info Keeping the existing optimization flag in CXXFLAGS)
20+
else
21+
CXXFLAGS := -O3 $(CXXFLAGS)
22+
endif
23+
LDFLAGS += $(LTO_FLAGS)
24+
BUILDDIR := build/release
25+
endif
26+
27+
128
UNAME_S := $(shell uname -s)
229

330
# is macos?
@@ -13,8 +40,6 @@ else
1340
SONAME_FLAGS := -Wl,--export-dynamic
1441
endif
1542

16-
CXX ?= g++
17-
GUI_APP ?= 0
1843
SRC = $(wildcard *.cc)
1944
OBJ = $(SRC:.cc=.o) ../$(BUILDDIR)/toml.o
2045
LDLIBS := ../$(BUILDDIR)/libfmt.a ../$(BUILDDIR)/libtiny-process-library.a

libcufetch/parse.cc

Lines changed: 124 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
#include <string_view>
3838
#include <vector>
3939

40-
#include "tiny-process-library/process.hpp"
40+
#include "fmt/format.h"
4141
#include "libcufetch/common.hh"
4242
#include "libcufetch/config.hh"
4343
#include "libcufetch/cufetch.hh"
44-
#include "fmt/format.h"
4544
#include "switch_fnv1a.hpp"
45+
#include "tiny-process-library/process.hpp"
4646
#include "util.hpp"
4747

4848
class Parser
@@ -194,10 +194,11 @@ EXPORT std::string parse(const std::string& input, parse_args_t& parse_args)
194194
parse_args.config, parse_args.parsingLayout, parse_args.no_more_reset);
195195
}
196196

197-
std::string get_and_color_percentage(const float n1, const float n2, parse_args_t& parse_args, const bool invert)
197+
EXPORT std::string get_and_color_percentage(const float n1, const float n2, parse_args_t& parse_args, const bool invert)
198198
{
199-
const std::vector<std::string>& percentage_colors = parse_args.config.getValueArrayStr("config.percentage-colors", {"green", "yellow", "red"});
200-
const float result = n1 / n2 * static_cast<float>(100);
199+
const std::vector<std::string>& percentage_colors =
200+
parse_args.config.getValueArrayStr("config.percentage-colors", { "green", "yellow", "red" });
201+
const float result = n1 / n2 * static_cast<float>(100);
201202

202203
std::string color;
203204
if (!invert)
@@ -222,83 +223,136 @@ std::string get_and_color_percentage(const float n1, const float n2, parse_args_
222223
return parse(fmt::format("{}{:.2f}%${{0}}", color, result), _, parse_args);
223224
}
224225

225-
std::string getInfoFromName(parse_args_t& parse_args, const std::string& moduleName)
226-
{
227-
std::string name;
228-
name.reserve(moduleName.size());
229-
230-
/* true when we find a '(' */
231-
bool collecting = false;
232-
233-
/* current position */
234-
size_t i = -1;
235-
size_t stripped_char_count = 0; /* amount of chars stripped from `name` */
226+
static std::string parse(Parser& parser, moduleArgs_t* moduleArg, bool collecting = false, const char until = 0);
236227

237-
/* position of start, resets every separator */
238-
size_t start_pos = 0;
239-
240-
moduleArgs_t* moduleArgs = new moduleArgs_t;
228+
static std::optional<std::string> parse_module(Parser& p)
229+
{
230+
if (!p.try_read('('))
231+
return {};
241232

242-
/* argument that's collected from what's between the parenthesis in "module(...).test" */
233+
// collect everything up to the matching ')', allowing '.' inside
243234
std::string arg;
244-
arg.reserve(moduleName.size());
245-
for (const char c : moduleName)
235+
int depth = 1;
236+
while (!p.is_eof() && depth > 0)
246237
{
247-
i++;
248-
if (c == '(' && !collecting)
238+
char c = p.read_char();
239+
if (c == '\\')
240+
{ // escaped char
241+
if (!p.is_eof())
242+
arg.push_back(p.read_char());
243+
}
244+
else if (c == '(')
249245
{
250-
collecting = true;
251-
continue;
246+
++depth;
247+
arg.push_back('(');
252248
}
253-
254-
if ((c == '.' || i + 1 == moduleName.size()))
249+
else if (c == ')')
255250
{
256-
if (collecting)
257-
{
258-
if (arg.back() != ')' && c != ')')
259-
die("Module name `{}` is invalid. Arguments must end with )", moduleName);
251+
--depth;
252+
if (depth > 0)
253+
arg.push_back(')');
254+
}
255+
else
256+
{
257+
arg.push_back(c);
258+
}
259+
}
260260

261-
if (arg.back() == ')')
262-
arg.pop_back();
261+
if (depth != 0)
262+
{
263+
error(_("PARSER: Missing closing ')' in string '{}'"), p.src);
264+
}
265+
return arg;
266+
}
263267

264-
moduleArgs_t* moduleArg = moduleArgs;
265-
while (moduleArg->next != nullptr)
266-
moduleArg = moduleArg->next;
268+
// Parse module path with optional arguments
269+
static std::string parse(Parser& p, moduleArgs_t* m, bool collecting, char until)
270+
{
271+
std::string token;
267272

268-
moduleArg->name = std::string{ name.begin() + start_pos, name.end() };
269-
moduleArg->value = arg;
270-
moduleArg->next = new moduleArgs_t;
271-
moduleArg->next->prev = moduleArg;
273+
while (until ? !p.try_read(until) : !p.is_eof())
274+
{
275+
if (until && p.is_eof())
276+
{
277+
error(_("PARSER: Missing closing '{}' in string '{}'"), until, p.src);
278+
break;
279+
}
272280

273-
if (c == '.')
274-
{
275-
name.push_back('.');
276-
stripped_char_count++;
277-
}
278-
}
279-
else
281+
if (p.try_read('\\'))
282+
{ // escape
283+
if (!p.is_eof())
284+
token.push_back(p.read_char());
285+
}
286+
else if (auto arg = parse_module(p))
287+
{ // found (...) argument
288+
m->name = token;
289+
m->value = *arg;
290+
m->next = new moduleArgs_t;
291+
m->next->prev = m;
292+
m = m->next;
293+
token.clear();
294+
}
295+
else if (!collecting && p.try_read('.'))
296+
{ // separator
297+
if (!token.empty())
280298
{
281-
name.push_back(c);
299+
m->name = token;
300+
m->value = "";
301+
m->next = new moduleArgs_t;
302+
m->next->prev = m;
303+
m = m->next;
304+
token.clear();
282305
}
283-
284-
start_pos = i + 1 - stripped_char_count;
285-
arg = "";
286-
collecting = false;
287-
288-
continue;
289306
}
290-
291-
if (!collecting)
307+
else
292308
{
293-
name.push_back(c);
309+
token.push_back(p.read_char());
294310
}
295-
else
311+
}
312+
313+
if (!collecting && !token.empty())
314+
{
315+
m->name = token;
316+
m->value = "";
317+
m->next = nullptr; // end of list
318+
}
319+
else if (!collecting)
320+
{
321+
m->next = nullptr;
322+
}
323+
324+
return token;
325+
}
326+
327+
std::string getInfoFromName(parse_args_t& parse_args, const std::string& moduleName)
328+
{
329+
Parser parser(moduleName, _);
330+
moduleArgs_t* moduleArgs = new moduleArgs_t;
331+
parse(parser, moduleArgs);
332+
333+
if (debug_print)
334+
{
335+
debug("moduleName = {}", moduleName);
336+
auto* debug_arg = moduleArgs;
337+
size_t i = 0;
338+
while (debug_arg->next)
296339
{
297-
stripped_char_count++;
298-
arg.push_back(c);
340+
debug("moduleArg[{}]\tname = '{}' && arg = '{}'", i, debug_arg->name, debug_arg->value);
341+
debug_arg = debug_arg->next;
342+
i++;
299343
}
300344
}
301345

346+
std::string name;
347+
moduleArgs_t* moduleArg = moduleArgs;
348+
while (moduleArg->next)
349+
{
350+
name += moduleArg->name + ".";
351+
moduleArg = moduleArg->next;
352+
}
353+
name.pop_back();
354+
debug("name = {}", name);
355+
302356
std::string result = "(unknown/invalid module)";
303357
if (const auto& it = parse_args.modulesInfo.find(name); it != parse_args.modulesInfo.end())
304358
{
@@ -354,8 +408,8 @@ std::optional<std::string> parse_command_tag(Parser& parser, parse_args_t& parse
354408
if (removetag)
355409
command.erase(0, 1);
356410

357-
std::string cmd_output;
358-
TinyProcessLib::Process proc(command, "", [&](const char* bytes, size_t n){cmd_output.assign(bytes, n);});
411+
std::string cmd_output;
412+
TinyProcessLib::Process proc(command, "", [&](const char* bytes, size_t n) { cmd_output.assign(bytes, n); });
359413
if (!parse_args.parsingLayout && !removetag && parser.dollar_pos != std::string::npos)
360414
parse_args.pureOutput.replace(parser.dollar_pos, command.length() + "$()"_len, cmd_output);
361415

@@ -396,7 +450,7 @@ std::optional<std::string> parse_color_tag(Parser& parser, parse_args_t& parse_a
396450
if (output[0] == '$')
397451
output += ' ';
398452
#endif
399-
453+
400454
static std::vector<std::string> alias_colors_name, alias_colors_value;
401455
const std::vector<std::string>& alias_colors = config.getValueArrayStr("config.alias-colors", {});
402456
if (!alias_colors.empty() && (alias_colors_name.empty() && alias_colors_value.empty()))
@@ -406,7 +460,8 @@ std::optional<std::string> parse_color_tag(Parser& parser, parse_args_t& parse_a
406460
const size_t pos = str.find('=');
407461
if (pos == std::string::npos)
408462
die(_("alias color '{}' does NOT have an equal sign '=' for separating color name and value\n"
409-
"For more check with --help"), str);
463+
"For more check with --help"),
464+
str);
410465

411466
alias_colors_name.push_back(str.substr(0, pos));
412467
alias_colors_value.push_back(str.substr(pos + 1));
@@ -793,10 +848,10 @@ std::string parse(Parser& parser, parse_args_t& parse_args, const bool evaluate,
793848
}
794849

795850
EXPORT std::string parse(std::string input, const moduleMap_t& modulesInfo, std::string& pureOutput,
796-
std::vector<std::string>& layout, std::vector<std::string>& tmp_layout, const ConfigBase& config,
797-
const bool parsingLayout, bool& no_more_reset)
851+
std::vector<std::string>& layout, std::vector<std::string>& tmp_layout,
852+
const ConfigBase& config, const bool parsingLayout, bool& no_more_reset)
798853
{
799-
const std::string& sep_reset = config.getValue<std::string>("config.sep-reset", ":");
854+
static const std::string& sep_reset = config.getValue<std::string>("config.sep-reset", ":");
800855
if (!sep_reset.empty() && parsingLayout && !no_more_reset)
801856
{
802857
if (config.getValue("config.sep-reset-after", false))

src/core-modules/linux/disk.cc

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static struct mntent* get_disk_info(const callbackInfo_t* callbackInfo)
166166

167167
const std::string& path = callbackInfo->moduleArgs->value;
168168
if (access(path.c_str(), F_OK) != 0 || !mountsFile)
169-
die("Failed to query disk at path: '{}", path);
169+
die("Failed to query disk at path: '{}'", path);
170170

171171
struct mntent* pDevice;
172172
while ((pDevice = getmntent(mountsFile)))
@@ -192,18 +192,31 @@ static bool get_disk_usage_info(const callbackInfo_t* callbackInfo, struct statv
192192
// clang-format off
193193
// don't get confused by the name pls
194194
MODFUNC(disk_fsname)
195-
{ return get_disk_info(callbackInfo)->mnt_type; }
195+
{
196+
mntent* d = get_disk_info(callbackInfo);
197+
return d ? d->mnt_type : MAGIC_LINE;
198+
}
196199

197200
MODFUNC(disk_device)
198-
{ return get_disk_info(callbackInfo)->mnt_fsname; }
201+
{
202+
mntent* d = get_disk_info(callbackInfo);
203+
return d ? d->mnt_fsname : MAGIC_LINE;
204+
}
199205

200206
MODFUNC(disk_mountdir)
201-
{ return get_disk_info(callbackInfo)->mnt_dir; }
207+
{
208+
mntent* d = get_disk_info(callbackInfo);
209+
return d ? d->mnt_dir : MAGIC_LINE;
210+
}
202211

203212
// clang-format on
204213
MODFUNC(disk_types)
205214
{
206-
const int types = get_disk_type(get_disk_info(callbackInfo));
215+
mntent* d = get_disk_info(callbackInfo);
216+
if (!d)
217+
return MAGIC_LINE;
218+
219+
const int types = get_disk_type(d);
207220
std::string str;
208221
if (types & DISK_VOLUME_TYPE_EXTERNAL)
209222
str += "External, ";

src/display.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ std::vector<std::string> Display::render(const Config& config, const bool alread
404404
origin += asciiArt.at(i).length();
405405
}
406406

407-
const size_t spaces = (maxLineLength + (config.args_disable_source ? 1 : offset)) -
407+
const size_t spaces = (maxLineLength + (config.args_disable_source ? 0 : offset)) -
408408
(i < asciiArt.size() ? pureAsciiArtLens.at(i) : 0);
409409

410410
debug("spaces: {}", spaces);

0 commit comments

Comments
 (0)