Skip to content

Commit

Permalink
display: parse the distro ASCII art when using --source-path and spec…
Browse files Browse the repository at this point in the history
…ify --distro
  • Loading branch information
Toni500github committed Oct 3, 2024
1 parent 4c42b73 commit 5153646
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
65 changes: 28 additions & 37 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(const Config& config, const colors_t& colors,
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)
{
std::string distro_path{ Display::detect_distro(config) };
systemInfo_t systemInfo{};
std::vector<std::string> layout{ config.layout };

int image_width, image_height, channels;
Expand All @@ -68,23 +66,7 @@ static std::vector<std::string> render_with_image(const Config& config, const co
else
die("Unable to load image '{}'", path);

if (!config.ascii_logo_type.empty())
{
const size_t& pos = distro_path.rfind('.');

if (pos != std::string::npos)
distro_path.insert(pos, "_" + config.ascii_logo_type);
else
distro_path += "_" + config.ascii_logo_type;
}

// this is just for parse() to auto add the distro colors
std::ifstream file(distro_path, std::ios::binary);
std::string line, _;

while (std::getline(file, line))
parse(line, systemInfo, _, config, colors, false);

std::string _;
for (std::string& layout : layout)
layout = parse(layout, systemInfo, _, config, colors, true);

Expand Down Expand Up @@ -167,18 +149,6 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
systemInfo_t systemInfo{};
std::vector<std::string> asciiArt{}, layout{ config.layout };

// 1. if we shouldn't display the autodetected distro
// 2. if we don't have --no-display enabled
// 3. if the source path for the logo is not empty
// 4. if we don't want to display a custom distro
// 5. if we aren't in GUI mode (I don't remember why to check that)
// damn I forgot even why I made this
if (!config.m_display_distro && !config.m_disable_source && !config.source_path.empty() &&
(!config.m_custom_distro.empty() && !config.gui))
{
die("You need to specify if either using a custom distro ascii art OR a custom source path");
}

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

std::ifstream file;
Expand All @@ -191,6 +161,29 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
die("Could not open ascii art file \"{}\"", path);
}

if (!config.m_custom_distro.empty() && !config.m_display_distro)
{
std::string distro_path{ Display::detect_distro(config) };
if (!config.ascii_logo_type.empty())
{
const size_t pos = distro_path.rfind('.');

if (pos != std::string::npos)
distro_path.insert(pos, "_" + config.ascii_logo_type);
else
distro_path += "_" + config.ascii_logo_type;
}

debug("{} distro_path = {}", __FUNCTION__, distro_path);

// this is just for parse() to auto add the distro colors
std::ifstream distro_file(distro_path);
std::string line, _;

while (std::getline(distro_file, line))
parse(line, systemInfo, _, config, colors, false);
}

std::vector<size_t> pureAsciiArtLens;
int maxLineLength = -1;

Expand Down Expand Up @@ -218,9 +211,7 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
get_pos(y, x);
fmt::print("\033[{};{}H", y, x);

const auto& ret = render_with_image(config, colors, path, font_width, font_height);

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

Expand Down Expand Up @@ -307,8 +298,8 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
origin += asciiArt.at(i).length();
}

const size_t& spaces = (maxLineLength + (config.m_disable_source ? 1 : config.offset)) -
(i < asciiArt.size() ? pureAsciiArtLens.at(i) : 0);
const size_t spaces = (maxLineLength + (config.m_disable_source ? 1 : config.offset)) -
(i < asciiArt.size() ? pureAsciiArtLens.at(i) : 0);

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

Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ static bool parseargs(int argc, char* argv[], Config& config, const std::string_
return true;
}

int main (int argc, char *argv[])
int main(int argc, char *argv[])
{

#ifdef VENDOR_TEST
Expand Down Expand Up @@ -423,7 +423,7 @@ int main (int argc, char *argv[])

std::string path = config.m_display_distro ? Display::detect_distro(config) : config.source_path;

if (!config.ascii_logo_type.empty())
if (!config.ascii_logo_type.empty() && config.m_display_distro)
{
const size_t& pos = path.rfind('.');

Expand Down
9 changes: 5 additions & 4 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
}

std::string command; // what's inside the tag
size_t endBracketIndex = -1;

char type = ' '; // ' ' = undefined, ')' = shell exec, 2 = ')' asking for a module
const char opentag = output[dollarSignIndex + 1];
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)
{
Expand Down

0 comments on commit 5153646

Please sign in to comment.