Changed parse_config(), now it works without json-c

This commit is contained in:
Adriano Oliviero 2021-04-23 18:02:05 +02:00
parent 83e327b34c
commit 09b32b1e1e
4 changed files with 73 additions and 74 deletions

View file

@ -2,19 +2,16 @@ NAME = uwufetch
FILES = uwufetch.c FILES = uwufetch.c
CFLAGS = -O3 CFLAGS = -O3
CFLAGS_DEBUG = -Wall -Wextra CFLAGS_DEBUG = -Wall -Wextra
LIBS = -ljson-c
PREFIX = /usr/bin PREFIX = /usr/bin
CC = cc CC = cc
MAN_COMPILER = pandoc MAN_COMPILER = pandoc
build: $(FILES) build: $(FILES)
$(CC) $(CFLAGS) $(LIBS) -o $(NAME) $(FILES) $(CC) $(CFLAGS) -o $(NAME) $(FILES)
$(MAN_COMPILER) $(NAME)_man.md -st man -o $(NAME).1
@gzip $(NAME).1
debug: debug:
@clear @clear
$(CC) $(CFLAGS_DEBUG) $(LIBS) -o $(NAME) $(FILES) $(CC) $(CFLAGS_DEBUG) -o $(NAME) $(FILES)
./uwufetch ./uwufetch
install: install:
@ -37,6 +34,10 @@ termux_uninstall:
rm -rf $(DESTDIR)/data/data/com.termux/files$(PREFIX)/$(NAME) rm -rf $(DESTDIR)/data/data/com.termux/files$(PREFIX)/$(NAME)
rm -rf $(DESTDIR)/data/data/com.termux/files/usr/lib/uwufetch/ rm -rf $(DESTDIR)/data/data/com.termux/files/usr/lib/uwufetch/
man:
$(MAN_COMPILER) $(NAME)_man.md -st man -o $(NAME).1
@gzip $(NAME).1
man_debug: man_debug:
$(MAN_COMPILER) $(NAME)_man.md -st man -o $(NAME).1 $(MAN_COMPILER) $(NAME)_man.md -st man -o $(NAME).1
@clear @clear

View file

