This commit is contained in:
Your Name 2024-06-07 14:41:51 +03:00
parent 76e0dcb8ed
commit 4fdee929ee
10 changed files with 149 additions and 329 deletions

View File

@ -2,32 +2,31 @@
#define _CONFIG_H
#include "fetch.h"
typedef struct {
char *title;
int (*func)(const char *title, const FETCH fetch);
} PARAMETR;
#include "structs.h"
/* Config, EDIT THERE */
#define COLORS " \033[31m(>_<) \033[32m(OwO) \033[33m($w$) \033[34m(;~;)\033[0m"
#define FONT_COLOR "\033[0m"
#define COLORS " \033[31m<> \033[32m<> \033[33m<> \033[34m<> \033[35m<> \033[36m<>\033[0m"
#define FONT_COLOR "\033[1;37m"
#define BAR_WIDTH 1
#define BATT_NAME NULL
#ifdef _MAIN_C
#define ON_ERROR_STR "none"
#ifdef _MAIN_H
PARAMETR CONFIG[] = {
{" -> ", PrintOs},
{" ", Blank},
{" <- ", GetUptime},
{" ", Blank},
{" -> ", GetMem},
{" ", Blank},
{" <- ", GetArch},
{" ", Blank},
{COLORS, Blank}
{PrintOs, {.str0=" os ", .str1=NULL}},
{GetKernel, {.str0=" kr ", .str1=NULL}},
{GetUser, {.str0=" usr ", .str1=NULL}},
{GetUptime, {.str0=" upt ", .str1=NULL}},
{GetArch, {.str0=" arch ", .str1=NULL}},
{GetShell, {.str0="shell ", .str1=NULL}},
{GetPkg, {.str0=" pkgs ", .str1=NULL}},
{GetMem, {.str0=" mem ", .str1=NULL}},
{GetModel, {.str0="model ", .str1=NULL}},
{GetHostname, {.str0=" host ", .str1=NULL}},
{GetAVG, {.str0=" AVG ", .str1=NULL}},
{Blank, {.str0=" ", .str1=NULL}},
{Execute, {.str0=" Id's ", .str1=NULL, .str2="id -gn"}},
{Blank, {.str0=COLORS, .str1=NULL}}
};
#endif
#endif

View File

