Skip to content

Commit

Permalink
gui: parse distro ascii art for getting auto colors
Browse files Browse the repository at this point in the history
instead of using always white
  • Loading branch information
Toni500github committed Sep 9, 2024
1 parent 9edd7f3 commit e38d5ca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ CXXFLAGS += -fvisibility=hidden -Iinclude -std=c++20 $(VARS) -DVERSION=\"

all: fmt toml $(TARGET)

recompile: clean all

fmt:
ifeq ($(wildcard $(BUILDDIR)/fmt/libfmt.a),)
mkdir -p $(BUILDDIR)/fmt
Expand Down
2 changes: 1 addition & 1 deletion include/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @param is_image If the source path is an image (used for GUI mode only)
*/
std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::string& pureOutput, const Config& config,
const colors_t& colors, const bool parsingLayout, const bool is_image = false);
const colors_t& colors, const bool parsingLayout);

/* Set module members values to a systemInfo_t map.
* If the name of said module matches any module name, it will be added
Expand Down
8 changes: 4 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ std::string Display::detect_distro(const Config& config)
{
debug("/etc/os-release = \n{}", shell_exec("cat /etc/os-release"));

const std::string& ascii_logo_type = (config.ascii_logo_type.empty() ? "" : "_" + config.ascii_logo_type);

if (!config.m_custom_distro.empty())
{
return fmt::format("{}/ascii/{}.txt", config.data_dir, config.m_custom_distro);
return fmt::format("{}/ascii/{}{}.txt", config.data_dir, config.m_custom_distro, ascii_logo_type);
}
else
{
const std::string& ascii_logo_type = (config.ascii_logo_type.empty() ? "" : "_" + config.ascii_logo_type);

Query::System system;
std::string format;

Expand All @@ -50,7 +50,7 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c

if (!config.m_display_distro && !config.m_disable_source && !config.source_path.empty())
{
if (!config.m_custom_distro.empty())
if (!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");
}

Expand Down
12 changes: 11 additions & 1 deletion src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using namespace GUI;
// Display::render but only for images on GUI
static std::vector<std::string> render_with_image(const Config& config, const colors_t& colors)
{
const std::string& path = Display::detect_distro(config);
systemInfo_t systemInfo{};
std::vector<std::string> layout{ config.layout };

Expand All @@ -46,10 +47,19 @@ static std::vector<std::string> render_with_image(const Config& config, const co
stbi_image_free(img);
else
die("Unable to load image '{}'", config.source_path);

// this is just for parse() to auto add the distro colors
std::ifstream file(path, std::ios::binary);
std::string line;
while (std::getline(file, line))
{
std::string _;
parse(line, systemInfo, _, config, colors, false);
}

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

// erase each element for each instance of MAGIC_LINE
layout.erase(std::remove_if(layout.begin(), layout.end(),
Expand Down
17 changes: 6 additions & 11 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ std::string getInfoFromName(const systemInfo_t& systemInfo, const std::string_vi
}

std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::string& pureOutput, const Config& config,
const colors_t& colors, const bool parsingLayout, const bool is_image)
const colors_t& colors, const bool parsingLayout)
{
std::string output = input.data();
pureOutput = output;
Expand Down Expand Up @@ -356,22 +356,17 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s

if (hasStart(command, "auto"))
{
std::uint8_t ver =
static_cast<std::uint8_t>(command.length() > 4 ? std::stoi(command.substr(4)) - 1 : 0);
std::uint16_t ver =
static_cast<std::uint16_t>(command.length() > 4 ? std::stoi(command.substr(4)) - 1 : 0);
if (ver >= auto_colors.size() || ver < 1)
ver = 0;

if (auto_colors.empty())
{
if (is_image)
auto_colors.push_back(config.gui ? "white" : NOCOLOR_BOLD);
if (firstrun_noclr)
auto_colors.push_back(config.gui ? "<span weight='bold'>" : NOCOLOR_BOLD);
else
{
if (firstrun_noclr)
auto_colors.push_back(config.gui ? "<span weight='bold'>" : NOCOLOR_BOLD);
else
auto_colors.push_back(config.gui ? "</span><span weight='bold'>" : NOCOLOR_BOLD);
}
auto_colors.push_back(config.gui ? "</span><span weight='bold'>" : NOCOLOR_BOLD);
}

command = auto_colors.at(ver);
Expand Down

0 comments on commit e38d5ca

Please sign in to comment.