Files
fastfetch/src/modules/packages/packages.c
T

213 lines
7.6 KiB
C
Raw Normal View History

#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2022-10-08 17:41:25 +08:00
#include "detection/packages/packages.h"
2023-05-26 14:32:27 +08:00
#include "modules/packages/packages.h"
#include "util/stringUtils.h"
2021-02-18 22:25:36 +01:00
#define FF_PACKAGES_NUM_FORMAT_ARGS 24
2022-09-15 14:17:33 -06:00
void ffPrintPackages(FFPackagesOptions* options)
2022-05-24 13:36:33 +02:00
{
2023-06-13 21:38:06 +08:00
FFPackagesResult counts = {};
ffStrbufInit(&counts.pacmanBranch);
2022-04-21 17:59:26 +02:00
const char* error = ffDetectPackages(&counts);
2023-06-13 21:38:06 +08:00
if(error)
2021-03-11 19:54:34 +01:00
{
ffPrintError(FF_PACKAGES_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2021-04-03 21:51:50 +02:00
return;
}
2021-05-15 18:17:34 +02:00
2023-05-26 14:32:27 +08:00
if(options->moduleArgs.outputFormat.length == 0)
2021-04-03 21:51:50 +02:00
{
ffPrintLogoAndKey(FF_PACKAGES_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2021-03-11 19:40:46 +01:00
2022-10-13 21:43:31 +02:00
#define FF_PRINT_PACKAGE_NAME(var, name) \
2023-06-13 21:38:06 +08:00
if(counts.var > 0) \
2022-10-13 21:43:31 +02:00
{ \
2023-06-13 21:38:06 +08:00
printf("%u (%s)", counts.var, (name)); \
if((all -= counts.var) > 0) \
2022-10-13 21:43:31 +02:00
printf(", "); \
};
#define FF_PRINT_PACKAGE(name) FF_PRINT_PACKAGE_NAME(name, #name)
2021-03-11 19:40:46 +01:00
2023-06-13 21:38:06 +08:00
uint32_t all = counts.all;
if(counts.pacman > 0)
2021-05-15 17:21:02 +02:00
{
2023-06-13 21:38:06 +08:00
printf("%u (pacman)", counts.pacman);
if(counts.pacmanBranch.length > 0)
printf("[%s]", counts.pacmanBranch.chars);
if((all -= counts.pacman) > 0)
2021-05-15 17:21:02 +02:00
printf(", ");
};
2021-05-15 18:12:10 +02:00
2021-05-01 21:22:07 +02:00
FF_PRINT_PACKAGE(dpkg)
2021-06-08 20:01:29 +02:00
FF_PRINT_PACKAGE(rpm)
2022-02-12 12:13:02 +01:00
FF_PRINT_PACKAGE(emerge)
2022-12-27 17:36:58 +08:00
FF_PRINT_PACKAGE(eopkg)
2022-08-12 09:40:02 +02:00
FF_PRINT_PACKAGE(xbps)
2022-10-13 21:43:31 +02:00
FF_PRINT_PACKAGE_NAME(nixSystem, "nix-system")
FF_PRINT_PACKAGE_NAME(nixUser, "nix-user")
FF_PRINT_PACKAGE_NAME(nixDefault, "nix-default")
2022-08-18 22:02:21 +02:00
FF_PRINT_PACKAGE(apk)
2022-09-20 11:16:08 +02:00
FF_PRINT_PACKAGE(pkg)
2023-06-13 21:38:06 +08:00
FF_PRINT_PACKAGE_NAME(flatpakSystem, counts.flatpakUser ? "flatpak-system" : "flatpak")
2023-03-24 17:47:03 +08:00
FF_PRINT_PACKAGE_NAME(flatpakUser, "flatpak-user")
2021-05-01 21:22:07 +02:00
FF_PRINT_PACKAGE(snap)
2022-09-05 05:15:19 -07:00
FF_PRINT_PACKAGE(brew)
FF_PRINT_PACKAGE_NAME(brewCask, "brew-cask")
2022-09-15 14:17:33 -06:00
FF_PRINT_PACKAGE(port)
2022-10-08 17:41:25 +08:00
FF_PRINT_PACKAGE(scoop)
2022-10-23 22:16:12 +08:00
FF_PRINT_PACKAGE(choco)
2023-07-03 23:18:36 +08:00
FF_PRINT_PACKAGE(pkgtool)
2023-07-20 16:43:29 +01:00
FF_PRINT_PACKAGE(paludis)
FF_PRINT_PACKAGE(winget)
2021-03-11 19:40:46 +01:00
putchar('\n');
}
else
{
ffPrintFormat(FF_PACKAGES_MODULE_NAME, 0, &options->moduleArgs, FF_PACKAGES_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-06-13 21:38:06 +08:00
{FF_FORMAT_ARG_TYPE_UINT, &counts.all},
{FF_FORMAT_ARG_TYPE_UINT, &counts.pacman},
{FF_FORMAT_ARG_TYPE_STRBUF, &counts.pacmanBranch},
{FF_FORMAT_ARG_TYPE_UINT, &counts.dpkg},
{FF_FORMAT_ARG_TYPE_UINT, &counts.rpm},
{FF_FORMAT_ARG_TYPE_UINT, &counts.emerge},
{FF_FORMAT_ARG_TYPE_UINT, &counts.eopkg},
{FF_FORMAT_ARG_TYPE_UINT, &counts.xbps},
{FF_FORMAT_ARG_TYPE_UINT, &counts.nixSystem},
{FF_FORMAT_ARG_TYPE_UINT, &counts.nixUser},
{FF_FORMAT_ARG_TYPE_UINT, &counts.nixDefault},
{FF_FORMAT_ARG_TYPE_UINT, &counts.apk},
{FF_FORMAT_ARG_TYPE_UINT, &counts.pkg},
{FF_FORMAT_ARG_TYPE_UINT, &counts.flatpakSystem},
{FF_FORMAT_ARG_TYPE_UINT, &counts.flatpakUser},
{FF_FORMAT_ARG_TYPE_UINT, &counts.snap},
{FF_FORMAT_ARG_TYPE_UINT, &counts.brew},
{FF_FORMAT_ARG_TYPE_UINT, &counts.brewCask},
{FF_FORMAT_ARG_TYPE_UINT, &counts.port},
{FF_FORMAT_ARG_TYPE_UINT, &counts.scoop},
{FF_FORMAT_ARG_TYPE_UINT, &counts.choco},
2023-07-03 23:18:36 +08:00
{FF_FORMAT_ARG_TYPE_UINT, &counts.pkgtool},
{FF_FORMAT_ARG_TYPE_UINT, &counts.paludis},
{FF_FORMAT_ARG_TYPE_UINT, &counts.winget},
2021-04-15 18:59:26 +02:00
});
}
2023-06-13 21:38:06 +08:00
ffStrbufDestroy(&counts.pacmanBranch);
2021-03-27 18:14:19 +01:00
}
2023-05-26 14:32:27 +08:00
void ffInitPackagesOptions(FFPackagesOptions* options)
{
2023-10-07 13:40:40 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_PACKAGES_MODULE_NAME, ffParsePackagesCommandOptions, ffParsePackagesJsonObject, ffPrintPackages, ffGeneratePackagesJson, ffPrintPackagesHelpFormat);
2023-05-26 14:32:27 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParsePackagesCommandOptions(FFPackagesOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_PACKAGES_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyPackagesOptions(FFPackagesOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2023-08-17 10:56:54 +08:00
void ffParsePackagesJsonObject(FFPackagesOptions* options, yyjson_val* module)
2023-05-26 14:32:27 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-05-26 14:32:27 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-05-26 14:32:27 +08:00
2023-08-17 16:09:41 +08:00
ffPrintError(FF_PACKAGES_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-05-26 14:32:27 +08:00
}
}
2023-09-08 15:44:44 +08:00
void ffGeneratePackagesJson(FF_MAYBE_UNUSED FFPackagesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FFPackagesResult counts = {};
ffStrbufInit(&counts.pacmanBranch);
const char* error = ffDetectPackages(&counts);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
#define FF_APPEND_PACKAGE_COUNT(name) yyjson_mut_obj_add_uint(doc, obj, #name, counts.name);
FF_APPEND_PACKAGE_COUNT(all)
FF_APPEND_PACKAGE_COUNT(apk)
FF_APPEND_PACKAGE_COUNT(brew)
FF_APPEND_PACKAGE_COUNT(brewCask)
FF_APPEND_PACKAGE_COUNT(choco)
FF_APPEND_PACKAGE_COUNT(dpkg)
FF_APPEND_PACKAGE_COUNT(emerge)
FF_APPEND_PACKAGE_COUNT(eopkg)
FF_APPEND_PACKAGE_COUNT(flatpakSystem)
FF_APPEND_PACKAGE_COUNT(flatpakUser)
FF_APPEND_PACKAGE_COUNT(nixDefault)
FF_APPEND_PACKAGE_COUNT(nixSystem)
FF_APPEND_PACKAGE_COUNT(nixUser)
FF_APPEND_PACKAGE_COUNT(pacman)
FF_APPEND_PACKAGE_COUNT(paludis)
FF_APPEND_PACKAGE_COUNT(pkg)
FF_APPEND_PACKAGE_COUNT(pkgtool)
FF_APPEND_PACKAGE_COUNT(port)
FF_APPEND_PACKAGE_COUNT(rpm)
FF_APPEND_PACKAGE_COUNT(scoop)
FF_APPEND_PACKAGE_COUNT(snap)
FF_APPEND_PACKAGE_COUNT(winget)
FF_APPEND_PACKAGE_COUNT(xbps)
yyjson_mut_obj_add_strbuf(doc, obj, "pacmanBranch", &counts.pacmanBranch);
}
2023-10-07 13:40:40 +08:00
void ffPrintPackagesHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_PACKAGES_MODULE_NAME, "{2} (pacman){?3}[{3}]{?}, {4} (dpkg), {5} (rpm), {6} (emerge), {7} (eopkg), {8} (xbps), {9} (nix-system), {10} (nix-user), {11} (nix-default), {12} (apk), {13} (pkg), {14} (flatpak-system), {15} (flatpack-user), {16} (snap), {17} (brew), {18} (brew-cask), {19} (port), {20} (scoop), {21} (choco), {22} (pkgtool), {23} (paludis), {24} (winget)", FF_PACKAGES_NUM_FORMAT_ARGS, (const char* []) {
"Number of all packages",
"Number of pacman packages",
"Pacman branch on manjaro",
"Number of dpkg packages",
"Number of rpm packages",
"Number of emerge packages",
"Number of eopkg packages",
"Number of xbps packages",
"Number of nix-system packages",
"Number of nix-user packages",
"Number of nix-default packages",
"Number of apk packages",
"Number of pkg packages",
"Number of flatpak-system packages",
"Number of flatpak-user packages",
"Number of snap packages",
"Number of brew packages",
"Number of brew-cask packages",
"Number of macports packages",
"Number of scoop packages",
"Number of choco packages",
"Number of pkgtool packages",
"Number of paludis packages"
"Number of winget packages"
});
}