From 6071c1b09da7b76c7c059320763f7de22f94d224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 7 Aug 2023 14:15:11 +0800 Subject: [PATCH] JsonConfig: support generate default config file with `--gen-config` --- CMakeLists.txt | 1 + README.md | 6 +++++- src/data/config_user.jsonc | 43 +++++++++++++++++++++++++++++++++++++ src/data/help.txt | 30 +++++++++++++------------- src/fastfetch.c | 25 ++++++++++++++++----- src/fastfetch_datatext.h.in | 1 + 6 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 src/data/config_user.jsonc diff --git a/CMakeLists.txt b/CMakeLists.txt index bc31ea211..882997f14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,6 +227,7 @@ endfunction(fastfetch_load_text) fastfetch_load_text(src/data/structure.txt DATATEXT_STRUCTURE) fastfetch_load_text(src/data/config_system.txt DATATEXT_CONFIG_SYSTEM) fastfetch_load_text(src/data/config_user.txt DATATEXT_CONFIG_USER) +fastfetch_load_text(src/data/config_user.jsonc DATATEXT_CONFIG_USER_JSONC) fastfetch_load_text(src/data/modules.txt DATATEXT_MODULES) fastfetch_load_text(src/data/help.txt DATATEXT_HELP) fastfetch_load_text(src/data/help_color.txt DATATEXT_HELP_COLOR) diff --git a/README.md b/README.md index b2152044f..1c3dff53b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ There are [screenshots on different platforms](https://github.com/fastfetch-cli/ With customization and speed being two competing goals, this project actually builds two executables: -* The main one is `fastfetch`, which can be very greatly configured via flags. These flags can be made persistent by modifying `$XDG_CONFIG_HOME/fastfetch/config.conf`. To view the available options, run `fastfetch --help`. +* The main one is `fastfetch`, which can be very greatly configured via flags. These flags can be made persistent by modifying `$XDG_CONFIG_HOME/fastfetch/config.conf`. Use `fastfetch --gen-config conf` to generate one. To view the available options, run `fastfetch --help`. * The second executable is called `flashfetch`, which is configured at compile time to eliminate any possible overhead. Configuration of it can be very easily done in [`src/flashfetch.c`](src/flashfetch.c). Currently, the performance difference is measurable, but too small to be recognizable by humans. But with more options planned, the leap will get bigger over time and on slow machines this might actually make a difference. @@ -27,6 +27,10 @@ There are some premade config files in [`presets`](presets), including the ones Logos can be heavily customized too; see the [logo documentation](https://github.com/fastfetch-cli/fastfetch/wiki/Logo-options) for more information. +### Customization with new JSONC format + +A new, structure based, and user-friendly config file format was introduced in v2.0.0. This format is based on [JSONC](https://jsonc.org/). [See more details in Wiki](https://github.com/fastfetch-cli/fastfetch/wiki/Configuration) + ## Dependencies Fastfetch dynamically loads needed libraries if they are available. On Linux, its only hard dependencies are `libc` (any implementation of the c standard library), `libdl` and [`libpthread`](https://man7.org/linux/man-pages/man7/pthreads.7.html) (if built with multithreading support). They are all shipped with [`glibc`](https://www.gnu.org/software/libc/), which is already installed on most Linux distributions. diff --git a/src/data/config_user.jsonc b/src/data/config_user.jsonc new file mode 100644 index 000000000..2e3bbb36f --- /dev/null +++ b/src/data/config_user.jsonc @@ -0,0 +1,43 @@ +// ~/.config/fastfetch/config.jsonc +// See https://github.com/fastfetch-cli/fastfetch/wiki/Configuration for more details +// See *.jsonc in https://github.com/fastfetch-cli/fastfetch/tree/dev/presets/examples for more examples +{ + "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json", + "logo": { + "padding": { + "top": 2 + } + }, + "display": { + "percentType": 9 // colored percent number + }, + "modules": [ + "title", + "separator", + "os", + "host", + "kernel", + "uptime", + "packages", + "shell", + "display", + "de", + "wm", + "wmtheme", + "theme", + "icons", + "font", + "cursor", + "terminal", + "terminalfont", + "cpu", + "gpu", + "memory", + "disk", + "battery", + "poweradapter", + "locale", + "break", + "colors" + ] +} diff --git a/src/data/help.txt b/src/data/help.txt index 2d6ef4dd4..95a74429c 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -1,21 +1,21 @@ Usage: fastfetch Informative options: - -h,--help: Show this message - -h,--help : Show help for a specific command - -v,--version: Show the version of fastfetch - --list-config-paths: List search paths of config files - --list-data-paths: List search paths of presets and logos - --list-logos: List available logos - --list-modules: List available modules - --list-presets: List presets fastfetch knows about; they can be loaded with --load-config (+) - --list-features: List the supported features fastfetch was compiled with (mainly for development) - --print-logos: Print available logos - --print-config-system: Print the default system config - --print-config-user: Print the default user config - --print-structure: Print the default structure - --gen-config: Generate a sample config file in the default path - --gen-config-force: Generate a sample config file in the default path. Overwrite the existing one + -h,--help: Show this message + -h,--help : Show help for a specific command + -v,--version: Show the version of fastfetch + --list-config-paths: List search paths of config files + --list-data-paths: List search paths of presets and logos + --list-logos: List available logos + --list-modules: List available modules + --list-presets: List presets fastfetch knows about; they can be loaded with --load-config (+) + --list-features: List the supported features fastfetch was compiled with (mainly for development) + --print-logos: Print available logos + --print-config-system: Print the default system config + --print-config-user: Print the default user config + --print-structure: Print the default structure + --gen-config : Generate a sample config file in the default path. Param `type` can be `jsonc` or `conf` + --gen-config-force : Generate a sample config file in the default path. Overwrite the existing one General options: --load-config : Load a config file or preset (+) diff --git a/src/fastfetch.c b/src/fastfetch.c index f12ffe0ca..2e7faf24b 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -619,11 +619,23 @@ static bool parseConfigFile(FFdata* data, const char* path) return true; } -static void generateConfigFile(bool force) +static void generateConfigFile(bool force, const char* type) { FFstrbuf* filename = (FFstrbuf*) ffListGet(&instance.state.platform.configDirs, 0); // Paths generated in `init.c/initConfigDirs` end with `/` - ffStrbufAppendS(filename, "fastfetch/config.conf"); + bool isJsonc = false; + if (type) + { + if (ffStrEqualsIgnCase(type, "jsonc")) + isJsonc = true; + else if (!ffStrEqualsIgnCase(type, "conf")) + { + fputs("config type can only be `jsonc` or `conf`\n", stderr); + exit(1); + } + } + + ffStrbufAppendS(filename, isJsonc ? "fastfetch/config.jsonc" : "fastfetch/config.conf"); if (!force && ffPathExists(filename->chars, FF_PATHTYPE_FILE)) { @@ -632,7 +644,10 @@ static void generateConfigFile(bool force) } else { - ffWriteFileData(filename->chars, sizeof(FASTFETCH_DATATEXT_CONFIG_USER), FASTFETCH_DATATEXT_CONFIG_USER); + ffWriteFileData( + filename->chars, + isJsonc ? strlen(FASTFETCH_DATATEXT_CONFIG_USER_JSONC) : strlen(FASTFETCH_DATATEXT_CONFIG_USER), + isJsonc ? FASTFETCH_DATATEXT_CONFIG_USER_JSONC : FASTFETCH_DATATEXT_CONFIG_USER); printf("A sample config file has been written in `%s`\n", filename->chars); exit(0); } @@ -843,9 +858,9 @@ static void parseOption(FFdata* data, const char* key, const char* value) else if(ffStrEqualsIgnCase(key, "--load-config")) optionParseConfigFile(data, key, value); else if(ffStrEqualsIgnCase(key, "--gen-config")) - generateConfigFile(false); + generateConfigFile(false, value); else if(ffStrEqualsIgnCase(key, "--gen-config-force")) - generateConfigFile(true); + generateConfigFile(true, value); else if(ffStrEqualsIgnCase(key, "--thread") || ffStrEqualsIgnCase(key, "--multithreading")) instance.config.multithreading = ffOptionParseBoolean(value); else if(ffStrEqualsIgnCase(key, "--stat")) diff --git a/src/fastfetch_datatext.h.in b/src/fastfetch_datatext.h.in index dd5418f3a..f8a699427 100644 --- a/src/fastfetch_datatext.h.in +++ b/src/fastfetch_datatext.h.in @@ -6,6 +6,7 @@ #define FASTFETCH_DATATEXT_STRUCTURE "@DATATEXT_STRUCTURE@" #define FASTFETCH_DATATEXT_CONFIG_SYSTEM "@DATATEXT_CONFIG_SYSTEM@" #define FASTFETCH_DATATEXT_CONFIG_USER "@DATATEXT_CONFIG_USER@" //Requires FASTFETCH_PROJECT_VERSION and FASTFETCH_DATATEXT_STRUCTURE to be set +#define FASTFETCH_DATATEXT_CONFIG_USER_JSONC "@DATATEXT_CONFIG_USER_JSONC@" #define FASTFETCH_DATATEXT_MODULES "@DATATEXT_MODULES@" #define FASTFETCH_DATATEXT_HELP "@DATATEXT_HELP@" #define FASTFETCH_DATATEXT_HELP_COLOR "@DATATEXT_HELP_COLOR@"