Skip to content

Commit

Permalink
parse (args): add --no-color and --layout-line
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Oct 3, 2024
1 parent b164799 commit 9ad21de
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
12 changes: 11 additions & 1 deletion cufetch.1
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ To escape any $ or bracket, just use \\
.SH OPTIONS
.TP
\fB\-n\fR, \fB\-\-no\-display\fR
Do not display the ascii art
Do not display the logo
.TP
\fB\-N\fR, \fB\-\-no\-color\fR
Do not output and parse colors. Useful for stdout or pipe operations
.TP
\fB\-s\fR, \fB\-\-source\-path\fR <path>
Path to the ascii art file to display
Expand Down Expand Up @@ -129,6 +132,13 @@ An example: "[Liberation Mono] [Normal] [12]", which can be "Liberation Mono Nor
.br
It's recommended to use GUI mode for the moment if something doesn't work
.TP
\fB\-m\fR, \fB\-\-layout\-line\fR
Will replace the config layout, with a layout you specify in the arguments
.br
Example: "cufetch -m "${auto}OS: $<os.name>" -m "${auto}CPU: $<cpu.cpu>" "
.br
Will only print the logo (if not disabled), along side the parsed OS and CPU
.TP
\fB\-g\fR, \fB\-\-gui\fR
Use GUI mode instead of priting in the terminal (use \fB\-V\fR to check if it's enabled)
.TP
Expand Down
2 changes: 2 additions & 0 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ class Config
std::vector<std::string> apk_files;

// inner management / argument configs
std::vector<std::string> m_args_layout;
std::string m_custom_distro;
std::string m_image_backend;
bool m_disable_source = false;
bool m_disable_colors = false;
bool m_display_distro = true;
bool m_print_logo_only = false;

Expand Down
11 changes: 7 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
const std::string_view path)
{
systemInfo_t systemInfo{};
std::vector<std::string> asciiArt{}, layout{ config.layout };
std::vector<std::string> asciiArt{}, layout{ config.m_args_layout.empty() ? config.layout : config.m_args_layout };

debug("Display::render path = {}", path);

Expand Down Expand Up @@ -231,7 +231,8 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
{
std::string pureOutput;
std::string asciiArt_s = parse(line, systemInfo, pureOutput, config, colors, false);
asciiArt_s += config.gui ? "" : NOCOLOR;
if (!config.m_disable_colors)
asciiArt_s += config.gui ? "" : NOCOLOR;

if (config.gui)
{
Expand Down Expand Up @@ -306,15 +307,17 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
for (size_t j = 0; j < spaces; j++)
layout.at(i).insert(origin, " ");

layout.at(i) += config.gui ? "" : NOCOLOR;
if (!config.m_disable_colors)
layout.at(i) += config.gui ? "" : NOCOLOR;
}

for (; i < asciiArt.size(); i++)
{
std::string line;
line.reserve(config.logo_padding_left + asciiArt.at(i).length());

for (size_t j = 0; j < config.logo_padding_left; j++)
line += " ";
line += ' ';

line += asciiArt.at(i);

Expand Down
2 changes: 1 addition & 1 deletion src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static std::vector<std::string> render_with_image(const Config& config, const co
{
std::string path{ Display::detect_distro(config) };
systemInfo_t systemInfo{};
std::vector<std::string> layout{ config.layout };
std::vector<std::string> layout{ config.m_args_layout.empty() ? config.layout : config.m_args_layout };

int image_width, image_height, channels;

Expand Down
17 changes: 14 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ static void help(bool invalid_opt = false)
fmt::println(R"(
A command-line system information tool (or neofetch like program), which its focus point is customizability and perfomance
-n, --no-display Do not display the ascii art
-s, --source-path <path> Path to the ascii art file to display
-n, --no-display Do not display the logo
-N, --no-color Do not output and parse colors. Useful for stdout or pipe operations
-s, --source-path <path> Path to the ascii art or image file to display
-C, --config <path> Path to the config file to use
-a, --ascii-logo-type [<name>]
The type of ASCII art to apply ("small" or "old").
Expand All @@ -62,6 +63,10 @@ A command-line system information tool (or neofetch like program), which its foc
Right now only 'kitty' and 'viu' are supported
It's recommended to use GUI mode for the moment if something doesn't work
-m, --layout-line Will replace the config layout, with a layout you specify in the arguments
Example: "cufetch -m "${{auto}}OS: $<os.name>" -m "${{auto}}CPU: $<cpu.cpu>" "
Will only print the logo (if not disabled), along side the parsed OS and CPU
-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 modules and its members
Expand Down Expand Up @@ -266,18 +271,20 @@ static bool parseargs(int argc, char* argv[], Config& config, const std::string_
int opt = 0;
int option_index = 0;
opterr = 1; // re-enable since before we disabled for "invalid option" error
const char *optstring = "-VhnLlga::f:o:C:i:d:D:s:";
const char *optstring = "-VhnNLlga::f:o:C:i:d:D:s:m:";
static const struct option opts[] = {
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"no-display", no_argument, 0, 'n'},
{"no-color", no_argument, 0, 'N'},
{"list-modules", no_argument, 0, 'l'},
{"logo-only", no_argument, 0, 'L'},
{"gui", no_argument, 0, 'g'},
{"ascii-logo-type", optional_argument, 0, 'a'},
{"offset", required_argument, 0, 'o'},
{"font", required_argument, 0, 'f'},
{"config", required_argument, 0, 'C'},
{"layout-line", required_argument, 0, 'm'},
{"data-dir", required_argument, 0, 'D'},
{"distro", required_argument, 0, 'd'},
{"source-path", required_argument, 0, 's'},
Expand Down Expand Up @@ -329,10 +336,14 @@ static bool parseargs(int argc, char* argv[], Config& config, const std::string_
config.data_dir = optarg; break;
case 'd':
config.m_custom_distro = str_tolower(optarg); break;
case 'm':
config.m_args_layout.push_back(optarg); break;
case 's':
config.source_path = optarg; break;
case 'i':
config.m_image_backend = optarg; break;
case 'N':
config.m_disable_colors = true; break;
case 'a':
if (OPTIONAL_ARGUMENT_IS_PRESENT)
config.ascii_logo_type = optarg;
Expand Down
14 changes: 11 additions & 3 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
if (dollarSignIndex == std::string::npos)
break;

else if (dollarSignIndex <= oldDollarSignIndex && start)
else if (dollarSignIndex <= oldDollarSignIndex && start && !config.m_disable_colors)
{
dollarSignIndex = output.find('$', dollarSignIndex + 1);
// oh nooo.. whatever
Expand Down Expand Up @@ -408,6 +408,14 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s

case '}': // please pay close attention when reading this really long code

if (config.m_disable_colors)
{
output.erase(dollarSignIndex, taglen);
if (tagpos != std::string::npos)
pureOutput.erase(tagpos, tagToReplace.length());
break;
}

// if at end there a '$', it will make the end output "$</span>" and so it will confuse
// addValueFromModule() and so let's make it "$ </span>". this is geniunenly stupid
if (config.gui && output.back() == '$')
Expand All @@ -426,7 +434,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
// I don't care, it does the work and well
if (command == *it_name)
command = config.colors_value.at(it_value);
goto jump;
goto jumpauto;
}

if (command == *it_name)
Expand All @@ -446,7 +454,7 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
command = auto_colors.at(ver);
}

jump:
jumpauto:
if (command == "1")
{
if (firstrun_noclr)
Expand Down

0 comments on commit 9ad21de

Please sign in to comment.