fix make & mise au propre du projet

This commit is contained in:
Puechberty Arthur
2026-06-26 21:54:33 +02:00
parent 9e02e75a98
commit 290da5ed46
12 changed files with 467 additions and 234 deletions
+12 -12
View File
@@ -5,21 +5,21 @@ SRC = main.c sysinfo.c logos.c config.c renderer.c
all: $(TARGET)
$(TARGET): $(SRC)
$(CC) $(CFLAGS) -o $(TARGET) $(SRC)
$(TARGET): $(SRC)
$(CC) $(CFLAGS) -o $(TARGET) $(SRC)
install: $(TARGET)
mkdir -p $(DESTDIR)/usr/bin
cp $(TARGET) $(DESTDIR)/usr/bin/
mkdir -p $(DESTDIR)/usr/share/optifetch/logos
cp -r logos/*.txt $(DESTDIR)/usr/share/optifetch/logos/ 2>/dev/null || true
mkdir -p $(DESTDIR)/etc
cp optifetch.conf $(DESTDIR)/etc/optifetch.conf 2>/dev/null || touch $(DESTDIR)/etc/optifetch.conf
mkdir -p $(DESTDIR)/usr/bin
cp $(TARGET) $(DESTDIR)/usr/bin/
mkdir -p $(DESTDIR)/usr/share/optifetch/logos
cp -r logos/*.txt $(DESTDIR)/usr/share/optifetch/logos/ 2>/dev/null || true
mkdir -p $(DESTDIR)/etc
cp optifetch.conf $(DESTDIR)/etc/optifetch.conf 2>/dev/null || touch $(DESTDIR)/etc/optifetch.conf
uninstall:
rm -f $(DESTDIR)/usr/bin/$(TARGET)
rm -rf $(DESTDIR)/usr/share/optifetch
rm -f $(DESTDIR)/etc/optifetch.conf
rm -f $(DESTDIR)/usr/bin/$(TARGET)
rm -rf $(DESTDIR)/usr/share/optifetch
rm -f $(DESTDIR)/etc/optifetch.conf
clean:
rm -f $(TARGET)
rm -f $(TARGET)
+4
View File
@@ -19,7 +19,9 @@ Assure-toi d'avoir `gcc` et `make` d'installés, puis exécute :
chmod +x build.sh
./build.sh
```
Ou manuellement :
```bash
make
```
@@ -38,6 +40,7 @@ Au premier lancement, un fichier de configuration est généré dans `~/.config/
Tu peux l'éditer pour modifier l'affichage.
Pour obtenir la liste de toutes les variables et balises disponibles :
```bash
optifetch help
```
@@ -45,6 +48,7 @@ optifetch help
## Logos
Les logos sont stockés dans des fichiers `.txt` situés dans :
1. `./logos/` (dossier local)
2. `/usr/share/optifetch/logos/` (dossier système)
+3 -2
View File
@@ -1,8 +1,9 @@
#!/bin/bash
echo "Compilation d'Optifetch..."
# Compilation directe sans Makefile
gcc -Wall -Wextra -O3 -s -o optifetch main.c sysinfo.c logos.c config.c renderer.c
# On nettoie l'ancienne compilation puis on lance make
make clean
make
if [ $? -eq 0 ]; then
echo "Build réussi ! Exécutable créé : ./optifetch"
+4 -2
View File
@@ -1,8 +1,10 @@
#include "optifetch.h"
void generate_default_config(const char* path) {
void generate_default_config(const char *path)
{
FILE *f = fopen(path, "w");
if (!f) return;
if (!f)
return;
fprintf(f, "{reset}{align:L}{distro_color}{bold}{user}{reset}@{distro_color}{bold}{host}{reset}\n");
fprintf(f, "{align:L}─────────────────────────────\n");
+42 -25
View File
@@ -3,7 +3,8 @@
#define MAX_LOGO_LINES 100
#define MAX_LOGO_WIDTH 512
typedef struct {
typedef struct
{
char id[64];
char lines[MAX_LOGO_LINES][MAX_LOGO_WIDTH];
int count;
@@ -15,49 +16,59 @@ static LogoCache cache_small[32];
static int cache_normal_count = 0;
static int cache_small_count = 0;
static LogoCache* load_logo(const char* os_id, int small) {
LogoCache* cache_arr = small ? cache_small : cache_normal;
int* cache_cnt = small ? &cache_small_count : &cache_normal_count;
static LogoCache *load_logo(const char *os_id, int small)
{
LogoCache *cache_arr = small ? cache_small : cache_normal;
int *cache_cnt = small ? &cache_small_count : &cache_normal_count;
for(int i=0; i<*cache_cnt; i++) {
if(strcmp(cache_arr[i].id, os_id) == 0) return &cache_arr[i];
for (int i = 0; i < *cache_cnt; i++)
{
if (strcmp(cache_arr[i].id, os_id) == 0)
return &cache_arr[i];
}
if(*cache_cnt >= 32) return NULL;
if (*cache_cnt >= 32)
return NULL;
LogoCache* new_cache = &cache_arr[(*cache_cnt)++];
LogoCache *new_cache = &cache_arr[(*cache_cnt)++];
snprintf(new_cache->id, sizeof(new_cache->id), "%s", os_id);
new_cache->count = 0;
new_cache->width = 0;
char path[512];
const char* search_paths[] = {
const char *search_paths[] = {
"logos",
"/usr/share/optifetch/logos",
"/usr/local/share/optifetch/logos",
NULL
};
NULL};
FILE *f = NULL;
for (int i = 0; search_paths[i] != NULL; i++) {
for (int i = 0; search_paths[i] != NULL; i++)
{
snprintf(path, sizeof(path), "%s/%s%s.txt", search_paths[i], os_id, small ? "_small" : "");
f = fopen(path, "r");
if (f) break;
if (f)
break;
}
if (!f) return new_cache;
if (!f)
return new_cache;
char line[MAX_LOGO_WIDTH];
while(fgets(line, sizeof(line), f) && new_cache->count < MAX_LOGO_LINES) {
while (fgets(line, sizeof(line), f) && new_cache->count < MAX_LOGO_LINES)
{
line[strcspn(line, "\n")] = 0;
snprintf(new_cache->lines[new_cache->count], MAX_LOGO_WIDTH, "%s", line);
int w = 0;
// CORRECTION : Utilisation de size_t au lieu de int pour éviter le warning
for(size_t j=0; j<strlen(line); j++) {
if((line[j] & 0xC0) != 0x80) w++;
for (size_t j = 0; j < strlen(line); j++)
{
if ((line[j] & 0xC0) != 0x80)
w++;
}
if(w > new_cache->width) new_cache->width = w;
if (w > new_cache->width)
new_cache->width = w;
new_cache->count++;
}
@@ -65,22 +76,28 @@ static LogoCache* load_logo(const char* os_id, int small) {
return new_cache;
}
void get_logo_line(const char* os_id, int small, int idx, char* out, size_t size) {
LogoCache* c = load_logo(os_id, small);
void get_logo_line(const char *os_id, int small, int idx, char *out, size_t size)
{
LogoCache *c = load_logo(os_id, small);
if(!c) {
if (!c)
{
out[0] = '\0';
return;
}
if(idx < c->count) {
if (idx < c->count)
{
snprintf(out, size, "%s", c->lines[idx]);
} else {
}
else
{
out[0] = '\0';
}
}
int get_logo_height(const char* os_id, int small) {
LogoCache* c = load_logo(os_id, small);
int get_logo_height(const char *os_id, int small)
{
LogoCache *c = load_logo(os_id, small);
return c ? c->count : 0;
}
+25 -12
View File
@@ -1,7 +1,8 @@
#include "optifetch.h"
#include <sys/stat.h> // Nécessaire pour mkdir()
void print_help() {
void print_help()
{
printf("Optifetch - Lightweight system info tool\n\n");
printf("Usage: optifetch [options]\n\n");
printf("Options:\n");
@@ -37,8 +38,10 @@ void print_help() {
printf(" {if:wifi} ... {endif} Show only if connected to wifi\n");
}
int main(int argc, char *argv[]) {
if (argc > 1 && (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) {
int main(int argc, char *argv[])
{
if (argc > 1 && (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0))
{
print_help();
return 0;
}
@@ -50,33 +53,41 @@ int main(int argc, char *argv[]) {
int config_found = 0;
// 1. Cherche dans le dossier local
if (access(config_path, F_OK) == 0) {
if (access(config_path, F_OK) == 0)
{
config_found = 1;
}
// 2. Cherche dans ~/.config/optifetch.conf
if (!config_found) {
if (!config_found)
{
const char *home = getenv("HOME");
if (home) {
if (home)
{
snprintf(config_path, sizeof(config_path), "%s/.config/optifetch.conf", home);
if (access(config_path, F_OK) == 0) {
if (access(config_path, F_OK) == 0)
{
config_found = 1;
}
}
}
// 3. Cherche dans /etc/optifetch.conf
if (!config_found) {
if (!config_found)
{
snprintf(config_path, sizeof(config_path), "/etc/optifetch.conf");
if (access(config_path, F_OK) == 0) {
if (access(config_path, F_OK) == 0)
{
config_found = 1;
}
}
// 4. Si toujours pas trouvé, on le génère
if (!config_found) {
if (!config_found)
{
const char *home = getenv("HOME");
if (home) {
if (home)
{
// S'assurer que le dossier ~/.config existe bien avant de créer le fichier
char config_dir[512];
snprintf(config_dir, sizeof(config_dir), "%s/.config", home);
@@ -84,7 +95,9 @@ int main(int argc, char *argv[]) {
snprintf(config_path, sizeof(config_path), "%s/.config/optifetch.conf", home);
generate_default_config(config_path);
} else {
}
else
{
// Si pas de HOME (ex: environnement très restreint), on crée dans le dossier courant
snprintf(config_path, sizeof(config_path), "optifetch.conf");
generate_default_config(config_path);
+6 -5
View File
@@ -17,7 +17,8 @@
#define BUF_SIZE 4096
typedef struct {
typedef struct
{
char user[128];
char hostname[256];
char os[256];
@@ -60,10 +61,10 @@ typedef struct {
} SysInfo;
void get_info(SysInfo *info);
void get_logo_line(const char* os_id, int small, int idx, char* out, size_t size);
int get_logo_height(const char* os_id, int small);
void generate_default_config(const char* path);
void render_config(const char* path, SysInfo *info);
void get_logo_line(const char *os_id, int small, int idx, char *out, size_t size);
int get_logo_height(const char *os_id, int small);
void generate_default_config(const char *path);
void render_config(const char *path, SysInfo *info);
void print_help();
#endif
+3 -3
View File
@@ -1,15 +1,15 @@
# Maintainer: Ton Nom <ton.email@example.com>
# Maintainer: Arthur P <contact@arthurp.fr>
pkgname=optifetch
pkgver=1.0.0
pkgrel=1
pkgdesc="Lightweight system info tool written in C"
arch=('x86_64')
url="https://github.com/tonpseudo/optifetch"
url="https://github.com/arthur-pbty/optifetch"
license=('MIT')
depends=('glibc')
makedepends=('gcc' 'make')
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
# md5sums=('SKIP') # À remplir si tu release une version fixe
# md5sums=('SKIP') # À remplir si release une version fixe
build() {
cd "$pkgname-$pkgver"
+1 -1
View File
@@ -1,7 +1,7 @@
Source: optifetch
Section: utils
Priority: optional
Maintainer: Ton Nom <ton.email@example.com>
Maintainer: Ton Nom <contact@arthurp.fr>
Build-Depends: gcc, make
Standards-Version: 4.5.0
+1 -1
View File
@@ -3,7 +3,7 @@ Version: 1.0.0
Release: 1%{?dist}
Summary: Lightweight system info tool written in C
License: MIT
URL: https://github.com/tonpseudo/optifetch
URL: https://github.com/arthur-pbty/optifetch
Source0: %{url}/archive/v%{version}/%{name}-%{version}.tar.gz
BuildRequires: gcc
+241 -117
View File
@@ -2,7 +2,8 @@
#define MAX_ALIGN_IDS 32
typedef struct {
typedef struct
{
char id[32];
int max_width;
} AlignID;
@@ -21,213 +22,328 @@ 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 get_align_width(const char* id) {
for(int i=0; i<align_count; i++) {
if(strcmp(align_ids[i].id, id) == 0) return align_ids[i].max_width;
static int get_align_width(const char *id)
{
for (int i = 0; i < align_count; i++)
{
if (strcmp(align_ids[i].id, id) == 0)
return align_ids[i].max_width;
}
return 0;
}
static void set_align_width(const char* id, int width) {
for(int i=0; i<align_count; i++) {
if(strcmp(align_ids[i].id, id) == 0) {
if(width > align_ids[i].max_width) align_ids[i].max_width = width;
static void set_align_width(const char *id, int width)
{
for (int i = 0; i < align_count; i++)
{
if (strcmp(align_ids[i].id, id) == 0)
{
if (width > align_ids[i].max_width)
align_ids[i].max_width = width;
return;
}
}
if(align_count < MAX_ALIGN_IDS) {
if (align_count < MAX_ALIGN_IDS)
{
snprintf(align_ids[align_count].id, sizeof(align_ids[align_count].id), "%.31s", id);
align_ids[align_count].max_width = width;
align_count++;
}
}
static void my_putchar(char c) {
if (skip_output) return;
if (current_pass == 2) {
static void my_putchar(char c)
{
if (skip_output)
return;
if (current_pass == 2)
{
putchar(c);
}
// NOUVEAU : Logique pour ignorer les séquences d'échappement ANSI (\033[...m)
if (c == '\033') {
if (c == '\033')
{
in_ansi_escape = 1;
return;
}
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;
}
return; // On ne compte pas ces caractères dans current_col
}
// Compte les caractères UTF-8 visuels
if ((c & 0xC0) != 0x80) {
if ((c & 0xC0) != 0x80)
{
current_col++;
}
}
static void my_putstr(const char* s) {
for (int i = 0; s[i]; i++) my_putchar(s[i]);
static void my_putstr(const char *s)
{
for (int i = 0; s[i]; i++)
my_putchar(s[i]);
}
static void print_color(const char* params, int is_bg) {
if (skip_output || current_pass == 1) return;
static void print_color(const char *params, int is_bg)
{
if (skip_output || current_pass == 1)
return;
int r, g, b;
if (sscanf(params, "%d,%d,%d", &r, &g, &b) == 3) {
if (sscanf(params, "%d,%d,%d", &r, &g, &b) == 3)
{
printf("\033[%d;2;%d;%d;%dm", is_bg ? 48 : 38, r, g, b);
return;
}
if (strcmp(params, "red") == 0) r=255, g=0, b=0;
else if (strcmp(params, "green") == 0) r=0, g=255, b=0;
else if (strcmp(params, "blue") == 0) r=0, g=0, b=255;
else if (strcmp(params, "yellow") == 0) r=255, g=255, b=0;
else if (strcmp(params, "cyan") == 0) r=0, g=255, b=255;
else if (strcmp(params, "magenta") == 0) r=255, g=0, b=255;
else if (strcmp(params, "white") == 0) r=255, g=255, b=255;
else if (strcmp(params, "black") == 0) r=0, g=0, b=0;
else r=200, g=200, b=200;
if (strcmp(params, "red") == 0)
r = 255, g = 0, b = 0;
else if (strcmp(params, "green") == 0)
r = 0, g = 255, b = 0;
else if (strcmp(params, "blue") == 0)
r = 0, g = 0, b = 255;
else if (strcmp(params, "yellow") == 0)
r = 255, g = 255, b = 0;
else if (strcmp(params, "cyan") == 0)
r = 0, g = 255, b = 255;
else if (strcmp(params, "magenta") == 0)
r = 255, g = 0, b = 255;
else if (strcmp(params, "white") == 0)
r = 255, g = 255, b = 255;
else if (strcmp(params, "black") == 0)
r = 0, g = 0, b = 0;
else
r = 200, g = 200, b = 200;
printf("\033[%d;2;%d;%d;%dm", is_bg ? 48 : 38, r, g, b);
}
static void print_variable(const char* name, SysInfo *info) {
if (skip_output) return;
static void print_variable(const char *name, SysInfo *info)
{
if (skip_output)
return;
char buf[1024];
buf[0] = '\0';
if (strcmp(name, "logo") == 0 || strcmp(name, "small_logo") == 0) {
if (strcmp(name, "logo") == 0 || strcmp(name, "small_logo") == 0)
{
get_logo_line(info->os_id, name[0] == 's', logo_line_idx, buf, sizeof(buf));
}
else if (strcmp(name, "distro_color") == 0) {
if (strlen(info->distro_color) > 0) {
else if (strcmp(name, "distro_color") == 0)
{
if (strlen(info->distro_color) > 0)
{
snprintf(buf, sizeof(buf), "\033[%sm", info->distro_color);
} else {
}
else
{
snprintf(buf, sizeof(buf), "\033[0m");
}
}
else if (strcmp(name, "user") == 0) snprintf(buf, sizeof(buf), "%s", info->user);
else if (strcmp(name, "host") == 0) snprintf(buf, sizeof(buf), "%s", info->hostname);
else if (strcmp(name, "os") == 0) snprintf(buf, sizeof(buf), "%s", info->os);
else if (strcmp(name, "kernel") == 0) snprintf(buf, sizeof(buf), "%s", info->kernel);
else if (strcmp(name, "uptime") == 0) snprintf(buf, sizeof(buf), "%s", info->uptime);
else if (strcmp(name, "arch") == 0) snprintf(buf, sizeof(buf), "%s", info->arch);
else if (strcmp(name, "shell") == 0) snprintf(buf, sizeof(buf), "%s", info->shell);
else if (strcmp(name, "cpu") == 0) snprintf(buf, sizeof(buf), "%s", info->cpu);
else if (strcmp(name, "cores") == 0) snprintf(buf, sizeof(buf), "%ld", info->cores);
else if (strcmp(name, "cpu_max_freq") == 0) snprintf(buf, sizeof(buf), "%ld", info->cpu_max_freq);
else if (strcmp(name, "gpu") == 0) snprintf(buf, sizeof(buf), "%s", info->gpu);
else if (strcmp(name, "de") == 0) snprintf(buf, sizeof(buf), "%s", info->de);
else if (strcmp(name, "wm") == 0) snprintf(buf, sizeof(buf), "%s", info->wm);
else if (strcmp(name, "term") == 0) snprintf(buf, sizeof(buf), "%s", info->term);
else if (strcmp(name, "packages") == 0) snprintf(buf, sizeof(buf), "%s", info->packages);
else if (strcmp(name, "battery") == 0) snprintf(buf, sizeof(buf), "%s", info->battery);
else if (strcmp(name, "bat_status") == 0) snprintf(buf, sizeof(buf), "%s", info->battery_status);
else if (strcmp(name, "date") == 0) snprintf(buf, sizeof(buf), "%s", info->date);
else if (strcmp(name, "time") == 0) snprintf(buf, sizeof(buf), "%s", info->time_str);
else if (strcmp(name, "ip_local") == 0) snprintf(buf, sizeof(buf), "%s", info->ip_local);
else if (strcmp(name, "ip_public") == 0) snprintf(buf, sizeof(buf), "%s", info->ip_public);
else if (strcmp(name, "dns") == 0) snprintf(buf, sizeof(buf), "%s", info->dns);
else if (strcmp(name, "wifi") == 0) snprintf(buf, sizeof(buf), "%s", info->wifi[0] ? info->wifi : "N/A");
else if (strcmp(name, "host_model") == 0) snprintf(buf, sizeof(buf), "%s", info->host_model);
else if (strcmp(name, "display_server") == 0) snprintf(buf, sizeof(buf), "%s", info->display_server);
else if (strcmp(name, "theme") == 0) snprintf(buf, sizeof(buf), "%s", info->theme);
else if (strcmp(name, "font") == 0) snprintf(buf, sizeof(buf), "%s", info->font);
else if (strcmp(name, "locale") == 0) snprintf(buf, sizeof(buf), "%s", info->locale);
else if (strcmp(name, "disk_fs") == 0) snprintf(buf, sizeof(buf), "%s", info->disk_fs);
else if (strncmp(name, "ram", 3) == 0) {
else if (strcmp(name, "user") == 0)
snprintf(buf, sizeof(buf), "%s", info->user);
else if (strcmp(name, "host") == 0)
snprintf(buf, sizeof(buf), "%s", info->hostname);
else if (strcmp(name, "os") == 0)
snprintf(buf, sizeof(buf), "%s", info->os);
else if (strcmp(name, "kernel") == 0)
snprintf(buf, sizeof(buf), "%s", info->kernel);
else if (strcmp(name, "uptime") == 0)
snprintf(buf, sizeof(buf), "%s", info->uptime);
else if (strcmp(name, "arch") == 0)
snprintf(buf, sizeof(buf), "%s", info->arch);
else if (strcmp(name, "shell") == 0)
snprintf(buf, sizeof(buf), "%s", info->shell);
else if (strcmp(name, "cpu") == 0)
snprintf(buf, sizeof(buf), "%s", info->cpu);
else if (strcmp(name, "cores") == 0)
snprintf(buf, sizeof(buf), "%ld", info->cores);
else if (strcmp(name, "cpu_max_freq") == 0)
snprintf(buf, sizeof(buf), "%ld", info->cpu_max_freq);
else if (strcmp(name, "gpu") == 0)
snprintf(buf, sizeof(buf), "%s", info->gpu);
else if (strcmp(name, "de") == 0)
snprintf(buf, sizeof(buf), "%s", info->de);
else if (strcmp(name, "wm") == 0)
snprintf(buf, sizeof(buf), "%s", info->wm);
else if (strcmp(name, "term") == 0)
snprintf(buf, sizeof(buf), "%s", info->term);
else if (strcmp(name, "packages") == 0)
snprintf(buf, sizeof(buf), "%s", info->packages);
else if (strcmp(name, "battery") == 0)
snprintf(buf, sizeof(buf), "%s", info->battery);
else if (strcmp(name, "bat_status") == 0)
snprintf(buf, sizeof(buf), "%s", info->battery_status);
else if (strcmp(name, "date") == 0)
snprintf(buf, sizeof(buf), "%s", info->date);
else if (strcmp(name, "time") == 0)
snprintf(buf, sizeof(buf), "%s", info->time_str);
else if (strcmp(name, "ip_local") == 0)
snprintf(buf, sizeof(buf), "%s", info->ip_local);
else if (strcmp(name, "ip_public") == 0)
snprintf(buf, sizeof(buf), "%s", info->ip_public);
else if (strcmp(name, "dns") == 0)
snprintf(buf, sizeof(buf), "%s", info->dns);
else if (strcmp(name, "wifi") == 0)
snprintf(buf, sizeof(buf), "%s", info->wifi[0] ? info->wifi : "N/A");
else if (strcmp(name, "host_model") == 0)
snprintf(buf, sizeof(buf), "%s", info->host_model);
else if (strcmp(name, "display_server") == 0)
snprintf(buf, sizeof(buf), "%s", info->display_server);
else if (strcmp(name, "theme") == 0)
snprintf(buf, sizeof(buf), "%s", info->theme);
else if (strcmp(name, "font") == 0)
snprintf(buf, sizeof(buf), "%s", info->font);
else if (strcmp(name, "locale") == 0)
snprintf(buf, sizeof(buf), "%s", info->locale);
else if (strcmp(name, "disk_fs") == 0)
snprintf(buf, sizeof(buf), "%s", info->disk_fs);
else if (strncmp(name, "ram", 3) == 0)
{
char unit[128] = "mb";
if (strlen(name) > 4 && name[3] == ':') snprintf(unit, sizeof(unit), "%s", name + 4);
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);
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 snprintf(buf, sizeof(buf), "%luMB / %luMB", info->ram_used_mb, info->ram_total_mb);
if (strlen(name) > 4 && name[3] == ':')
snprintf(unit, sizeof(unit), "%s", name + 4);
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);
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
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";
if (strlen(name) > 5 && name[4] == ':') snprintf(unit, sizeof(unit), "%s", name + 5);
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);
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 snprintf(buf, sizeof(buf), "%luMB / %luMB", info->swap_used_mb, info->swap_total_mb);
if (strlen(name) > 5 && name[4] == ':')
snprintf(unit, sizeof(unit), "%s", name + 5);
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);
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
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";
if (strlen(name) > 5 && name[4] == ':') snprintf(unit, sizeof(unit), "%s", name + 5);
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);
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 snprintf(buf, sizeof(buf), "%luMB / %luMB", info->disk_used_mb, info->disk_total_mb);
if (strlen(name) > 5 && name[4] == ':')
snprintf(unit, sizeof(unit), "%s", name + 5);
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);
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
snprintf(buf, sizeof(buf), "%luMB / %luMB", info->disk_used_mb, info->disk_total_mb);
}
else {
else
{
snprintf(buf, sizeof(buf), "{%s}", name);
}
my_putstr(buf);
}
static void process_line(const char* line, SysInfo *info) {
static void process_line(const char *line, SysInfo *info)
{
int i = 0;
current_col = 0;
skip_output = 0;
in_ansi_escape = 0; // NOUVEAU : Réinitialiser l'état ANSI à chaque ligne
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)
{
logo_line_idx = 0;
}
while (line[i] != '\0' && line[i] != '\n') {
if (line[i] == '{') {
while (line[i] != '\0' && line[i] != '\n')
{
if (line[i] == '{')
{
int j = i + 1;
char token[128];
int k = 0;
while (line[j] != '}' && line[j] != '\0' && line[j] != '\n' && k < 127) {
while (line[j] != '}' && line[j] != '\0' && line[j] != '\n' && k < 127)
{
token[k++] = line[j++];
}
token[k] = '\0';
if (line[j] == '}') {
if (line[j] == '}')
{
i = j + 1;
if (strcmp(token, "if:laptop") == 0) skip_output = (strcmp(info->battery, "N/A") == 0);
else if (strcmp(token, "if:gpu") == 0) skip_output = (strlen(info->gpu) == 0);
else if (strcmp(token, "if:swap") == 0) skip_output = (info->swap_total_mb == 0);
else if (strcmp(token, "if:wifi") == 0) skip_output = (strlen(info->wifi) == 0);
else if (strcmp(token, "endif") == 0) skip_output = 0;
if (strcmp(token, "if:laptop") == 0)
skip_output = (strcmp(info->battery, "N/A") == 0);
else if (strcmp(token, "if:gpu") == 0)
skip_output = (strlen(info->gpu) == 0);
else if (strcmp(token, "if:swap") == 0)
skip_output = (info->swap_total_mb == 0);
else if (strcmp(token, "if:wifi") == 0)
skip_output = (strlen(info->wifi) == 0);
else if (strcmp(token, "endif") == 0)
skip_output = 0;
else if (strncmp(token, "align:", 6) == 0) {
if (!skip_output) {
if (current_pass == 1) {
else if (strncmp(token, "align:", 6) == 0)
{
if (!skip_output)
{
if (current_pass == 1)
{
set_align_width(token + 6, current_col);
}
int target = get_align_width(token + 6);
while (current_col < target) my_putchar(' ');
while (current_col < target)
my_putchar(' ');
}
}
else if (strncmp(token, "fg:", 3) == 0) print_color(token + 3, 0);
else if (strncmp(token, "bg:", 3) == 0) print_color(token + 3, 1);
else if (strcmp(token, "reset") == 0) { if(!skip_output && current_pass == 2) printf("\033[0m"); }
else if (strcmp(token, "bold") == 0) { if(!skip_output && current_pass == 2) printf("\033[1m"); }
else print_variable(token, info);
} else {
else if (strncmp(token, "fg:", 3) == 0)
print_color(token + 3, 0);
else if (strncmp(token, "bg:", 3) == 0)
print_color(token + 3, 1);
else if (strcmp(token, "reset") == 0)
{
if (!skip_output && current_pass == 2)
printf("\033[0m");
}
else if (strcmp(token, "bold") == 0)
{
if (!skip_output && current_pass == 2)
printf("\033[1m");
}
else
print_variable(token, info);
}
else
{
my_putchar(line[i++]);
}
} else {
}
else
{
my_putchar(line[i++]);
}
}
if (current_pass == 2 && !skip_output) putchar('\n');
if (current_pass == 2 && !skip_output)
putchar('\n');
current_line_num++;
prev_line_had_logo = has_logo;
if (has_logo) logo_line_idx++;
if (has_logo)
logo_line_idx++;
}
void render_config(const char* path, SysInfo *info) {
void render_config(const char *path, SysInfo *info)
{
FILE *f = fopen(path, "r");
if (!f) return;
if (!f)
return;
static char lines[200][1024];
int line_count = 0;
while (fgets(lines[line_count], sizeof(lines[line_count]), f) && line_count < 200) {
while (fgets(lines[line_count], sizeof(lines[line_count]), f) && line_count < 200)
{
line_count++;
}
fclose(f);
@@ -235,20 +351,26 @@ void render_config(const char* path, SysInfo *info) {
int logo_h = 0;
char logo_token[20] = "";
for (int i = 0; i < line_count; i++) {
if (strstr(lines[i], "{logo}")) {
for (int i = 0; i < line_count; i++)
{
if (strstr(lines[i], "{logo}"))
{
snprintf(logo_token, sizeof(logo_token), "{logo}");
logo_h = get_logo_height(info->os_id, 0);
}
if (strstr(lines[i], "{small_logo}")) {
if (strstr(lines[i], "{small_logo}"))
{
snprintf(logo_token, sizeof(logo_token), "{small_logo}");
int sh = get_logo_height(info->os_id, 1);
if (sh > logo_h) logo_h = sh;
if (sh > logo_h)
logo_h = sh;
}
}
if (strlen(logo_token) > 0) {
while (line_count < logo_h && line_count < 200) {
if (strlen(logo_token) > 0)
{
while (line_count < logo_h && line_count < 200)
{
snprintf(lines[line_count++], 1024, "%s{align:L} \n", logo_token);
}
}
@@ -259,7 +381,8 @@ void render_config(const char* path, SysInfo *info) {
current_line_num = 0;
logo_line_idx = 0;
prev_line_had_logo = 0;
for (int i = 0; i < line_count; i++) {
for (int i = 0; i < line_count; i++)
{
process_line(lines[i], info);
}
@@ -267,7 +390,8 @@ void render_config(const char* path, SysInfo *info) {
current_line_num = 0;
logo_line_idx = 0;
prev_line_had_logo = 0;
for (int i = 0; i < line_count; i++) {
for (int i = 0; i < line_count; i++)
{
process_line(lines[i], info);
}
}
+106 -35
View File
@@ -1,21 +1,31 @@
#include "optifetch.h"
static void read_file(const char *path, char *buf, size_t size) {
static void read_file(const char *path, char *buf, size_t size)
{
FILE *f = fopen(path, "r");
if (!f) { if(size>0) buf[0] = '\0'; return; }
if (!f)
{
if (size > 0)
buf[0] = '\0';
return;
}
size_t n = fread(buf, 1, size - 1, f);
buf[n] = '\0';
fclose(f);
}
static void extract_value(const char *buf, const char *key, char *out, size_t size) {
static void extract_value(const char *buf, const char *key, char *out, size_t size)
{
out[0] = '\0';
const char *p = strstr(buf, key);
if (p) {
if (p)
{
p += strlen(key);
while (*p == ' ' || *p == '\t' || *p == ':' || *p == '=' || *p == '"') p++;
while (*p == ' ' || *p == '\t' || *p == ':' || *p == '=' || *p == '"')
p++;
size_t i = 0;
while (p[i] && p[i] != '\n' && p[i] != '"' && i < size - 1) {
while (p[i] && p[i] != '\n' && p[i] != '"' && i < size - 1)
{
out[i] = p[i];
i++;
}
@@ -23,11 +33,13 @@ static void extract_value(const char *buf, const char *key, char *out, size_t si
}
}
static void clean_newline(char *str) {
static void clean_newline(char *str)
{
str[strcspn(str, "\n")] = 0;
}
void get_info(SysInfo *info) {
void get_info(SysInfo *info)
{
struct passwd *pw = getpwuid(getuid());
snprintf(info->user, sizeof(info->user), "%s", pw ? pw->pw_name : "unknown");
gethostname(info->hostname, sizeof(info->hostname));
@@ -66,7 +78,8 @@ void get_info(SysInfo *info) {
read_file("/sys/class/dmi/id/product_name", info->host_model, sizeof(info->host_model));
clean_newline(info->host_model);
if (strlen(info->host_model) == 0) snprintf(info->host_model, sizeof(info->host_model), "Unknown");
if (strlen(info->host_model) == 0)
snprintf(info->host_model, sizeof(info->host_model), "Unknown");
info->ram_total_mb = s_info.totalram * s_info.mem_unit / (1024 * 1024);
info->ram_used_mb = (s_info.totalram - s_info.freeram) * s_info.mem_unit / (1024 * 1024);
@@ -74,14 +87,23 @@ void get_info(SysInfo *info) {
info->swap_used_mb = (s_info.totalswap - s_info.freeswap) * s_info.mem_unit / (1024 * 1024);
struct statvfs vfs;
if (statvfs("/", &vfs) == 0) {
if (statvfs("/", &vfs) == 0)
{
info->disk_total_mb = (vfs.f_blocks * vfs.f_frsize) / (1024 * 1024);
info->disk_used_mb = info->disk_total_mb - ((vfs.f_bfree * vfs.f_frsize) / (1024 * 1024));
} else {
info->disk_total_mb = 0; info->disk_used_mb = 0;
}
else
{
info->disk_total_mb = 0;
info->disk_used_mb = 0;
}
FILE *fp_fs = popen("findmnt -no FSTYPE / 2>/dev/null", "r");
if (fp_fs) { if (fgets(info->disk_fs, sizeof(info->disk_fs), fp_fs)) clean_newline(info->disk_fs); pclose(fp_fs); }
if (fp_fs)
{
if (fgets(info->disk_fs, sizeof(info->disk_fs), fp_fs))
clean_newline(info->disk_fs);
pclose(fp_fs);
}
const char *de = getenv("XDG_CURRENT_DESKTOP");
const char *wm = getenv("DESKTOP_SESSION");
@@ -93,42 +115,76 @@ void get_info(SysInfo *info) {
info->gpu[0] = '\0';
FILE *fdrm = popen("cat /sys/class/drm/card*/device/uevent 2>/dev/null | grep DRIVER | head -n 1", "r");
if (fdrm) {
if (fdrm)
{
char drmbuf[256];
if (fgets(drmbuf, sizeof(drmbuf), fdrm)) {
if (fgets(drmbuf, sizeof(drmbuf), fdrm))
{
extract_value(drmbuf, "DRIVER", info->gpu, sizeof(info->gpu));
if(info->gpu[0] >= 'a' && info->gpu[0] <= 'z') info->gpu[0] -= 32;
if (info->gpu[0] >= 'a' && info->gpu[0] <= 'z')
info->gpu[0] -= 32;
}
pclose(fdrm);
}
FILE *fp_th = popen("gsettings get org.gnome.desktop.interface gtk-theme 2>/dev/null | tr -d \"'\"", "r");
if (fp_th) { if (fgets(info->theme, sizeof(info->theme), fp_th)) clean_newline(info->theme); pclose(fp_th); }
if (fp_th)
{
if (fgets(info->theme, sizeof(info->theme), fp_th))
clean_newline(info->theme);
pclose(fp_th);
}
FILE *fp_fn = popen("gsettings get org.gnome.desktop.interface font-name 2>/dev/null | tr -d \"'\"", "r");
if (fp_fn) { if (fgets(info->font, sizeof(info->font), fp_fn)) clean_newline(info->font); pclose(fp_fn); }
if (fp_fn)
{
if (fgets(info->font, sizeof(info->font), fp_fn))
clean_newline(info->font);
pclose(fp_fn);
}
int pkg_count = 0;
DIR *d; struct dirent *dir;
DIR *d;
struct dirent *dir;
d = opendir("/var/lib/pacman/local");
if (d) { while ((dir = readdir(d)) != NULL) if (dir->d_type == DT_DIR && dir->d_name[0] != '.') pkg_count++; closedir(d); }
else {
if (d)
{
while ((dir = readdir(d)) != NULL)
if (dir->d_type == DT_DIR && dir->d_name[0] != '.')
pkg_count++;
closedir(d);
}
else
{
FILE *fp = popen("ls /var/lib/dpkg/info 2>/dev/null | grep -c '.list$' ; rpm -qa 2>/dev/null | wc -l", "r");
if(fp) { char out[32]; if(fgets(out, sizeof(out), fp)) pkg_count = atoi(out); pclose(fp); }
if (fp)
{
char out[32];
if (fgets(out, sizeof(out), fp))
pkg_count = atoi(out);
pclose(fp);
}
}
snprintf(info->packages, sizeof(info->packages), "%d", pkg_count);
char bat_path[256] = "";
if (access("/sys/class/power_supply/BAT0/capacity", F_OK) == 0) snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT0");
else if (access("/sys/class/power_supply/BAT1/capacity", F_OK) == 0) snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT1");
if (strlen(bat_path) > 0) {
if (access("/sys/class/power_supply/BAT0/capacity", F_OK) == 0)
snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT0");
else if (access("/sys/class/power_supply/BAT1/capacity", F_OK) == 0)
snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT1");
if (strlen(bat_path) > 0)
{
char cap_path[512], stat_path[512];
snprintf(cap_path, sizeof(cap_path), "%s/capacity", bat_path);
snprintf(stat_path, sizeof(stat_path), "%s/status", bat_path);
read_file(cap_path, buf, sizeof(buf)); clean_newline(buf);
read_file(cap_path, buf, sizeof(buf));
clean_newline(buf);
snprintf(info->battery, sizeof(info->battery), "%s%%", buf);
read_file(stat_path, buf, sizeof(buf)); clean_newline(buf);
read_file(stat_path, buf, sizeof(buf));
clean_newline(buf);
snprintf(info->battery_status, sizeof(info->battery_status), "%s", buf);
} else {
}
else
{
snprintf(info->battery, sizeof(info->battery), "N/A");
snprintf(info->battery_status, sizeof(info->battery_status), "N/A");
}
@@ -140,14 +196,19 @@ void get_info(SysInfo *info) {
info->ip_local[0] = '\0';
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) == 0) {
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) continue;
if (ifa->ifa_addr->sa_family == AF_INET) {
if (getifaddrs(&ifaddr) == 0)
{
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
if (ifa->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *sa = (struct sockaddr_in *)ifa->ifa_addr;
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &sa->sin_addr, ip, INET_ADDRSTRLEN);
if (strcmp(ip, "127.0.0.1") != 0) {
if (strcmp(ip, "127.0.0.1") != 0)
{
snprintf(info->ip_local, sizeof(info->ip_local), "%s (%s)", ip, ifa->ifa_name);
break;
}
@@ -161,9 +222,19 @@ void get_info(SysInfo *info) {
info->wifi[0] = '\0';
FILE *fp_wf = popen("iwgetid -r 2>/dev/null", "r");
if (fp_wf) { if (fgets(info->wifi, sizeof(info->wifi), fp_wf)) clean_newline(info->wifi); pclose(fp_wf); }
if (fp_wf)
{
if (fgets(info->wifi, sizeof(info->wifi), fp_wf))
clean_newline(info->wifi);
pclose(fp_wf);
}
info->ip_public[0] = '\0';
FILE *fp_ip = popen("curl -s --max-time 2 ifconfig.me 2>/dev/null", "r");
if (fp_ip) { if (fgets(info->ip_public, sizeof(info->ip_public), fp_ip)) clean_newline(info->ip_public); pclose(fp_ip); }
if (fp_ip)
{
if (fgets(info->ip_public, sizeof(info->ip_public), fp_ip))
clean_newline(info->ip_public);
pclose(fp_ip);
}
}