Skip to content

Commit

Permalink
minor readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alba4k committed Nov 13, 2024
1 parent 0c43c03 commit 2a9de47
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 142 deletions.
6 changes: 3 additions & 3 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ void parse_config(const char *file, struct Module *modules, void **ascii_ptr, bo
for(size_t i = 0; i < sizeof(booleanOptions)/sizeof(booleanOptions[0]); ++i) {
if(parse_config_bool(conf, booleanOptions[i], &buffer) == 0) {
if(buffer)
config.options |= ((uint64_t)1 << i);
config.boolean_options |= ((uint64_t)1 << i);
else
config.options &= ~((uint64_t)1 << i);
config.boolean_options &= ~((uint64_t)1 << i);
}
}
*default_bold = bold;
*default_bold = _bold;

// OTHER MODULE-RELATED OPTIONS

Expand Down
66 changes: 33 additions & 33 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

struct Config {
/* Starting from the least significant byte, see the #define statements later
* 0. align
* 1. bold
* 2. title_color
* 3. os_arch
* 4. kernel_short
* 0. _align
* 1. _bold
* 2. _title_color
* 3. _os_arch
* 4. _kernel_short
* 5. de_type
* 6. shell_path
* 7. term_ssh
Expand All @@ -38,7 +38,7 @@ struct Config {
* 25. bat_status
* 26. [...]
*/
uint64_t options;
uint64_t boolean_options;

char **logo;
char color[8];
Expand Down Expand Up @@ -81,33 +81,33 @@ struct Config {
};
extern struct Config config;

#define align config.options & 0x1
#define bold config.options & 0x2
#define title_color config.options & 0x4
#define os_arch config.options & 0x8
#define kernel_short config.options & 0x10
#define de_type config.options & 0x20
#define shell_path config.options & 0x40
#define term_ssh config.options & 0x80
#define pkg_mgr config.options & 0x100
#define pkg_pacman config.options & 0x200
#define pkg_dpkg config.options & 0x400
#define pkg_rpm config.options & 0x800
#define pkg_flatpak config.options & 0x1000
#define pkg_snap config.options & 0x2000
#define pkg_brew config.options & 0x4000
#define pkg_pip config.options & 0x8000
#define cpu_brand config.options & 0x10000
#define cpu_freq config.options & 0x20000
#define cpu_count config.options & 0x40000
#define gpu_brand config.options & 0x80000
#define mem_perc config.options & 0x100000
#define loc_localhost config.options & 0x200000
#define loc_docker config.options & 0x400000
#define pwd_path config.options & 0x800000
#define kernel_type config.options & 0x1000000
#define col_background config.options & 0x2000000
#define bat_status config.options & 0x4000000
#define _align config.boolean_options & 0x1
#define _bold config.boolean_options & 0x2
#define _title_color config.boolean_options & 0x4
#define _os_arch config.boolean_options & 0x8
#define _kernel_short config.boolean_options & 0x10
#define _de_type config.boolean_options & 0x20
#define _shell_path config.boolean_options & 0x40
#define _term_ssh config.boolean_options & 0x80
#define _pkg_mgr config.boolean_options & 0x100
#define _pkg_pacman config.boolean_options & 0x200
#define _pkg_dpkg config.boolean_options & 0x400
#define _pkg_rpm config.boolean_options & 0x800
#define _pkg_flatpak config.boolean_options & 0x1000
#define _pkg_snap config.boolean_options & 0x2000
#define _pkg_brew config.boolean_options & 0x4000
#define _pkg_pip config.boolean_options & 0x8000
#define _cpu_brand config.boolean_options & 0x10000
#define _cpu_freq config.boolean_options & 0x20000
#define _cpu_count config.boolean_options & 0x40000
#define _gpu_brand config.boolean_options & 0x80000
#define _mem_perc config.boolean_options & 0x100000
#define _loc_localhost config.boolean_options & 0x200000
#define _loc_docker config.boolean_options & 0x400000
#define _pwd_path config.boolean_options & 0x800000
#define _kernel_type config.boolean_options & 0x1000000
#define _col_background config.boolean_options & 0x2000000
#define _bat_status config.boolean_options & 0x4000000

void parse_config(const char *file, struct Module *modules, void **ascii_ptr, bool *default_bold, char *default_color, char *default_logo);

Expand Down
6 changes: 3 additions & 3 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ int main(int argc, char **argv) {
struct timeval start, end, start_all;
double time;

// just setting every option to 1 (except maybe pkg_pip cause pip is slow af)
// just setting every option to 1 (except maybe _pkg_pip cause pip is slow af)
if(argc > 1)
config.options = strcmp(argv[1], "--no-pip") ? 0xffffffffffffffff : 0xffffffffffff7fff;
config.boolean_options = strcmp(argv[1], "--no-pip") ? 0xffffffffffffffff : 0xffffffffffff7fff;
else
config.options = 0xffffffffffffffff;
config.boolean_options = 0xffffffffffffffff;
// these are just defaults
strcpy(config.col_block_str, " ");
strcpy(config.date_format, "%02d/%02d/%d %02d:%02d:%02d");
Expand Down
4 changes: 2 additions & 2 deletions src/info/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ int battery(char *dest) {
#endif // __ANDROID__

if(capacity[0] != 0 && status[0] != 0) {
if((bat_status))
if((_bat_status))
snprintf(dest, 256, "%s%% (%s)", capacity, status);
else
snprintf(dest, 256, "%s%%", capacity);
}
else if(capacity[0] != 0)
snprintf(dest, 256, "%s%%", capacity);
else if(status[0] != 0 && (bat_status))
else if(status[0] != 0 && (_bat_status))
strncpy(dest, status, 256);
else
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/info/colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int colors(char *dest) {
memset(dest, 0, 256);

for(int i = 0; i < 8; ++i)
sprintf(dest+strlen(dest), "\033[%s%dm%s", col_background ? "4" : "3", i, config.col_block_str);
sprintf(dest+strlen(dest), "\033[%s%dm%s", _col_background ? "4" : "3", i, config.col_block_str);

strcat(dest, "\033[0m");

Expand Down
8 changes: 4 additions & 4 deletions src/info/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int cpu(char *dest) {
fclose(fp);

cpu_info = buf;
if(cpu_count) {
if(_cpu_count) {
end = cpu_info;
while((end = strstr(end, "processor"))) {
++count;
Expand Down Expand Up @@ -82,7 +82,7 @@ int cpu(char *dest) {
// Printing the clock frequency the first thread is currently running at
++end;
char *frequency = strstr(end, "cpu MHz");
if(frequency && cpu_freq) {
if(frequency && _cpu_freq) {
frequency = strchr(frequency, ':');
if(frequency) {
frequency += 2;
Expand Down Expand Up @@ -114,7 +114,7 @@ int cpu(char *dest) {
*end = 0;
}

if((cpu_brand) == 0) {
if((_cpu_brand) == 0) {
if((end = strstr(cpu_info, "Intel Core ")))
memmove(end, end+11, strlen(end+1));
else if((end = strstr(cpu_info, "Apple ")))
Expand All @@ -131,7 +131,7 @@ int cpu(char *dest) {
if(freq[0])
strncat(dest, freq, 255-strlen(dest));

if(count && cpu_count) {
if(count && _cpu_count) {
char core_count[16];
snprintf(core_count, 16, " (%d) ", count);
strncat(dest, core_count, 255-strlen(dest));
Expand Down
2 changes: 1 addition & 1 deletion src/info/desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int desktop(char *dest) {

strcpy(dest, desktop);

if(de_type) {
if(_de_type) {
if(getenv("WAYLAND_DISPLAY"))
strncat(dest, " (Wayland)", 255-strlen(dest));
else if((desktop = getenv("XDG_SESSION_TYPE"))) {
Expand Down
2 changes: 1 addition & 1 deletion src/info/gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int gpu(char *dest) {
// also, I'm using end as a random char* - BaD pRaCtIcE aNd CoNfUsInG - lol stfu
dest[0] = 0; // yk it's decent a yk it works
for(unsigned j = 0; j < sizeof(gpus)/sizeof(gpus[0]) && gpus[j%3]; ++j) {
if((gpu_brand) == 0) {
if((_gpu_brand) == 0) {
if(strstr(gpus[j], "Intel ")
|| strstr(gpus[j], "Apple "))
gpus[j] += 6;
Expand Down
6 changes: 3 additions & 3 deletions src/info/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ int kernel(char *dest) {
uname(&name);
char *ptr = name.release, *type = NULL;

if(kernel_type) {
if(_kernel_type) {
while((ptr = strchr(ptr, '-')))
type = ++ptr;
}

if(kernel_short) {
if(_kernel_short) {
if((ptr = strchr(name.release, '-')))
*ptr = 0;
}

if(kernel_type && type)
if(_kernel_type && type)
snprintf(dest, 256, "%s (%s)", name.release, type);
else
strncpy(dest, name.release, 256);
Expand Down
2 changes: 1 addition & 1 deletion src/info/light_colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int light_colors(char *dest) {
memset(dest, 0, 256);

for(int i = 0; i < 8; ++i)
sprintf(dest+strlen(dest), "\033[%s%dm%s", col_background ? "10" : "9", i, config.col_block_str);
sprintf(dest+strlen(dest), "\033[%s%dm%s", _col_background ? "10" : "9", i, config.col_block_str);

strcat(dest, "\033[0m");

Expand Down
2 changes: 1 addition & 1 deletion src/info/local_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int local_ip(char *dest) {
// checking if the ip is valid
if(addrs->ifa_addr && addrs->ifa_addr->sa_family == AF_INET) {
// filtering out docker or localhost ips
if((strcmp(addrs->ifa_name, "lo") || loc_localhost) && (strcmp(addrs->ifa_name, "docker0") || loc_docker)) {
if((strcmp(addrs->ifa_name, "lo") || _loc_localhost) && (strcmp(addrs->ifa_name, "docker0") || _loc_docker)) {
struct sockaddr_in *pAddr = (struct sockaddr_in *)addrs->ifa_addr;

// saving it to the list of interfaces
Expand Down
2 changes: 1 addition & 1 deletion src/info/login_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int login_shell(char *dest) {
char *buf = getenv("SHELL");

if(buf && buf[0]) {
strncpy(dest, shell_path ? buf : basename(buf), 256);
strncpy(dest, _shell_path ? buf : basename(buf), 256);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/info/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int memory(char *dest) {
snprintf(dest, 256, "%lu MiB / %lu MiB", usedram/1024, totalram/1024);
#endif

if(mem_perc) {
if(_mem_perc) {
const size_t len = 256-strlen(dest);
char perc[len];

Expand Down
6 changes: 3 additions & 3 deletions src/info/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int os(char *dest) {
uname(&name);

#ifdef __APPLE__
if(os_arch)
if(_os_arch)
snprintf(dest, 256, "macOS (%s)", name.machine);
else
strncpy(dest, "macOS", 255);
Expand All @@ -28,7 +28,7 @@ int os(char *dest) {
char *args[] = {"getprop", "ro.build.version.release", NULL},
exec_cmd(version, 16, args);

if(os_arch)
if(_os_arch)
snprintf(dest, 256, "Android %s%s(%s)", version, version[0] ? " " : "", name.machine);
else
snprintf(dest, 256, "Android %s", version);
Expand Down Expand Up @@ -64,7 +64,7 @@ int os(char *dest) {
if(end)
*end = 0;

if(os_arch)
if(_os_arch)
snprintf(dest, 256, "%s (%s)", os_name, name.machine);
else
strncpy(dest, os_name, 255);
Expand Down
28 changes: 14 additions & 14 deletions src/info/packages.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ int packages(char *dest) {
if(getenv("PREFIX"))
strncpy(path, getenv("PREFIX"), 255);
strncat(path, "/var/lib/pacman/local", 256-strlen(path));
if(pkg_pacman && (dir = opendir(path))) {
if(_pkg_pacman && (dir = opendir(path))) {
while((entry = readdir(dir)) != NULL)
if(entry->d_type == DT_DIR && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
++count;

if(count) {
snprintf(dest, 255 - strlen(buf), "%s%u%s", done ? ", " : "", count, pkg_mgr ? " (pacman)" : "");
snprintf(dest, 255 - strlen(buf), "%s%u%s", done ? ", " : "", count, _pkg_mgr ? " (pacman)" : "");
done = true;
}
closedir(dir);
Expand All @@ -42,7 +42,7 @@ int packages(char *dest) {
if(getenv("PREFIX"))
strncpy(path, getenv("PREFIX"), 255);
strncat(path, "/var/lib/dpkg/status", 256-strlen(path));
if(pkg_dpkg && (fp = fopen(path, "r"))) { // alternatively, I could use "dpkg-query -f L -W" and strlen
if(_pkg_dpkg && (fp = fopen(path, "r"))) { // alternatively, I could use "dpkg-query -f L -W" and strlen
fseek(fp, 0, SEEK_END);
size_t len = (size_t)ftell(fp);
rewind(fp);
Expand All @@ -62,7 +62,7 @@ int packages(char *dest) {
free(dpkg_list);

if(count) {
snprintf(buf, 256, "%u%s", count, pkg_mgr ? " (dpkg)" : "");
snprintf(buf, 256, "%u%s", count, _pkg_mgr ? " (dpkg)" : "");
done = true;
strncat(dest, buf, 256 - strlen(dest));
}
Expand All @@ -72,12 +72,12 @@ int packages(char *dest) {
if(getenv("PREFIX"))
strncpy(path, getenv("PREFIX"), 255);
strncat(path, "/var/lib/rpm/rpmdb.sqlite", 256-strlen(path));
if(pkg_rpm && access(path, F_OK) == 0) {
if(_pkg_rpm && access(path, F_OK) == 0) {
char *args[] = {"sqlite3", path, "SELECT count(*) FROM Packages", NULL};
exec_cmd(str, 16, args);

if(str[0] != '0' && str[0]) {
snprintf(buf, 255 - strlen(buf), "%s%s%s", done ? ", " : "", str, pkg_mgr ? " (rpm)" : "");
snprintf(buf, 255 - strlen(buf), "%s%s%s", done ? ", " : "", str, _pkg_mgr ? " (rpm)" : "");
done = true;
strncat(dest, buf, 256 - strlen(dest));
}
Expand All @@ -87,14 +87,14 @@ int packages(char *dest) {
if(getenv("PREFIX"))
strncpy(path, getenv("PREFIX"), 255);
strncat(path, "/var/lib/flatpak/runtime", 256-strlen(path));
if(pkg_flatpak && (dir = opendir(path))) {
if(_pkg_flatpak && (dir = opendir(path))) {
count = 0;
while((entry = readdir(dir)) != NULL)
if(entry->d_type == DT_DIR && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
++count;

if(count) {
snprintf(buf, 255 - strlen(buf), "%s%u%s", done ? ", " : "", count, pkg_mgr ? " (flatpak)" : "");
snprintf(buf, 255 - strlen(buf), "%s%u%s", done ? ", " : "", count, _pkg_mgr ? " (flatpak)" : "");
done = true;
strncat(dest, buf, 256 - strlen(dest));
}
Expand All @@ -105,18 +105,18 @@ int packages(char *dest) {
if(getenv("PREFIX"))
strncpy(path, getenv("PREFIX"), 255);
strncat(path, "/bin/snap", 256-strlen(path));
if(pkg_snap && access(path, F_OK) == 0) {
if(_pkg_snap && access(path, F_OK) == 0) {
char *args[] = {"sh", "-c", "snap list 2>/dev/null | wc -l", NULL};
exec_cmd(str, 16, args);

if(str[0] != '0' && str[0]) {
snprintf(buf, 255 - strlen(buf), "%s%d%s", done ? ", " : "", atoi(str)-1, pkg_mgr ? " (snap)" : "");
snprintf(buf, 255 - strlen(buf), "%s%d%s", done ? ", " : "", atoi(str)-1, _pkg_mgr ? " (snap)" : "");
done = true;
strncat(dest, buf, 256 - strlen(dest));
}
}
#endif
if(pkg_brew && (access("/usr/local/bin/brew", F_OK) == 0 || access("/opt/homebrew/bin/brew", F_OK) == 0 || access("/bin/brew", F_OK) == 0)) {
if(_pkg_brew && (access("/usr/local/bin/brew", F_OK) == 0 || access("/opt/homebrew/bin/brew", F_OK) == 0 || access("/bin/brew", F_OK) == 0)) {
char *args[] = {"brew", "--cellar", NULL};
exec_cmd(str, 16, args);

Expand All @@ -129,7 +129,7 @@ int packages(char *dest) {
++count;

if(count) {
snprintf(buf, 256, "%s%u%s", done ? ", " : "", count, pkg_mgr ? " (brew)" : "");
snprintf(buf, 256, "%s%u%s", done ? ", " : "", count, _pkg_mgr ? " (brew)" : "");
done = true;
strncat(dest, buf, 256 - strlen(dest));
}
Expand All @@ -144,12 +144,12 @@ int packages(char *dest) {
if(getenv("PREFIX"))
strncpy(path, getenv("PREFIX"), 255);
strncat(path, "/bin/pip", 256-strlen(path));
if(pkg_pip && access(path, F_OK) == 0) {
if(_pkg_pip && access(path, F_OK) == 0) {
char *args[] = {"sh", "-c", "pip list 2>/dev/null | wc -l", NULL};
exec_cmd(str, 16, args);

if(str[0] != '0' && str[0]) {
snprintf(buf, 255 - strlen(buf), "%s%d%s", done ? ", " : "", atoi(str)-2, pkg_mgr ? " (pip)" : "");
snprintf(buf, 255 - strlen(buf), "%s%d%s", done ? ", " : "", atoi(str)-2, _pkg_mgr ? " (pip)" : "");
done = true;
strncat(dest, buf, 256 - strlen(dest));
}
Expand Down
Loading

0 comments on commit 2a9de47

Please sign in to comment.