From f23384725fcb8cf396cac2ce34245eb4512a1930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 21 Aug 2023 09:19:16 +0800 Subject: [PATCH] Version: add new module --- CHANGELOG.md | 1 + CMakeLists.txt | 2 + README.md | 2 +- doc/json_schema.json | 2 + presets/all | 2 +- presets/all.jsonc | 1 + src/common/init.c | 2 + src/common/modules.c | 1 + src/data/config_user.txt | 3 ++ src/detection/version/version.c | 35 +++++++++++++++++ src/detection/version/version.h | 20 ++++++++++ src/fastfetch.c | 11 ++++++ src/fastfetch.h | 1 + src/fastfetch_config.h.in | 1 + src/modules/modules.h | 1 + src/modules/options.h | 1 + src/modules/version/option.h | 11 ++++++ src/modules/version/version.c | 68 +++++++++++++++++++++++++++++++++ src/modules/version/version.h | 11 ++++++ 19 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 src/detection/version/version.c create mode 100644 src/detection/version/version.h create mode 100644 src/modules/version/option.h create mode 100644 src/modules/version/version.c create mode 100644 src/modules/version/version.h diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d677dc4..b9cf6773e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Features: * Add `--key-width` for aligning the left edge of values, supported both for global `--key-width` and specific module `--module-key-width` * Add `--bar-char-elapsed`, `--bar-char-total`, `--bar-width` and `--bar-border` options * Add CMake option `ENABLE_SYSTEM_YYJSON`, which allow building fastfetch with system-provided yyjson (for package managers) +* Add new module `Version`, which prints fastfetch version (like `fastfetch --version`) Bugfixes: * Fix label detection. Use `--disk-key 'Disk ({2})'` to display it (Disk, Linux) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dd0c06db..723b6cb0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -279,6 +279,7 @@ set(LIBFASTFETCH_SRC src/detection/packages/packages.c src/detection/terminalfont/terminalfont.c src/detection/terminalshell/terminalshell.c + src/detection/version/version.c src/detection/vulkan/vulkan.c src/logo/builtin.c src/logo/image/im6.c @@ -334,6 +335,7 @@ set(LIBFASTFETCH_SRC src/modules/title/title.c src/modules/uptime/uptime.c src/modules/users/users.c + src/modules/version/version.c src/modules/vulkan/vulkan.c src/modules/wallpaper/wallpaper.c src/modules/weather/weather.c diff --git a/README.md b/README.md index 61d2b0b15..271a91afd 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ All categories not listed here should work without needing a specific implementa ##### Available Modules ``` -Battery, Bios, Bluetooth, Board, Break, Brightness, Colors, Command, CPU, CPUUsage, Cursor, Custom, Date, DateTime, DE, Disk, Display, Font, Gamepad, GPU, Host, Icons, Kernel, LM, Locale, LocalIP, Media, Memory, Monitor, OpenCL, OpenGL, OS, Packages, Player, Power Adapter, Processes, PublicIP, Separator, Shell, Sound, Swap, Terminal, Terminal Font, Terminal Size, Theme, Time, Title, Uptime, Vulkan, Wallpaper, Weather, Wifi, WM, WMTheme +Battery, Bios, Bluetooth, Board, Break, Brightness, Colors, Command, CPU, CPUUsage, Cursor, Custom, Date, DateTime, DE, Disk, Display, Font, Gamepad, GPU, Host, Icons, Kernel, LM, Locale, LocalIP, Media, Memory, Monitor, OpenCL, OpenGL, OS, Packages, Player, Power Adapter, Processes, PublicIP, Separator, Shell, Sound, Swap, Terminal, Terminal Font, Terminal Size, Theme, Time, Title, Uptime, Version, Vulkan, Wallpaper, Weather, Wifi, WM, WMTheme ``` ##### Builtin logos diff --git a/doc/json_schema.json b/doc/json_schema.json index 909145c9a..4baa74a2d 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -558,6 +558,7 @@ "theme", "uptime", "users", + "version", "vulkan", "wallpaper", "weather", @@ -626,6 +627,7 @@ "theme", "uptime", "users", + "version", "vulkan", "wallpaper", "wm", diff --git a/presets/all b/presets/all index 48d24b4a7..1841c47ca 100644 --- a/presets/all +++ b/presets/all @@ -1 +1 @@ ---structure Title:Separator:OS:Host:Bios:Board:Chassis:Kernel:Uptime:Processes:Packages:Shell:Display:Brightness:Monitor:LM:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Wallpaper:Terminal:TerminalFont:TerminalSize:CPU:CPUUsage:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Media:PublicIP:LocalIP:Wifi:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Bluetooth:Sound:Gamepad:Weather:Break:Colors +--structure Title:Separator:OS:Host:Bios:Board:Chassis:Kernel:Uptime:Processes:Packages:Shell:Display:Brightness:Monitor:LM:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Wallpaper:Terminal:TerminalFont:TerminalSize:CPU:CPUUsage:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Media:PublicIP:LocalIP:Wifi:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Bluetooth:Sound:Gamepad:Weather:Version:Break:Colors diff --git a/presets/all.jsonc b/presets/all.jsonc index a1e722fae..b3e9c30d9 100644 --- a/presets/all.jsonc +++ b/presets/all.jsonc @@ -51,6 +51,7 @@ "sound", "gamepad", "weather", + "version", "break", "colors" ] diff --git a/src/common/init.c b/src/common/init.c index 4ef61b718..2f277ddfb 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -123,6 +123,7 @@ static void defaultConfig(void) ffInitTitleOptions(&instance.config.title); ffInitUptimeOptions(&instance.config.uptime); ffInitUsersOptions(&instance.config.users); + ffInitVersionOptions(&instance.config.version); ffInitVulkanOptions(&instance.config.vulkan); ffInitWMOptions(&instance.config.wm); ffInitWMThemeOptions(&instance.config.wmTheme); @@ -335,6 +336,7 @@ static void destroyConfig(void) ffDestroyTitleOptions(&instance.config.title); ffDestroyUptimeOptions(&instance.config.uptime); ffDestroyUsersOptions(&instance.config.users); + ffDestroyVersionOptions(&instance.config.version); ffDestroyVulkanOptions(&instance.config.vulkan); ffDestroyWMOptions(&instance.config.wm); ffDestroyWMThemeOptions(&instance.config.wmTheme); diff --git a/src/common/modules.c b/src/common/modules.c index 84d4cfb5c..1c4cd108a 100644 --- a/src/common/modules.c +++ b/src/common/modules.c @@ -133,6 +133,7 @@ static FFModuleBaseInfo* U[] = { }; static FFModuleBaseInfo* V[] = { + (void*) &instance.config.version, (void*) &instance.config.vulkan, NULL, }; diff --git a/src/data/config_user.txt b/src/data/config_user.txt index 619bdfdc3..7bd07a49e 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -376,6 +376,7 @@ #--player-key Media Player #--media-key Media #--datetime-key Date Time +#--version-key Version #--vulkan-key Vulkan #--opengl-key OpenGL #--opencl-key OpenCL @@ -426,6 +427,7 @@ #--player-format #--media-format #--datetime-format +#--version-format #--vulkan-format #--opengl-format #--opencl-format @@ -473,6 +475,7 @@ #--player-key-color #--media-key-color #--datetime-key-color +#--version-key-color #--vulkan-key-color #--opengl-key-color #--opencl-key-color diff --git a/src/detection/version/version.c b/src/detection/version/version.c new file mode 100644 index 000000000..4a8eb420c --- /dev/null +++ b/src/detection/version/version.c @@ -0,0 +1,35 @@ +#include "version.h" + +#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) + #define FF_ARCHITECTURE "x86_64" +#elif defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(__i586__) || defined(__i586) || defined(__i686__) || defined(__i686) + #define FF_ARCHITECTURE "i386" +#elif defined(__aarch64__) + #define FF_ARCHITECTURE "aarch64" +#elif defined(__arm__) + #define FF_ARCHITECTURE "arm" +#elif defined(__mips__) + #define FF_ARCHITECTURE "mips" +#elif defined(__powerpc__) || defined(__powerpc) + #define FF_ARCHITECTURE "powerpc" +#elif defined(__riscv__) || defined(__riscv) + #define FF_ARCHITECTURE "riscv" +#elif defined(__s390x__) + #define FF_ARCHITECTURE "s390x" +#else + #define FF_ARCHITECTURE "unknown" +#endif + +void ffDetectVersion(FFVersionResult* version) +{ + version->projectName = FASTFETCH_PROJECT_NAME; + version->architecture = FF_ARCHITECTURE; + version->version = FASTFETCH_PROJECT_VERSION; + version->versionTweak = FASTFETCH_PROJECT_VERSION_TWEAK; + version->cmakeBuiltType = FASTFETCH_PROJECT_CMAKE_BUILD_TYPE; + #ifndef NDEBUG + version->debugMode = true; + #else + version->debugMode = false; + #endif +} diff --git a/src/detection/version/version.h b/src/detection/version/version.h new file mode 100644 index 000000000..bdb11aa3a --- /dev/null +++ b/src/detection/version/version.h @@ -0,0 +1,20 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_version_version +#define FF_INCLUDED_detection_version_version + +#include "fastfetch.h" + +typedef struct FFVersionResult +{ + const char* projectName; + const char* architecture; + const char* version; + const char* versionTweak; + const char* cmakeBuiltType; + bool debugMode; +} FFVersionResult; + +void ffDetectVersion(FFVersionResult* version); + +#endif diff --git a/src/fastfetch.c b/src/fastfetch.c index b5c53f9a5..8d485bf82 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -456,6 +456,17 @@ static inline void printCommandHelp(const char* command) "second with leading zero" ); } + else if(ffStrEqualsIgnCase(command, "version-format")) + { + constructAndPrintCommandHelpFormat("version", "{1} {2}{3} (5)", 6, + "Project name", + "Version", + "Version tweak", + "Build type (debug or release)", + "Architecture", + "CMake build type (Debug, Release, RelWithDebInfo, MinSizeRel)" + ); + } else if(ffStrEqualsIgnCase(command, "vulkan-format")) { constructAndPrintCommandHelpFormat("vulkan", "{} (driver), {} (api version)", 3, diff --git a/src/fastfetch.h b/src/fastfetch.h index 543c64b30..6dfa675fa 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -126,6 +126,7 @@ typedef struct FFconfig FFTitleOptions title; FFUptimeOptions uptime; FFUsersOptions users; + FFVersionOptions version; FFVulkanOptions vulkan; FFWMOptions wm; FFWMThemeOptions wmTheme; diff --git a/src/fastfetch_config.h.in b/src/fastfetch_config.h.in index f91397389..2092c91d3 100644 --- a/src/fastfetch_config.h.in +++ b/src/fastfetch_config.h.in @@ -10,6 +10,7 @@ #define FASTFETCH_PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@ #define FASTFETCH_PROJECT_VERSION_TWEAK "@PROJECT_VERSION_TWEAK@" #define FASTFETCH_PROJECT_VERSION_TWEAK_NUM @PROJECT_VERSION_TWEAK_NUM@ +#define FASTFETCH_PROJECT_CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@" #define FASTFETCH_PROJECT_HOMEPAGE_URL "@PROJECT_HOMEPAGE_URL@" #define FASTFETCH_PROJECT_DESCRIPTION "@PROJECT_DESCRIPTION@" #define FASTFETCH_PROJECT_LICENSE "@PROJECT_LICENSE@" diff --git a/src/modules/modules.h b/src/modules/modules.h index c5e75e525..1f7669c10 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -50,6 +50,7 @@ #include "modules/title/title.h" #include "modules/uptime/uptime.h" #include "modules/users/users.h" +#include "modules/version/version.h" #include "modules/vulkan/vulkan.h" #include "modules/wallpaper/wallpaper.h" #include "modules/weather/weather.h" diff --git a/src/modules/options.h b/src/modules/options.h index 84bc4764b..12041ce5b 100644 --- a/src/modules/options.h +++ b/src/modules/options.h @@ -50,6 +50,7 @@ #include "modules/title/option.h" #include "modules/uptime/option.h" #include "modules/users/option.h" +#include "modules/version/option.h" #include "modules/vulkan/option.h" #include "modules/wallpaper/option.h" #include "modules/weather/option.h" diff --git a/src/modules/version/option.h b/src/modules/version/option.h new file mode 100644 index 000000000..12c151be0 --- /dev/null +++ b/src/modules/version/option.h @@ -0,0 +1,11 @@ +#pragma once + +// This file will be included in "fastfetch.h", do NOT put unnecessary things here + +#include "common/option.h" + +typedef struct FFVersionOptions +{ + FFModuleBaseInfo moduleInfo; + FFModuleArgs moduleArgs; +} FFVersionOptions; diff --git a/src/modules/version/version.c b/src/modules/version/version.c new file mode 100644 index 000000000..9cef899d2 --- /dev/null +++ b/src/modules/version/version.c @@ -0,0 +1,68 @@ +#include "common/printing.h" +#include "common/jsonconfig.h" +#include "detection/version/version.h" +#include "modules/version/version.h" +#include "util/stringUtils.h" + +#define FF_VERSION_NUM_FORMAT_ARGS 6 + +void ffPrintVersion(FFVersionOptions* options) +{ + FFVersionResult result = {}; + ffDetectVersion(&result); + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + printf("%s %s%s%s (%s)\n", result.projectName, result.version, result.versionTweak, result.debugMode ? "-debug" : "", result.architecture); + } + else + { + ffPrintFormat(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_VERSION_NUM_FORMAT_ARGS, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRING, result.projectName}, + {FF_FORMAT_ARG_TYPE_STRING, result.version}, + {FF_FORMAT_ARG_TYPE_STRING, result.versionTweak}, + {FF_FORMAT_ARG_TYPE_STRING, result.debugMode ? "debug" : "release"}, + {FF_FORMAT_ARG_TYPE_STRING, result.architecture}, + {FF_FORMAT_ARG_TYPE_STRING, result.cmakeBuiltType}, + }); + } +} + +void ffInitVersionOptions(FFVersionOptions* options) +{ + ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_VERSION_MODULE_NAME, ffParseVersionCommandOptions, ffParseVersionJsonObject, ffPrintVersion); + ffOptionInitModuleArg(&options->moduleArgs); +} + +bool ffParseVersionCommandOptions(FFVersionOptions* options, const char* key, const char* value) +{ + const char* subKey = ffOptionTestPrefix(key, FF_VERSION_MODULE_NAME); + if (!subKey) return false; + if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) + return true; + + return false; +} + +void ffDestroyVersionOptions(FFVersionOptions* options) +{ + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module) +{ + yyjson_val *key_, *val; + size_t idx, max; + yyjson_obj_foreach(module, idx, max, key_, val) + { + const char* key = yyjson_get_str(key_); + if(ffStrEqualsIgnCase(key, "type")) + continue; + + if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) + continue; + + ffPrintError(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); + } +} diff --git a/src/modules/version/version.h b/src/modules/version/version.h new file mode 100644 index 000000000..ef423b36e --- /dev/null +++ b/src/modules/version/version.h @@ -0,0 +1,11 @@ +#pragma once + +#include "fastfetch.h" + +#define FF_VERSION_MODULE_NAME "Version" + +void ffPrintVersion(FFVersionOptions* options); +void ffInitVersionOptions(FFVersionOptions* options); +bool ffParseVersionCommandOptions(FFVersionOptions* options, const char* key, const char* value); +void ffDestroyVersionOptions(FFVersionOptions* options); +void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module);