@ -1,53 +1,47 @@
#ifndef _FETCH_H
#define _FETCH_H
#include <time.h>
#include <sys/utsname.h>
#include "structs.h"
#ifdef _FETCH_C
#include <time.h>
#if defined(CLOCK_BOOTTIME)
#define CLOCK CLOCK_BOOTTIME
#elif defined(CLOCK_UPTIME)
#define CLOCK CLOCK_UPTIME
#elif defined(__APPLE__)
#define CLOCK CLOCK_MONOTONIC
#endif
#if defined(CLOCK_BOOTTIME)
#define CLOCK CLOCK_BOOTTIME
#elif defined(CLOCK_UPTIME)
#define CLOCK CLOCK_UPTIME
#elif defined(__APPLE__)
#define CLOCK CLOCK_MONOTONIC
#define MODEL_BUFF_SIZE 512
static const char *MODELS[] = {
"/sys/devices/virtual/dmi/id/product_name",
"/sys/devices/virtual/dmi/id/product_version",
"/sys/firmware/devicetree/base/model"
};
#endif
#define OS_SIZE 128
#define MODEL_BUFF_SIZE 512
static const char *MODELS[] = {
"/sys/devices/virtual/dmi/id/product_name",
"/sys/devices/virtual/dmi/id/product_version",
"/sys/firmware/devicetree/base/model"
};
typedef struct {
char os_name[OS_SIZE + 1];
struct utsname uts;
char *pkg_cmd;
char *color;
char *font_color;
size_t logo_size;
char **logo;
} FETCH;
FETCH Init(void);
#if defined(_FETCH_C) || defined(_MAIN_C)
int GetOs(char *os_name, size_t len);
void GetArt(FETCH *fetch, int flag);
int GetKernel(const char *title, const FETCH fetch);
int PrintOs(const char *title, const FETCH fetch);
int Blank(const char *title, const FETCH fetch);
int GetUser(const char *title, const FETCH fetch);
int GetUptime(const char *title, const FETCH fetch);
int GetArch(const char *title, const FETCH fetch);
int GetShell(const char *title, const FETCH fetch);
int GetPkg(const char *title, const FETCH fetch);
int GetMem(const char *title, const FETCH fetch);
int GetModel(const char *title, const FETCH fetch);
int GetHostname(const char *title, const FETCH fetch);
int GetAVG(const char *title, const FETCH fetch);
int GetBattery(const char *title, const FETCH fetch);
int Execute(const char *title, const FETCH fetch);
int eBlank(const char *title, const FETCH fetch);
FETCH Init(void);
#endif
#if defined(_CONFIG_C) || defined(_MAIN_C)
int GetKernel(const struct par par, const FETCH fetch);
int PrintOs(const struct par par, const FETCH fetch);
int Blank(const struct par par, const FETCH fetch);
int GetUser(const struct par par, const FETCH fetch);
int GetUptime(const struct par par, const FETCH fetch);
int GetArch(const struct par par, const FETCH fetch);
int GetShell(const struct par par, const FETCH fetch);
int GetPkg(const struct par par, const FETCH fetch);
int GetMem(const struct par par, const FETCH fetch);
int GetModel(const struct par par, const FETCH fetch);
int GetHostname(const struct par par, const FETCH fetch);
int GetAVG(const struct par par, const FETCH fetch);
int Execute(const struct par par, const FETCH fetch);
int eBlank(const struct par par, const FETCH fetch);
#endif
#endif

View File

