From fceb19affe590d0e5636305f226f1acf65ae5c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 14 Nov 2023 09:58:42 +0800 Subject: [PATCH] Fastfetch: change the order of loading config files --- CHANGELOG.md | 2 ++ src/data/help.txt | 1 + src/fastfetch.c | 21 +++++++++------------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b9ecdc2c..24ed5857d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ Changes: * The deprecated flag `--gen-config conf` is removed * Flag `--gen-config` now does the same thing as `--migrate-config`, which can be used as config migration and default config file generation. Flag `--migrate-config` is removed +* Fastfetch now searchs for config files in the order of `fastfetch --list-config-paths`, and won't load other config if one is found. +* Flag `--load-user-config` now works in command line flags, but not in config file. # 2.2.3 diff --git a/src/data/help.txt b/src/data/help.txt index bc2682db1..1e1df2107 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -20,6 +20,7 @@ Config options: -c, --config : Load a config file or preset (+) --gen-config : Generate a config file (or print the file if is `-`), with options specified in the command line (if any) --gen-config-force : Generate a config file. Overwrite the existing one + --load-user-config : Set if user config should be loaded. Default is true if env-var `NO_CONFIG` is not set General options: --thread : Use separate threads to send HTTP requests diff --git a/src/fastfetch.c b/src/fastfetch.c index 103f0b109..837eae772 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -160,6 +160,7 @@ static void listConfigPaths(void) FF_LIST_FOR_EACH(FFstrbuf, folder, instance.state.platform.configDirs) { bool exists = false; + uint32_t length = folder->length + sizeof("fastfetch"); ffStrbufAppendS(folder, "fastfetch/config.jsonc"); exists = ffPathExists(folder->chars, FF_PATHTYPE_FILE); if (!exists) @@ -168,6 +169,7 @@ static void listConfigPaths(void) ffStrbufAppendS(folder, "conf"); exists = ffPathExists(folder->chars, FF_PATHTYPE_FILE); } + ffStrbufSubstrBefore(folder, length); printf("%s%s\n", folder->chars, exists ? " (*)" : ""); } } @@ -218,7 +220,7 @@ static bool parseJsoncFile(const char* path) static bool parseConfigFile(FFdata* data, const char* path) { - FILE* file = fopen(path, "r"); + FF_AUTO_CLOSE_FILE FILE* file = fopen(path, "r"); if(file == NULL) return false; @@ -311,7 +313,6 @@ static bool parseConfigFile(FFdata* data, const char* path) if(line != NULL) free(line); - fclose(file); return true; } @@ -572,9 +573,8 @@ static void parseConfigFiles(FFdata* data) { if (__builtin_expect(instance.state.genConfigPath.length == 0, true)) { - for (uint32_t i = instance.state.platform.configDirs.length; i > 0; --i) + FF_LIST_FOR_EACH(FFstrbuf, dir, instance.state.platform.configDirs) { - FFstrbuf* dir = ffListGet(&instance.state.platform.configDirs, i - 1); uint32_t dirLength = dir->length; ffStrbufAppendS(dir, "fastfetch/config.jsonc"); @@ -583,17 +583,14 @@ static void parseConfigFiles(FFdata* data) if (success) return; } } - for (uint32_t i = instance.state.platform.configDirs.length; i > 0; --i) + FF_LIST_FOR_EACH(FFstrbuf, dir, instance.state.platform.configDirs) { - if (!data->loadUserConfig) - return; - - FFstrbuf* dir = ffListGet(&instance.state.platform.configDirs, i - 1); uint32_t dirLength = dir->length; ffStrbufAppendS(dir, "fastfetch/config.conf"); - parseConfigFile(data, dir->chars); + bool success = parseConfigFile(data, dir->chars); ffStrbufSubstrBefore(dir, dirLength); + if (success) return; } } @@ -710,11 +707,11 @@ int main(int argc, char** argv) FFdata data = { .structure = ffStrbufCreate(), .customValues = ffListCreate(sizeof(FFCustomValue)), - .loadUserConfig = true, + .loadUserConfig = !getenv("NO_CONFIG"), }; parseArguments(&data, argc, argv, parseCommand); - if(!getenv("NO_CONFIG") && data.loadUserConfig) + if(data.loadUserConfig) parseConfigFiles(&data); parseArguments(&data, argc, argv, (void*) parseOption);