feat: created a color function for ANSI colors

This commit is contained in:
2026-05-25 19:50:24 +03:00
parent 4e3089c84e
commit e0d49b76f8
+42 -17
View File
@@ -28,6 +28,17 @@ string getGPU();
string getRAM(); string getRAM();
string getOsDate(); string getOsDate();
enum class Color {
Red,
Green,
Blue,
Yellow,
Magenta,
Reset
};
string color(Color c);
struct gpuId { struct gpuId {
string vendor; string vendor;
string device; string device;
@@ -55,12 +66,6 @@ std::vector<gpuId> getGpuIds() {
int main () { int main () {
std::setlocale(LC_ALL, ""); std::setlocale(LC_ALL, "");
const string colorRED = "\033[1;31m";
const string colorGREEN = "\033[1;32m";
const string colorBLUE = "\033[1;34m";
const string colorYELLOW = "\033[1;33m";
const string colorMAGENTA = "\033[1;35m";
const string colorRESET = "\033[0m";
auto displayWidth = [&](const string& text) { auto displayWidth = [&](const string& text) {
std::mbstate_t state{}; std::mbstate_t state{};
@@ -82,7 +87,7 @@ int main () {
return width; return width;
}; };
auto formatLine = [&](const string& color, const string& label, const string& value) { auto formatLine = [&](Color c, const string& label, const string& value) {
const size_t labelWidth = 12; const size_t labelWidth = 12;
const size_t valueGap = 2; const size_t valueGap = 2;
string padded = label; string padded = label;
@@ -91,18 +96,18 @@ int main () {
padded += string(labelWidth - currentWidth, ' '); padded += string(labelWidth - currentWidth, ' ');
} }
padded += string(valueGap, ' '); padded += string(valueGap, ' ');
return color + padded + colorRESET + value; return color(c) + padded + color(Color::Reset) + value;
}; };
std::vector<string> infoLines = { std::vector<string> infoLines = {
formatLine(colorGREEN, " distro:", getDistro()), formatLine(Color::Green, " distro:", getDistro()),
formatLine(colorMAGENTA, " kernel:", getKernel()), formatLine(Color::Magenta, " kernel:", getKernel()),
formatLine(colorBLUE, " uptime:", getUptime()), formatLine(Color::Blue, " uptime:", getUptime()),
formatLine(colorMAGENTA, " shell:", getShell()), formatLine(Color::Magenta, " shell:", getShell()),
formatLine(colorYELLOW, "󰍛 CPU:", getCPU()), formatLine(Color::Yellow, "󰍛 CPU:", getCPU()),
formatLine(colorYELLOW, "󰾲 GPU:", getGPU()), formatLine(Color::Yellow, "󰾲 GPU:", getGPU()),
formatLine(colorRED, " RAM:", getRAM()), formatLine(Color::Red, " RAM:", getRAM()),
formatLine(colorBLUE, " OS Date:", getOsDate()) formatLine(Color::Blue, " OS Date:", getOsDate())
}; };
std::vector<string> logoLines = distroArt(); std::vector<string> logoLines = distroArt();
@@ -113,7 +118,8 @@ int main () {
const string gap = " "; const string gap = " ";
cout << string(logoWidth, ' ') << gap cout << string(logoWidth, ' ') << gap
<< "--- " << colorGREEN << getUser() << colorRESET << "@" << colorRED << getHost() << colorRESET << " ---\n"; << "--- " << color(Color::Green) << getUser() << color(Color::Reset)
<< "@" << color(Color::Red) << getHost() << color(Color::Reset) << " ---\n";
cout << "\n"; cout << "\n";
size_t totalLines = infoLines.size(); size_t totalLines = infoLines.size();
@@ -196,6 +202,25 @@ string getDistro() {
else return ""; else return "";
} }
string color(Color c) {
switch (c) {
case Color::Red:
return "\033[1;31m";
case Color::Green:
return "\033[1;32m";
case Color::Blue:
return "\033[1;34m";
case Color::Yellow:
return "\033[1;33m";
case Color::Magenta:
return "\033[1;35m";
case Color::Reset:
return "\033[0m";
}
return "\033[0m";
}
std::vector<string> distroArt() { std::vector<string> distroArt() {
std::ifstream readOsRelease("/etc/os-release"); std::ifstream readOsRelease("/etc/os-release");