@ -10,7 +10,7 @@ Nyalpine, Nyarch Linuwu, Nyartix Linuwu, Debinyan, Fedowa, GentOwO, GnUwU gUwUix
#### Partial support (Either no Ascii art, or no image is provided) #### Partial support (Either no Ascii art, or no image is provided)
endeavOwO, KDE NeOwOn, nixOwOs, Swackwawe, sOwOlus; Plus FweeBSD, OwOpenBSD and macOwOS endOwO, KDE NeOwOn, nixOwOs, Swackwawe, sOwOlus; Plus FweeBSD, OwOpenBSD and macOwOS
## Building and installation ## Building and installation
@ -20,8 +20,6 @@ endeavOwO, KDE NeOwOn, nixOwOs, Swackwawe, sOwOlus; Plus FweeBSD, OwOpenBSD and
[lshw](https://github.com/lyonel/lshw) to get gpu info. [lshw](https://github.com/lyonel/lshw) to get gpu info.
[json-c](https://github.com/json-c/json-c) to parse the json config.
##### Via package manager ##### Via package manager
Right now, the package is only available on the AUR: Right now, the package is only available on the AUR:

View file

@ -19,7 +19,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <json-c/json.h>
#ifdef __APPLE__ #ifdef __APPLE__
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <time.h> #include <time.h>
@ -73,7 +72,7 @@ struct winsize win;
int target_width = 0, screen_width = 0, screen_height = 0, ram_total, ram_used = 0, pkgs = 0; int target_width = 0, screen_width = 0, screen_height = 0, ram_total, ram_used = 0, pkgs = 0;
long uptime = 0; long uptime = 0;
// all flags available // all flags available
int ascii_image_flag = 0, int ascii_image_flag = 0, // when (0) ascii is printed, when (1) image is printed
show_user_info = 1, show_user_info = 1,
show_os = 1, show_os = 1,
show_kernel = 1, show_kernel = 1,
@ -85,6 +84,7 @@ int ascii_image_flag = 0,
show_pkgs = 1, show_pkgs = 1,
show_uptime = 1, show_uptime = 1,
show_colors = 1; show_colors = 1;
char user[32], host[256], shell[64], kernel[256], version_name[64], cpu_model[256], char user[32], host[256], shell[64], kernel[256], version_name[64], cpu_model[256],
gpu_model[8][256] = {{'0'}, {'0'}, {'0'}, {'0'}, {'0'}, {'0'}, {'0'}, {'0'}}, gpu_model[8][256] = {{'0'}, {'0'}, {'0'}, {'0'}, {'0'}, {'0'}, {'0'}, {'0'}},
pkgman_name[64], image_name[128], *config_directory = NULL; pkgman_name[64], image_name[128], *config_directory = NULL;
@ -145,69 +145,73 @@ int main(int argc, char *argv[])
break; break;
} }
} }
if (argc == 1)
parse_config(); parse_config();
if ((argc == 1 && ascii_image_flag == 0) || (argc > 1 && ascii_image_flag == 0)) if ((argc == 1 && ascii_image_flag == 0) || (argc > 1 && ascii_image_flag == 0))
print_ascii(); print_ascii();
else if (ascii_image_flag) else if (ascii_image_flag == 1)
print_image(); print_image();
uwu_name(); uwu_name();
print_info(); print_info();
} }
void parse_config() void parse_config()
{ // using json-c library to parse the config {
// allocating all necessary variables char line[256];
char buffer[1024];
char *homedir = getenv("HOME"); char *homedir = getenv("HOME");
struct json_object *parsed_json, *distributon, *image, *ascii, *user, *os, *kernel, *cpu, *gpu, *ram, *resolution, *shell, *pkgs, *uptime, *colors;
// opening and reading the config file // opening and reading the config file
FILE *config; FILE *config;
if (config_directory == NULL) if (config_directory == NULL)
config = fopen(strcat(homedir, "/.config/uwufetch/config.json"), "r"); config = fopen(strcat(homedir, "/.config/uwufetch/config"), "r");
else else
config = fopen(config_directory, "r"); config = fopen(config_directory, "r");
if (config == NULL) if (config == NULL)
return; return;
fread(buffer, 1024, 1, config); while (fgets(line, sizeof(line), config))
fclose(config); {
char buffer[128] = {0};
// parsing json data sscanf(line, "distro=%s", version_name);
parsed_json = json_tokener_parse(buffer); if (sscanf(line, "ascii=%[truefalse]", buffer))
json_object_object_get_ex(parsed_json, "distribution", &distributon); ascii_image_flag = !strcmp(buffer, "false");
json_object_object_get_ex(parsed_json, "image", &image); if (sscanf(line, "image=\"%[^\"]\"", image_name))
json_object_object_get_ex(parsed_json, "ascii", &ascii); {
json_object_object_get_ex(parsed_json, "user", &user); if (image_name[0] == '~')
json_object_object_get_ex(parsed_json, "os", &os); { // image name with ~ does not work
json_object_object_get_ex(parsed_json, "kernel", &kernel); memmove(&image_name[0], &image_name[1], strlen(image_name));
json_object_object_get_ex(parsed_json, "cpu", &cpu); char temp[128] = "/home/";
json_object_object_get_ex(parsed_json, "gpu", &gpu); strcat(temp, user);
json_object_object_get_ex(parsed_json, "ram", &ram); strcat(temp, image_name);
json_object_object_get_ex(parsed_json, "resolution", &resolution); sprintf(image_name, "%s", temp);
json_object_object_get_ex(parsed_json, "shell", &shell); }
json_object_object_get_ex(parsed_json, "pkgs", &pkgs);
json_object_object_get_ex(parsed_json, "uptime", &uptime);
json_object_object_get_ex(parsed_json, "colors", &colors);
if (json_object_get_string(distributon) != NULL)
sprintf(version_name, "%s", json_object_get_string(distributon));
if (sprintf(image_name, "%s", json_object_get_string(image)) == 0)
ascii_image_flag = 1; ascii_image_flag = 1;
else // reset image_name var to avoid errors }
sprintf(image_name, "%i", 0x0); if (sscanf(line, "user=%[truefalse]", buffer))
ascii_image_flag = !json_object_get_boolean(ascii); show_user_info = !strcmp(buffer, "true");
show_user_info = json_object_get_boolean(user); if (sscanf(line, "os=%[truefalse]", buffer))
show_os = json_object_get_boolean(os); show_os = strcmp(buffer, "false");
show_kernel = json_object_get_boolean(kernel); if (sscanf(line, "kernel=%[truefalse]", buffer))
show_cpu = json_object_get_boolean(cpu); show_kernel = strcmp(buffer, "false");
show_gpu = json_object_get_boolean(gpu); if (sscanf(line, "cpu=%[truefalse]", buffer))
show_ram = json_object_get_boolean(ram); show_cpu = strcmp(buffer, "false");
show_resolution = json_object_get_boolean(resolution); if (sscanf(line, "gpu=%[truefalse]", buffer))
show_shell = json_object_get_boolean(shell); show_gpu = strcmp(buffer, "false");
show_pkgs = json_object_get_boolean(pkgs); if (sscanf(line, "ram=%[truefalse]", buffer))
show_uptime = json_object_get_boolean(uptime); show_ram = strcmp(buffer, "false");
show_colors = json_object_get_boolean(colors); if (sscanf(line, "resolution=%[truefalse]", buffer))
show_resolution = strcmp(buffer, "false");
if (sscanf(line, "shell=%[truefalse]", buffer))
show_shell = strcmp(buffer, "false");
if (sscanf(line, "pkgs=%[truefalse]", buffer))
show_pkgs = strcmp(buffer, "false");
if (sscanf(line, "uptime=%[truefalse]", buffer))
show_uptime = strcmp(buffer, "false");
if (sscanf(line, "colors=%[truefalse]", buffer))
show_colors = strcmp(buffer, "false");
}
fclose(config);
} }
int pkgman() int pkgman()

View file

@ -39,27 +39,23 @@ prints a list of all supported distributions
# CONFIG FILE # CONFIG FILE
The config file is located in $HOME/.config/uwufetch/config.json (you need to create it), but you can change the path by using the `--config` option. The config file is located in $HOME/.config/uwufetch/config (you need to create it), but you can change the path by using the `--config` option.
## EXAMPLE ## EXAMPLE:
```json distro=arch\
{ image="~/Pictures/picture.png"\
"distribution": "debian", ascii=false\
"image": "/home/user/Pictures/debian.png", user=true\
"ascii": false, os=true\
"user": true, kernel=true\
"os": true, cpu=true\
"kernel": true, gpu=false\
"cpu": true, ram=true\
"gpu": true, resolution=true\
"ram": true, shell=true\
"resolution": true, pkgs=true\
"shell": true, uptime=true\
"pkgs": true, colors=true
"uptime": true,
"colors": true
}
```
# SUPPORTED DISTRIBUTIONS # SUPPORTED DISTRIBUTIONS