mirror of
https://github.com/arthur-pbty/optifetch.git
synced 2026-08-01 20:29:33 +02:00
add disk usage color & add picture for readme
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
Un outil d'information système rapide, léger, écrit en C et sans dépendances externes. Inspiré de Fastfetch, il se concentre sur la rapidité d'exécution et la personnalisation via un fichier de configuration simple.
|
Un outil d'information système rapide, léger, écrit en C et sans dépendances externes. Inspiré de Fastfetch, il se concentre sur la rapidité d'exécution et la personnalisation via un fichier de configuration simple.
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="assets/picture.png" alt="Picture" width="700"/>
|
||||||
|
</p>
|
||||||
|
|
||||||
## Fonctionnalités
|
## Fonctionnalités
|
||||||
|
|
||||||
- **Zéro dépendance** : Uniquement la bibliothèque C standard (glibc/musl).
|
- **Zéro dépendance** : Uniquement la bibliothèque C standard (glibc/musl).
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 244 KiB |
@@ -29,7 +29,6 @@ void generate_default_config(const char *path)
|
|||||||
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}Date{reset}: {align:V}{date} {time}\n");
|
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}Date{reset}: {align:V}{date} {time}\n");
|
||||||
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}Local IP{reset}: {align:V}{ip_local}\n");
|
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}Local IP{reset}: {align:V}{ip_local}\n");
|
||||||
fprintf(f, "{if:wifi} {distro_color}{logo} {align:L}{fg:157,211,154}Wifi{reset}: {align:V}{wifi}\n{endif}");
|
fprintf(f, "{if:wifi} {distro_color}{logo} {align:L}{fg:157,211,154}Wifi{reset}: {align:V}{wifi}\n{endif}");
|
||||||
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}Public IP{reset}: {align:V}{ip_public}\n");
|
|
||||||
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}DNS{reset}: {align:V}{dns}\n");
|
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}DNS{reset}: {align:V}{dns}\n");
|
||||||
fprintf(f, " {distro_color}{logo}\n");
|
fprintf(f, " {distro_color}{logo}\n");
|
||||||
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}███{fg:23,147,209}███{fg:255,200,87}███{fg:255,90,120}███{fg:180,120,255}███{fg:80,220,180}███{fg:240,240,240}███{fg:120,120,120}███{reset}\n");
|
fprintf(f, " {distro_color}{logo} {align:L}{fg:157,211,154}███{fg:23,147,209}███{fg:255,200,87}███{fg:255,90,120}███{fg:180,120,255}███{fg:80,220,180}███{fg:240,240,240}███{fg:120,120,120}███{reset}\n");
|
||||||
|
|||||||
+51
-13
@@ -18,10 +18,25 @@ static int current_pass = 1;
|
|||||||
|
|
||||||
static int logo_line_idx = 0;
|
static int logo_line_idx = 0;
|
||||||
static int prev_line_had_logo = 0;
|
static int prev_line_had_logo = 0;
|
||||||
|
|
||||||
// NOUVEAU : Variable pour ignorer les codes ANSI lors du calcul de la largeur
|
|
||||||
static int in_ansi_escape = 0;
|
static int in_ansi_escape = 0;
|
||||||
|
|
||||||
|
// NOUVEAU : Génère le code ANSI pour un pourcentage dynamique
|
||||||
|
static void get_dynamic_color(double pct, char *out, size_t size)
|
||||||
|
{
|
||||||
|
if (pct >= 80.0)
|
||||||
|
{
|
||||||
|
snprintf(out, size, "\033[38;2;255;50;50m"); // Rouge
|
||||||
|
}
|
||||||
|
else if (pct >= 50.0)
|
||||||
|
{
|
||||||
|
snprintf(out, size, "\033[38;2;255;165;0m"); // Orange
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(out, size, "\033[38;2;0;200;0m"); // Vert
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int get_align_width(const char *id)
|
static int get_align_width(const char *id)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < align_count; i++)
|
for (int i = 0; i < align_count; i++)
|
||||||
@@ -60,7 +75,6 @@ static void my_putchar(char c)
|
|||||||
putchar(c);
|
putchar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOUVEAU : Logique pour ignorer les séquences d'échappement ANSI (\033[...m)
|
|
||||||
if (c == '\033')
|
if (c == '\033')
|
||||||
{
|
{
|
||||||
in_ansi_escape = 1;
|
in_ansi_escape = 1;
|
||||||
@@ -68,15 +82,13 @@ static void my_putchar(char c)
|
|||||||
}
|
}
|
||||||
if (in_ansi_escape)
|
if (in_ansi_escape)
|
||||||
{
|
{
|
||||||
// Les séquences ANSI se terminent par une lettre (souvent 'm')
|
|
||||||
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
||||||
{
|
{
|
||||||
in_ansi_escape = 0;
|
in_ansi_escape = 0;
|
||||||
}
|
}
|
||||||
return; // On ne compte pas ces caractères dans current_col
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compte les caractères UTF-8 visuels
|
|
||||||
if ((c & 0xC0) != 0x80)
|
if ((c & 0xC0) != 0x80)
|
||||||
{
|
{
|
||||||
current_col++;
|
current_col++;
|
||||||
@@ -201,42 +213,68 @@ static void print_variable(const char *name, SysInfo *info)
|
|||||||
snprintf(buf, sizeof(buf), "%s", info->locale);
|
snprintf(buf, sizeof(buf), "%s", info->locale);
|
||||||
else if (strcmp(name, "disk_fs") == 0)
|
else if (strcmp(name, "disk_fs") == 0)
|
||||||
snprintf(buf, sizeof(buf), "%s", info->disk_fs);
|
snprintf(buf, sizeof(buf), "%s", info->disk_fs);
|
||||||
|
|
||||||
|
// NOUVEAU : Gestion des variables avec unités et couleurs dynamiques
|
||||||
else if (strncmp(name, "ram", 3) == 0)
|
else if (strncmp(name, "ram", 3) == 0)
|
||||||
{
|
{
|
||||||
char unit[128] = "mb";
|
char unit[128] = "mb";
|
||||||
if (strlen(name) > 4 && name[3] == ':')
|
if (strlen(name) > 4 && name[3] == ':')
|
||||||
snprintf(unit, sizeof(unit), "%s", name + 4);
|
snprintf(unit, sizeof(unit), "%s", name + 4);
|
||||||
|
if (strcmp(unit, "%") == 0)
|
||||||
|
{
|
||||||
|
double pct = info->ram_total_mb ? (double)info->ram_used_mb / (double)info->ram_total_mb * 100.0 : 0;
|
||||||
|
char col[32];
|
||||||
|
get_dynamic_color(pct, col, sizeof(col));
|
||||||
|
snprintf(buf, sizeof(buf), "%s%.1f%%\033[0m", col, pct);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (strcmp(unit, "gb") == 0)
|
if (strcmp(unit, "gb") == 0)
|
||||||
snprintf(buf, sizeof(buf), "%.1fGB / %.1fGB", (double)info->ram_used_mb / 1024.0, (double)info->ram_total_mb / 1024.0);
|
snprintf(buf, sizeof(buf), "%.1fGB / %.1fGB", (double)info->ram_used_mb / 1024.0, (double)info->ram_total_mb / 1024.0);
|
||||||
else if (strcmp(unit, "%") == 0)
|
|
||||||
snprintf(buf, sizeof(buf), "%.1f%%", info->ram_total_mb ? (double)info->ram_used_mb / (double)info->ram_total_mb * 100.0 : 0);
|
|
||||||
else
|
else
|
||||||
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->ram_used_mb, info->ram_total_mb);
|
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->ram_used_mb, info->ram_total_mb);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (strncmp(name, "swap", 4) == 0)
|
else if (strncmp(name, "swap", 4) == 0)
|
||||||
{
|
{
|
||||||
char unit[128] = "mb";
|
char unit[128] = "mb";
|
||||||
if (strlen(name) > 5 && name[4] == ':')
|
if (strlen(name) > 5 && name[4] == ':')
|
||||||
snprintf(unit, sizeof(unit), "%s", name + 5);
|
snprintf(unit, sizeof(unit), "%s", name + 5);
|
||||||
|
if (strcmp(unit, "%") == 0)
|
||||||
|
{
|
||||||
|
double pct = info->swap_total_mb ? (double)info->swap_used_mb / (double)info->swap_total_mb * 100.0 : 0;
|
||||||
|
char col[32];
|
||||||
|
get_dynamic_color(pct, col, sizeof(col));
|
||||||
|
snprintf(buf, sizeof(buf), "%s%.1f%%\033[0m", col, pct);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (strcmp(unit, "gb") == 0)
|
if (strcmp(unit, "gb") == 0)
|
||||||
snprintf(buf, sizeof(buf), "%.1fGB / %.1fGB", (double)info->swap_used_mb / 1024.0, (double)info->swap_total_mb / 1024.0);
|
snprintf(buf, sizeof(buf), "%.1fGB / %.1fGB", (double)info->swap_used_mb / 1024.0, (double)info->swap_total_mb / 1024.0);
|
||||||
else if (strcmp(unit, "%") == 0)
|
|
||||||
snprintf(buf, sizeof(buf), "%.1f%%", info->swap_total_mb ? (double)info->swap_used_mb / (double)info->swap_total_mb * 100.0 : 0);
|
|
||||||
else
|
else
|
||||||
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->swap_used_mb, info->swap_total_mb);
|
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->swap_used_mb, info->swap_total_mb);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (strncmp(name, "disk", 4) == 0)
|
else if (strncmp(name, "disk", 4) == 0)
|
||||||
{
|
{
|
||||||
char unit[128] = "mb";
|
char unit[128] = "mb";
|
||||||
if (strlen(name) > 5 && name[4] == ':')
|
if (strlen(name) > 5 && name[4] == ':')
|
||||||
snprintf(unit, sizeof(unit), "%s", name + 5);
|
snprintf(unit, sizeof(unit), "%s", name + 5);
|
||||||
|
if (strcmp(unit, "%") == 0)
|
||||||
|
{
|
||||||
|
double pct = info->disk_total_mb ? (double)info->disk_used_mb / (double)info->disk_total_mb * 100.0 : 0;
|
||||||
|
char col[32];
|
||||||
|
get_dynamic_color(pct, col, sizeof(col));
|
||||||
|
snprintf(buf, sizeof(buf), "%s%.1f%%\033[0m", col, pct);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (strcmp(unit, "gb") == 0)
|
if (strcmp(unit, "gb") == 0)
|
||||||
snprintf(buf, sizeof(buf), "%.1fGB / %.1fGB", (double)info->disk_used_mb / 1024.0, (double)info->disk_total_mb / 1024.0);
|
snprintf(buf, sizeof(buf), "%.1fGB / %.1fGB", (double)info->disk_used_mb / 1024.0, (double)info->disk_total_mb / 1024.0);
|
||||||
else if (strcmp(unit, "%") == 0)
|
|
||||||
snprintf(buf, sizeof(buf), "%.1f%%", info->disk_total_mb ? (double)info->disk_used_mb / (double)info->disk_total_mb * 100.0 : 0);
|
|
||||||
else
|
else
|
||||||
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->disk_used_mb, info->disk_total_mb);
|
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->disk_used_mb, info->disk_total_mb);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(buf, sizeof(buf), "{%s}", name);
|
snprintf(buf, sizeof(buf), "{%s}", name);
|
||||||
@@ -250,7 +288,7 @@ static void process_line(const char *line, SysInfo *info)
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
current_col = 0;
|
current_col = 0;
|
||||||
skip_output = 0;
|
skip_output = 0;
|
||||||
in_ansi_escape = 0; // NOUVEAU : Réinitialiser l'état ANSI à chaque ligne
|
in_ansi_escape = 0;
|
||||||
|
|
||||||
int has_logo = (strstr(line, "{logo}") != NULL || strstr(line, "{small_logo}") != NULL);
|
int has_logo = (strstr(line, "{logo}") != NULL || strstr(line, "{small_logo}") != NULL);
|
||||||
if (has_logo && !prev_line_had_logo)
|
if (has_logo && !prev_line_had_logo)
|
||||||
|
|||||||
@@ -38,6 +38,20 @@ static void clean_newline(char *str)
|
|||||||
str[strcspn(str, "\n")] = 0;
|
str[strcspn(str, "\n")] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOUVEAU : Lecture précise de la mémoire depuis /proc/meminfo
|
||||||
|
static long get_meminfo_kb(const char *buf, const char *key)
|
||||||
|
{
|
||||||
|
const char *p = strstr(buf, key);
|
||||||
|
if (p)
|
||||||
|
{
|
||||||
|
p += strlen(key);
|
||||||
|
while (*p == ' ' || *p == '\t' || *p == ':')
|
||||||
|
p++;
|
||||||
|
return atol(p);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void get_info(SysInfo *info)
|
void get_info(SysInfo *info)
|
||||||
{
|
{
|
||||||
struct passwd *pw = getpwuid(getuid());
|
struct passwd *pw = getpwuid(getuid());
|
||||||
@@ -49,7 +63,7 @@ void get_info(SysInfo *info)
|
|||||||
read_file("/etc/os-release", buf, sizeof(buf));
|
read_file("/etc/os-release", buf, sizeof(buf));
|
||||||
extract_value(buf, "PRETTY_NAME", info->os, sizeof(info->os));
|
extract_value(buf, "PRETTY_NAME", info->os, sizeof(info->os));
|
||||||
extract_value(buf, "ID", info->os_id, sizeof(info->os_id));
|
extract_value(buf, "ID", info->os_id, sizeof(info->os_id));
|
||||||
extract_value(buf, "ANSI_COLOR", info->distro_color, sizeof(info->distro_color)); // NOUVEAU
|
extract_value(buf, "ANSI_COLOR", info->distro_color, sizeof(info->distro_color));
|
||||||
|
|
||||||
struct utsname uts;
|
struct utsname uts;
|
||||||
uname(&uts);
|
uname(&uts);
|
||||||
@@ -81,10 +95,17 @@ void get_info(SysInfo *info)
|
|||||||
if (strlen(info->host_model) == 0)
|
if (strlen(info->host_model) == 0)
|
||||||
snprintf(info->host_model, sizeof(info->host_model), "Unknown");
|
snprintf(info->host_model, sizeof(info->host_model), "Unknown");
|
||||||
|
|
||||||
info->ram_total_mb = s_info.totalram * s_info.mem_unit / (1024 * 1024);
|
// NOUVEAU : Calcul de la RAM et Swap sans le cache
|
||||||
info->ram_used_mb = (s_info.totalram - s_info.freeram) * s_info.mem_unit / (1024 * 1024);
|
read_file("/proc/meminfo", buf, sizeof(buf));
|
||||||
info->swap_total_mb = s_info.totalswap * s_info.mem_unit / (1024 * 1024);
|
long mem_total_kb = get_meminfo_kb(buf, "MemTotal");
|
||||||
info->swap_used_mb = (s_info.totalswap - s_info.freeswap) * s_info.mem_unit / (1024 * 1024);
|
long mem_avail_kb = get_meminfo_kb(buf, "MemAvailable");
|
||||||
|
long swap_total_kb = get_meminfo_kb(buf, "SwapTotal");
|
||||||
|
long swap_free_kb = get_meminfo_kb(buf, "SwapFree");
|
||||||
|
|
||||||
|
info->ram_total_mb = mem_total_kb / 1024;
|
||||||
|
info->ram_used_mb = (mem_total_kb - mem_avail_kb) / 1024; // Vraie RAM utilisée
|
||||||
|
info->swap_total_mb = swap_total_kb / 1024;
|
||||||
|
info->swap_used_mb = (swap_total_kb - swap_free_kb) / 1024;
|
||||||
|
|
||||||
struct statvfs vfs;
|
struct statvfs vfs;
|
||||||
if (statvfs("/", &vfs) == 0)
|
if (statvfs("/", &vfs) == 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user