Skip to content

Commit

Permalink
gui: better bash escape colors support
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Jun 19, 2024
1 parent b1e945a commit 9d24f89
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 70 deletions.
2 changes: 1 addition & 1 deletion include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Display {

std::vector<std::string>& render(std::string reset_fgcolor = "");
std::vector<std::string>& render();
void display(std::vector<std::string>& renderResult);
std::string detect_distro();

Expand Down
2 changes: 1 addition & 1 deletion include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class RAM {
// Parse input, in-place, with data from systemInfo.
// Documentation on formatting is in the default config.toml file.
// pureOutput is set to the string, but without the brackets.
std::string parse(std::string& input, systemInfo_t &systemInfo, std::unique_ptr<std::string> &pureOutput, std::string reset_fgcolor);
std::string parse(std::string& input, systemInfo_t &systemInfo, std::unique_ptr<std::string> &pureOutput );

// Set module values to a systemInfo_t map.
// If the name of said module matches any module name, it will be added
Expand Down
6 changes: 3 additions & 3 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::string Display::detect_distro() {
return file_path;
}

std::vector<std::string>& Display::render(std::string reset_fgcolor) {
std::vector<std::string>& Display::render() {
systemInfo_t systemInfo{};

// first check if the file is an image
Expand Down Expand Up @@ -59,7 +59,7 @@ std::vector<std::string>& Display::render(std::string reset_fgcolor) {

for (std::string& layout : config.layouts) {
std::unique_ptr<std::string> _;
layout = parse(layout, systemInfo, _, reset_fgcolor);
layout = parse(layout, systemInfo, _);
}

std::string path = config.m_display_distro ? detect_distro() : config.source_path;
Expand All @@ -78,7 +78,7 @@ std::vector<std::string>& Display::render(std::string reset_fgcolor) {

while (std::getline(file, line)) {
std::unique_ptr<std::string> pureOutput = std::make_unique<std::string>();
std::string asciiArt_s = parse(line, systemInfo, pureOutput, reset_fgcolor);
std::string asciiArt_s = parse(line, systemInfo, pureOutput);
asciiArt_s += config.gui ? "" : NOCOLOR;

asciiArt.push_back(asciiArt_s);
Expand Down
16 changes: 8 additions & 8 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
using namespace GUI;

// https://www.codespeedy.com/convert-rgb-to-hex-color-code-in-cpp/
static std::string rgba_to_hexstr(const Gdk::RGBA& color) {
/*static std::string rgba_to_hexstr(const Gdk::RGBA& color) {
int red = color.get_red() * 255;
int green = color.get_green() * 255;
int blue = color.get_blue() * 255;
std::stringstream ss;
ss << "#" << std::hex << (red << 16 | green << 8 | blue);
return ss.str();
}
}*/

// Display::render but only for images on GUI
static std::vector<std::string>& render_with_image(std::string reset_fgcolor) {
static std::vector<std::string>& render_with_image() {
systemInfo_t systemInfo{};

int image_width, image_height, channels;
Expand Down Expand Up @@ -57,7 +57,7 @@ static std::vector<std::string>& render_with_image(std::string reset_fgcolor) {

for (std::string& layout : config.layouts) {
std::unique_ptr<std::string> _;
layout = parse(layout, systemInfo, _, reset_fgcolor);
layout = parse(layout, systemInfo, _);
}

for (size_t i = 0; i < config.layouts.size(); i++) {
Expand Down Expand Up @@ -103,15 +103,15 @@ Window::Window() {
context->set_font_description(font);

auto style_context = m_label.get_style_context();
Gdk::RGBA fg_color;
/*Gdk::RGBA fg_color;
style_context->lookup_color("theme_fg_color", fg_color);
std::string fg_color_str = rgba_to_hexstr(fg_color);
std::string fg_color_str = rgba_to_hexstr(fg_color);*/

std::string colored_text;
if (useImage)
colored_text = fmt::format("{}", fmt::join(render_with_image(fg_color_str), "\n"));
colored_text = fmt::format("{}", fmt::join(render_with_image(), "\n"));
else
colored_text = fmt::format("{}", fmt::join(Display::render(fg_color_str), "\n"));
colored_text = fmt::format("{}", fmt::join(Display::render(), "\n"));

m_label.set_markup(colored_text);
m_label.set_alignment(Gtk::ALIGN_CENTER);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static bool parseargs(int argc, char* argv[]) {
case 'l':
components_list(); break;
case 'g':
config.overrides["config.gui"] = {BOOL, "", true}; break;
config.overrides["gui.enable"] = {BOOL, "", true}; break;
case 'C':
configFile = strndup(optarg, PATH_MAX); break;
case 'd':
Expand Down
113 changes: 57 additions & 56 deletions src/query.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,89 @@
#include "query.hpp"

#include <unistd.h>

#include <array>
#include <stdexcept>
#include <string>

#include "query.hpp"
#include "config.hpp"
#include "switch_fnv1a.hpp"

// using namespace Query;

static std::array<std::string, 3> get_ansi_color( std::string str )
{
#define light "light"
#define bgcolor "bgcolor"

auto first_m = str.find( "m" );
if ( first_m == std::string::npos )
die( "Parser: failed to parse layout/ascii art: missing m while using ANSI color escape code" );

std::string col = str.erase( first_m );
std::string col = str.erase( first_m ); // 1;42
std::string weight = hasStart( col, "1;" ) ? "bold" : "normal";
std::string type = "fgcolor";
std::string type = "fgcolor"; // either fgcolor or bgcolor

if ( hasStart(col, "1;") || hasStart(col, "0;") )
col.erase(0, 2);

int n = std::stoi( col );

// ugly but efficent code
switch ( n )
{
case 40: type = bgcolor;
case 100: weight = light;
case 30:
col = color.gui_black;
// copy paste ahh code
// unfortunatly you can't do bold and light in pango
switch (n) {
case 90:
case 30: col = color.gui_black;
break;

case 41: type = bgcolor;
case 101: weight = light;
case 31:
col = color.gui_red;

case 91:
case 31: col = color.gui_red;
break;

case 42: type = bgcolor;
case 102: weight = light;
case 32:
col = color.gui_green;

case 92:
case 32: col = color.gui_green;
break;

case 43: type = bgcolor;
case 103: weight = light;
case 33:
col = color.gui_yellow;

case 93:
case 33: col = color.gui_yellow;
break;

case 44: type = bgcolor;
case 104: weight = light;
case 34:
col = color.gui_blue;

case 94:
case 34: col = color.gui_blue;
break;

case 45: type = bgcolor;
case 105: weight = light;
case 35:
col = color.gui_magenta;
case 95:
case 35: col = color.gui_magenta;
break;

case 46: type = bgcolor;
case 106: weight = light;
case 36:
col = color.gui_cyan;

case 96:
case 36: col = color.gui_cyan;
break;

case 47: type = bgcolor;
case 107: weight = light;
case 37:
col = color.gui_white;

case 97:
case 37: col = color.gui_white;
break;

case 100:
case 40: col = color.gui_black; type = bgcolor; break;

case 101:
case 41: col = color.gui_red; type = bgcolor; break;

case 102:
case 42: col = color.gui_green; type = bgcolor; break;

case 103:
case 43: col = color.gui_yellow; type = bgcolor; break;

case 104:
case 44: col = color.gui_blue; type = bgcolor; break;

case 105:
case 45: col = color.gui_magenta; type = bgcolor; break;

case 106:
case 46: col = color.gui_cyan; type = bgcolor; break;

case 107:
case 47: col = color.gui_white; type = bgcolor; break;
}

if ( col[0] != '#' )
Expand All @@ -100,7 +105,7 @@ static std::string getInfoFromName( systemInfo_t& systemInfo, const std::string&

auto result = systemInfo[sections[0]][sections[1]];
std::string stringResult;

if ( std::holds_alternative<size_t>( result ) )
stringResult = std::to_string( std::get<size_t>( result ) );
else
Expand All @@ -114,8 +119,7 @@ static std::string getInfoFromName( systemInfo_t& systemInfo, const std::string&
};
}

std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr<std::string>& pureOutput,
std::string reset_fgcolor )
std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr<std::string>& pureOutput )
{
std::string output = input;
if ( pureOutput )
Expand Down Expand Up @@ -196,12 +200,9 @@ std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr
break;
case '}': // please pay very attention when reading this unreadable code
if ( command == "0" )
{
{
resetclr = true;
output = output.replace(
dollarSignIndex, ( endBracketIndex + 1 ) - dollarSignIndex,
config.gui ? fmt::format( "<span fgcolor='{}' weight='normal'>", reset_fgcolor ) : NOCOLOR );

output = output.replace( dollarSignIndex, ( endBracketIndex + 1 ) - dollarSignIndex, config.gui ? "</span><span>" : NOCOLOR );
if ( pureOutput )
*pureOutput = pureOutput->replace( pureOutput->size() /*dollarSignIndex-pureOutputOffset*/,
( endBracketIndex + 1 ) - dollarSignIndex, "" );
Expand Down Expand Up @@ -312,7 +313,7 @@ std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr
break;
}
// close the span tag of the reseted color
output += (config.gui && resetclr) ? "</span>" : "";
//output += (config.gui && resetclr) ? "</span>" : "";
resetclr = false;
}

Expand Down

0 comments on commit 9d24f89

Please sign in to comment.