Config: don't generate a config file silently

Instead, provide a new flag `--gen-config` and print the path to the config file, so that users can know where to find the file.
This commit is contained in:
李通洲
2023-01-02 12:43:11 +08:00
parent d692fe91fb
commit ae6d921f23
6 changed files with 37 additions and 8 deletions
+2 -2
View File
@@ -6,7 +6,7 @@
# Empty lines or lines starting with # are ignored.
# This file was shipped with $" FASTFETCH_PROJECT_VERSION $".
# Use fastfetch --print-config-user > ~/.config/fastfetch/config.conf to overwrite this file with the current defaults
# Use fastfetch --gen-config-force to overwrite this file with the current defaults
# Below some often usefull options are listed. Uncomment and modify them so they take affect.
# Note that there are a lot more options than the ones listed here, take a look at "fastfetch --help".
@@ -16,7 +16,7 @@
# Load additional config files.
# Some are shipped with fastfetch, list them with "fastfetch --list-presets".
# Must be a path to a config file or the name of a shipped preset.
# The config file is completly loaded before continuing in the current file, so the placement of this option matters, as later options overwrite already set ones.
# The config file is completely loaded before continuing in the current file, so the placement of this option matters, as later options overwrite already set ones.
# Can be used multiple times to load multiple config files / presets.
#--load-config /path/to/config.txt
+2
View File
@@ -12,6 +12,8 @@ Informative options:
--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
General options:
--load-config <file>: Load a config file or preset (+)
+27 -5
View File
@@ -603,6 +603,25 @@ static bool parseConfigFile(FFinstance* instance, FFdata* data, const char* path
return true;
}
static void generateConfigFile(FFinstance* instance, bool force)
{
FFstrbuf* filename = (FFstrbuf*) ffListGet(&instance->state.configDirs, 0);
// Paths generated in `init.c/initConfigDirs` end with `/`
ffStrbufAppendS(filename, "fastfetch/config.conf");
if (!force && ffFileExists(filename->chars, S_IFREG))
{
fprintf(stderr, "Config file exists in `%s`, use `--gen-config-force` to overwrite\n", filename->chars);
exit(1);
}
else
{
ffWriteFileData(filename->chars, sizeof(FASTFETCH_DATATEXT_CONFIG_USER), FASTFETCH_DATATEXT_CONFIG_USER);
printf("A sample config file has been written in `%s`", filename->chars);
exit(0);
}
}
static void optionParseConfigFile(FFinstance* instance, FFdata* data, const char* key, const char* value)
{
if(value == NULL)
@@ -912,6 +931,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
fputs("`--nocache` are obsoleted. Caching functions other than image caching are removed.\n\n", stderr);
else if(strcasecmp(key, "--load-config") == 0)
optionParseConfigFile(instance, data, key, value);
else if(strcasecmp(key, "--gen-config") == 0)
generateConfigFile(instance, false);
else if(strcasecmp(key, "--gen-config-force") == 0)
generateConfigFile(instance, true);
else if(strcasecmp(key, "--thread") == 0 || strcasecmp(key, "--multithreading") == 0)
instance->config.multithreading = optionParseBoolean(value);
else if(strcasecmp(key, "--stat") == 0)
@@ -1265,7 +1288,7 @@ error:
}
}
static void parseConfigFileSystem(FFinstance* instance, FFdata* data)
FF_UNUSED_PARAM static void parseConfigFileSystem(FFinstance* instance, FFdata* data)
{
parseConfigFile(instance, data, FASTFETCH_TARGET_DIR_INSTALL_SYSCONF"/fastfetch/config.conf");
}
@@ -1278,10 +1301,9 @@ static void parseConfigFileUser(FFinstance* instance, FFdata* data)
FFstrbuf* filename = ffListGet(&instance->state.configDirs, 0);
uint32_t filenameLength = filename->length;
ffStrbufAppendS(filename, "/fastfetch/config.conf");
ffStrbufAppendS(filename, "fastfetch/config.conf");
if(!parseConfigFile(instance, data, filename->chars))
ffWriteFileData(filename->chars, sizeof(FASTFETCH_DATATEXT_CONFIG_USER), FASTFETCH_DATATEXT_CONFIG_USER);
parseConfigFile(instance, data, filename->chars);
ffStrbufSubstrBefore(filename, filenameLength);
}
@@ -1424,7 +1446,7 @@ int main(int argc, const char** argv)
data.loadUserConfig = true;
#ifndef _WIN32
parseConfigFileSystem(&instance, &data);
parseConfigFileSystem(&instance, &data);
#endif
parseConfigFileUser(&instance, &data);