@ -69,14 +69,23 @@ char *OpenBSD[] = {
" "
};
char *PlainOs[] = {
" ",
" (\\(\\ ",
" j . .) ",
" | ° ! ",
" ? ; ",
" c?\".UJ ",
" "
char *Manana[] = {
"\033[1;32m -: \033[0m",
"\033[1;32m =* .*: \033[0m",
"\033[1;32m =*=. \033[1;31m:+* =\033[1;32m *** \033[0m",
"\033[1;32m -*+.- \033[1;31m*****#\033[1;32m =*-*- \033[0m",
"\033[1;32m .**:.= \033[1;31m****-\033[1;32m .*:.** \033[0m",
"\033[1;32m +** :* \033[1;31m==:\033[1;32m *+ .**: \033[0m",
"\033[1;32m :**+ -* =*: :**= \033[0m",
"\033[1;32m +**: -*. .*+ .**= \033[0m",
"\033[1;32m .**+ -*- +*. .**= \033[0m",
"\033[1;32m -**: -*= =*- +*= \033[0m",
"\033[1;32m +*+ :*+ .*= =*- \033[0m",
"\033[1;32m .**. ** *= :*: \033[0m",
"\033[1;32m -*: :*. == *. \033[0m",
"\033[1;32m +- +- := +. \033[0m",
"\033[1;32m .- .= .- : \033[0m",
"\033[1;32m :.. \033[0m"
};
char *Ubuntu[] = {

30
include/structs.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef _STRUCTS_H
#define _STRUCTS_H
#include <sys/utsname.h>
#define OS_SIZE 128
typedef struct {
char os_name[OS_SIZE + 1];
struct utsname uts;
char *pkg_cmd;
char *color;
size_t logo_size;
char **logo;
} FETCH;
struct par {
char *str0;
char *str1;
char *str2;
int error_flag;
};
typedef struct {
int (*func)(const struct par par, const FETCH fetch);
struct par par;
} PARAMETR;
#endif

View File

@ -1,3 +1,4 @@
#define _FETCH_C
#include <pwd.h>
#include <stdio.h>
#include <errno.h>
@ -8,6 +9,7 @@
#include <limits.h>
#include <sys/utsname.h>
#include "fetch.h"
#include "structs.h"
#include "config.h"
#include "logo.h"
@ -24,7 +26,6 @@ FETCH Init(void) {
if (GetOs(fetch.os_name, sizeof(fetch.os_name)))
snprintf(fetch.os_name, sizeof(fetch.os_name), "%s", fetch.uts.sysname);
fetch.font_color = FONT_COLOR;
GetArt(&fetch, 1);
return fetch;
}
@ -77,18 +78,15 @@ void GetArt(FETCH *fetch, int flag) {
else if (strstr(fetch->os_name, "Arch") || strstr(fetch->os_name, "Artix"))
SetArt(fetch, sizeof(Arch), "pacman -Qq | wc -l", Arch, "\033[0;34m", flag);
else if (strstr(fetch->os_name, "PlainOs"))
SetArt(fetch, sizeof(PlainOs), NULL, PlainOs, "\033[37m", flag);
else if (strstr(fetch->os_name, "OpenBSD"))
SetArt(fetch, sizeof(OpenBSD), "/bin/ls -1 /var/db/pkg/ | wc -l | tr -d ' '", OpenBSD, "\033[1;33m", flag);
else if (strstr(fetch->os_name, "Ubuntu"))
SetArt(fetch, sizeof(Ubuntu), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Ubuntu, "\033[1;33m", flag);
else if (strstr(fetch->os_name, "Mint"))
SetArt(fetch, sizeof(Lmint), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Lmint, "\033[1;32m", flag);
else if (strstr(fetch->os_name, "Manana"))
SetArt(fetch, sizeof(Manana), "pacman -Qq | wc -l", Manana, "\033[1;32m", flag);
else if (!chdir("/system") && !strcmp(fetch->os_name, "Linux"))
SetArt(fetch, sizeof(Android), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Android, "\033[32m", flag);
@ -96,33 +94,32 @@ void GetArt(FETCH *fetch, int flag) {
SetArt(fetch, sizeof(Unknow), NULL, Unknow, "\033[1;36m", flag);
}
int GetKernel(const char *title, const FETCH fetch) {
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.release);
int GetKernel(struct par par, const FETCH fetch) {
printf("%s%s", FONT_COLOR, fetch.uts.release);
return 0;
}
int PrintOs(const char *title, const FETCH fetch) {
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.os_name);
int PrintOs(struct par par, const FETCH fetch) {
printf("%s%s", FONT_COLOR, fetch.os_name);
return 0;
}
int Blank(const char *title, const FETCH fetch) {
printf("%s%s%s", fetch.color, title, fetch.font_color);
int Blank(struct par par, const FETCH fetch) {
return 0;
}
int GetUser(const char *title, const FETCH fetch) {
int GetUser(struct par par, const FETCH fetch) {
UNUSED(fetch);
struct passwd *pw = getpwuid(geteuid());
if (pw == 0)
return 1;
printf("%s%s%s%s", fetch.color, title, fetch.font_color, pw->pw_name);
printf("%s%s", FONT_COLOR, pw->pw_name);
return 0;
}
int GetUptime(const char *title, const FETCH fetch) {
int GetUptime(struct par par, const FETCH fetch) {
#ifdef CLOCK
struct timespec uptime;
if (clock_gettime(CLOCK, &uptime) == -1)
@ -133,10 +130,10 @@ int GetUptime(const char *title, const FETCH fetch) {
int mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60);
if (days > 0)
printf("%s%s%s%d days", fetch.color, title, fetch.font_color, days);
printf("%s%d days", FONT_COLOR, days);
else
printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins);
printf("%s%dh %dm", FONT_COLOR, hours, mins);
return 0;
#endif
@ -144,12 +141,12 @@ int GetUptime(const char *title, const FETCH fetch) {
return 1;
}
int GetArch(const char *title, const FETCH fetch) {
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.machine);
int GetArch(struct par par, const FETCH fetch) {
printf("%s%s", FONT_COLOR, fetch.uts.machine);
return 0;
}
int GetShell(const char *title, const FETCH fetch) {
int GetShell(struct par par, const FETCH fetch) {
char *shell = getenv("SHELL");
if (shell == NULL)
return 1;
@ -158,11 +155,11 @@ int GetShell(const char *title, const FETCH fetch) {
if (splt == NULL)
return 1;
printf("%s%s%s%s", fetch.color, title, fetch.font_color, splt + 1);
printf("%s%s", FONT_COLOR, splt + 1);
return 0;
}
int GetPkg(const char *title, const FETCH fetch) {
int GetPkg(struct par par, const FETCH fetch) {
if (fetch.pkg_cmd == NULL)
return 1;
@ -170,7 +167,7 @@ int GetPkg(const char *title, const FETCH fetch) {
if (fp == NULL)
return 1;
printf("%s%s%s", fetch.color, title, fetch.font_color);
printf("%s", FONT_COLOR);
int ch;
while ((ch = getc(fp))) {
@ -184,22 +181,22 @@ int GetPkg(const char *title, const FETCH fetch) {
return 0;
}
int GetMem(const char *title, const FETCH fetch) {
int GetMem(struct par par, const FETCH fetch) {
FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL)
return 1;
off_t free = 0, total = 0, cached = 0, buffer = 0, unused = 0;
if (fscanf(fp, "MemTotal: %ju kB\nMemFree: %ju kB\nMemAvailable: %ju kB\nBuffers: %ju kB\nCached: %ju kB", &total, &free, &unused, &buffer, &cached) < 0)
if (fscanf(fp, "MemTotal: %jd kB\nMemFree: %jd kB\nMemAvailable: %jd kB\nBuffers: %jd kB\nCached: %jd kB", &total, &free, &unused, &buffer, &cached) < 0)
return 1;
fclose(fp);
printf("%s%s%s%lumb / %lumb", fetch.color, title, fetch.font_color, (uintmax_t)(total - (cached + free + buffer)) / 1024, (uintmax_t)total / 1024);
printf("%s%lumb / %lumb", FONT_COLOR, (uintmax_t)(total - (cached + free + buffer)) / 1024, (uintmax_t)total / 1024);
return 0;
}
int GetModel(const char *title, const FETCH fetch) {
int GetModel(struct par par, const FETCH fetch) {
FILE *fp = NULL;
for (size_t i = 0; i < sizeof(MODELS) / sizeof(char *); i++) {
fp = fopen(MODELS[i], "r");
@ -219,30 +216,31 @@ int GetModel(const char *title, const FETCH fetch) {
if (ptr != NULL)
*ptr = '\0';
printf("%s%s%s%.25s", fetch.color, title, fetch.font_color, model_buffer);
printf("%s%.25s", FONT_COLOR, model_buffer);
}
fclose(fp);
return 0;
}
int GetHostname(const char *title, const FETCH fetch) {
int GetHostname(struct par par, const FETCH fetch) {
char hostname[HOST_NAME_MAX + 1];
if (gethostname(hostname, sizeof(hostname)) < 0)
return 1;
printf("%s%s%s%s", fetch.color, title, fetch.font_color, hostname);
printf("%s%s", FONT_COLOR, hostname);
return 0;
}
int GetAVG(const char *title, const FETCH fetch) {
int GetAVG(struct par par, const FETCH fetch) {
#ifndef __ANDROID__
double avg[3] = {0, 0, 0};
if (getloadavg(avg, sizeof(avg) / sizeof(avg[0])) < 0)
return 1;
printf("%s%s%s%.2f %.2f %.2f", fetch.color, title, fetch.font_color, avg[0], avg[1], avg[2]);
printf("%s%.2f %.2f %.2f", FONT_COLOR, avg[0], avg[1], avg[2]);
return 0;
#else
@ -251,67 +249,15 @@ int GetAVG(const char *title, const FETCH fetch) {
#endif
}
void DrawBar(int cur, int max) {
int bar = max / BAR_WIDTH;
#ifdef FIRA_CODE
char *start = "";
if (cur * bar > 0)
start = "";
char *end = "";
if (cur < max)
end = "";
char *bar_filled = "";
char *bar_void = "";
#else
char *start = "[";
char *end = "]";
char *bar_filled = "#";
char *bar_void = ".";
#endif
printf("%s", start);
for (int i = 0; i < BAR_WIDTH; i++) {
if (i * bar < cur)
printf("%s", bar_filled);
else
printf("%s", bar_void);
}
printf("%s", end);
}
int GetBattery(const char *title, const FETCH fetch) {
if (BATT_NAME == NULL)
int Execute(struct par par, const FETCH fetch) {
if (par.str2 == NULL)
return 1;
FILE *fp = fopen(BATT_NAME, "r");
FILE *fp = popen(par.str2, "r");
if (fp == NULL)
return 1;
int capacity = 0;
if (fscanf(fp, "%d", &capacity) < 0)
return 1;
fclose(fp);
printf("%s%s%s", fetch.color, title, fetch.font_color);
DrawBar(capacity, 100);
return 0;
}
int Execute(const char *title, const FETCH fetch) {
FILE *fp = popen(title, "r");
if (fp == NULL)
return 1;
printf("%s", fetch.font_color);
printf("%s", FONT_COLOR);
int c;
while ((c = getc(fp))) {
@ -324,9 +270,3 @@ int Execute(const char *title, const FETCH fetch) {
pclose(fp);
return 0;
}
int eBlank(const char *title, const FETCH fetch) {
printf("%s%s%s", fetch.color, title, fetch.font_color);
return 1;
}

View File

@ -1,8 +1,8 @@
#define _MAIN_H
#define _MAIN_C
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "fetch.h"
#include "structs.h"
#include "config.h"
int main(int argc, char **argv) {
@ -37,23 +37,27 @@ int main(int argc, char **argv) {
size_t j = 0;
for (size_t i = 0; i < sizeof(CONFIG) / sizeof(PARAMETR); i++) {
/* Print ascii logo */
if (j < fetch.logo_size && !flag && !l_flag)
if (j < fetch.logo_size && !l_flag)
printf("%s", fetch.logo[j]);
else if (j >= fetch.logo_size && !flag && !l_flag)
else if (j >= fetch.logo_size && !l_flag)
printf("%s", fetch.logo[fetch.logo_size - 1]);
/* Function exec */
if (CONFIG[i].func(CONFIG[i].title, fetch)) {
printf("%s%s", fetch.color, CONFIG[i].par.str0);
if (CONFIG[i].func(CONFIG[i].par, fetch)) {
flag = 1;
printf("\033[0m");
printf("%s%s", FONT_COLOR, ON_ERROR_STR);
}
else {
flag = 0;
j++;
printf("\033[0m\n");
if (CONFIG[i].par.str1 != NULL)
printf(CONFIG[i].par.str1, fetch.color);
}
j++;
printf("\033[0m\n");
}
for (; j < fetch.logo_size; j++)

View File

@ -1,42 +0,0 @@
#ifndef _CONFIG_H
#define _CONFIG_H
#include "fetch.h"
typedef struct {
char *title;
int (*func)(const char *title, const FETCH fetch);
} PARAMETR;
/* Config, EDIT THERE */
#define COLORS "\033[31m<> \033[32m<> \033[33m<> \033[34m<> \033[35m<> \033[36m<>\033[0m"
#define FONT_COLOR "\033[1;37m"
#define BAR_WIDTH 10
/* Uncomment if you use firacode font */
/* #define FIRA_CODE */
/* Full path to capacity file */
/* Example: /sys/class/power_supply/battery/capacity */
#define BATT_NAME NULL
#ifdef _MAIN_H
PARAMETR CONFIG[] = {
{" os ", PrintOs},
{" kr ", GetKernel},
{" usr ", GetUser},
{" upt ", GetUptime},
{" arch ", GetArch},
{"shell ", GetShell},
{" pkgs ", GetPkg},
{" mem ", GetMem},
{"model ", GetModel},
{" host ", GetHostname},
{" lAVG ", GetAVG},
{" batt ", GetBattery},
{" ", Blank},
{COLORS, Blank}
};
#endif
#endif

View File

@ -1,35 +0,0 @@
#ifndef _CONFIG_H
#define _CONFIG_H
#include "fetch.h"
typedef struct {
char *title;
int (*func)(const char *title, const FETCH fetch);
} PARAMETR;
/* Config, EDIT THERE */
#define COLORS " \033[31mx \033[32mx \033[33mx \033[34mx \033[35mx \033[0m"
#define FONT_COLOR "\033[1;37m"
#define BAR_WIDTH 10
/* Uncomment if you use firacode font */
/* #define FIRA_CODE */
/* Full path to capacity file */
/* Example: /sys/class/power_supply/battery/capacity */
#define BATT_NAME NULL
#ifdef _MAIN_H
PARAMETR CONFIG[] = {
{" ", Blank},
{"\033[1;31m  ", PrintOs},
{"", GetKernel},
{"", GetUptime},
{"", GetPkg},
{" ", Blank},
{COLORS, Blank}
};
#endif
#endif

View File

@ -1,35 +0,0 @@
#ifndef _CONFIG_H
#define _CONFIG_H
#include "fetch.h"
typedef struct {
char *title;
int (*func)(const char *title, const FETCH fetch);
} PARAMETR;
/* Config, EDIT THERE */
#define COLORS " \033[31mx \033[32mx \033[33mx \033[34mx \033[35mx \033[0m"
#define FONT_COLOR "\033[1;37m"
#define BAR_WIDTH 10
/* Uncomment if you use firacode font */
/* #define FIRA_CODE */
/* Full path to capacity file */
/* Example: /sys/class/power_supply/battery/capacity */
#define BATT_NAME NULL
#ifdef _MAIN_H
PARAMETR CONFIG[] = {
{" ", Blank},
{" arch ", GetArch},
{" upt ", GetUptime},
{" pkgs ", GetPkg},
{" os ", PrintOs},
{" ", Blank},
{COLORS, Blank}
};
#endif
#endif

View File

@ -1,44 +0,0 @@
#ifndef _CONFIG_H
#define _CONFIG_H
#include "fetch.h"
typedef struct {
char *title;
int (*func)(const char *title, const FETCH fetch);
} PARAMETR;
/* Config, EDIT THERE */
#define COLORS " \033[41;31m++ \033[42;32m++ \033[43;33m++ \033[44;34m++ \033[45;35m++ \033[46;36m++\033[0m"
#define FONT_COLOR "\033[1;37m"
#define BAR_WIDTH 10
/* Uncomment if you use firacode font */
/* #define FIRA_CODE */
/* Full path to capacity file */
/* Example: /sys/class/power_supply/battery/capacity */
#define BATT_NAME NULL
#ifdef _MAIN_H
PARAMETR CONFIG[] = {
{" pc: ", Blank},
{" - mem ", GetMem},
{" - kr ", GetKernel},
{" - ar ", GetArch},
{" - mod ", GetModel},
{" - ht ", GetHostname},
{" ", Blank},
{" - info: ", Blank},
{" - os ", PrintOs},
{" - shl ", GetShell},
{" - pkg ", GetPkg},
{" - up ", GetUptime},
{" - avg ", GetAVG},
{" - bat ", GetBattery},
{" ", Blank},
{COLORS, Blank}
};
#endif
#endif