JsonConfig: support generate default config file with --gen-config

This commit is contained in:
李通洲
2023-08-07 14:15:11 +08:00
parent cc1a35e379
commit 6071c1b09d
6 changed files with 85 additions and 21 deletions
+1
View File
@@ -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)
+5 -1
View File
@@ -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.
+43
View File
@@ -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"
]
}
+15 -15
View File
@@ -1,21 +1,21 @@
Usage: fastfetch <options>
Informative options:
-h,--help: Show this message
-h,--help <command>: 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 <command>: 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 <?type>: Generate a sample config file in the default path. Param `type` can be `jsonc` or `conf`
--gen-config-force <?type>: Generate a sample config file in the default path. Overwrite the existing one
General options:
--load-config <file>: Load a config file or preset (+)
+20 -5
View File
@@ -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"))
+1
View File
@@ -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@"