mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Disk: support JSON config
This commit is contained in:
parent
2359b7e2f4
commit
623557f521
@ -277,7 +277,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/command/command.c
|
||||
src/modules/datetime/datetime.c
|
||||
src/modules/de.c
|
||||
src/modules/disk.c
|
||||
src/modules/disk/disk.c
|
||||
src/modules/font.c
|
||||
src/modules/gpu/gpu.c
|
||||
src/modules/host/host.c
|
||||
|
@ -86,7 +86,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitGPUOptions(&instance->config.gpu);
|
||||
initModuleArg(&instance->config.memory);
|
||||
initModuleArg(&instance->config.swap);
|
||||
initModuleArg(&instance->config.disk);
|
||||
ffInitDiskOptions(&instance->config.disk);
|
||||
ffInitBatteryOptions(&instance->config.battery);
|
||||
initModuleArg(&instance->config.powerAdapter);
|
||||
initModuleArg(&instance->config.locale);
|
||||
@ -136,9 +136,6 @@ static void defaultConfig(FFinstance* instance)
|
||||
instance->config.shellVersion = true;
|
||||
instance->config.terminalVersion = true;
|
||||
|
||||
ffStrbufInitA(&instance->config.diskFolders, 0);
|
||||
instance->config.diskShowTypes = FF_DISK_TYPE_REGULAR_BIT | FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
|
||||
instance->config.soundType = FF_SOUND_TYPE_MAIN;
|
||||
|
||||
instance->config.localIpShowType = FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
@ -316,7 +313,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyGPUOptions(&instance->config.gpu);
|
||||
destroyModuleArg(&instance->config.memory);
|
||||
destroyModuleArg(&instance->config.swap);
|
||||
destroyModuleArg(&instance->config.disk);
|
||||
ffDestroyDiskOptions(&instance->config.disk);
|
||||
ffDestroyBatteryOptions(&instance->config.battery);
|
||||
destroyModuleArg(&instance->config.powerAdapter);
|
||||
destroyModuleArg(&instance->config.locale);
|
||||
@ -363,7 +360,6 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffStrbufDestroy(&instance->config.libwlanapi);
|
||||
ffStrbufDestroy(&instance->config.libnm);
|
||||
|
||||
ffStrbufDestroy(&instance->config.diskFolders);
|
||||
ffStrbufDestroy(&instance->config.localIpNamePrefix);
|
||||
ffStrbufDestroy(&instance->config.publicIpUrl);
|
||||
ffStrbufDestroy(&instance->config.weatherOutputFormat);
|
||||
|
@ -1007,7 +1007,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseGPUCommandOptions(&instance->config.gpu, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "memory", &instance->config.memory)) {}
|
||||
else if(optionParseModuleArgs(key, value, "swap", &instance->config.swap)) {}
|
||||
else if(optionParseModuleArgs(key, value, "disk", &instance->config.disk)) {}
|
||||
else if(ffParseDiskCommandOptions(&instance->config.disk, key, value)) {}
|
||||
else if(ffParseBatteryCommandOptions(&instance->config.battery, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "poweradapter", &instance->config.powerAdapter)) {}
|
||||
else if(optionParseModuleArgs(key, value, "locale", &instance->config.locale)) {}
|
||||
@ -1095,18 +1095,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
instance->config.shellVersion = optionParseBoolean(value);
|
||||
else if(strcasecmp(key, "--terminal-version") == 0)
|
||||
instance->config.terminalVersion = optionParseBoolean(value);
|
||||
else if(strcasecmp(key, "--disk-folders") == 0)
|
||||
optionParseString(key, value, &instance->config.diskFolders);
|
||||
else if(strcasecmp(key, "--disk-show-regular") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.diskShowTypes |= FF_DISK_TYPE_REGULAR_BIT) : (instance->config.diskShowTypes &= ~FF_DISK_TYPE_REGULAR_BIT);
|
||||
else if(strcasecmp(key, "--disk-show-removable") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.diskShowTypes |= FF_DISK_TYPE_EXTERNAL_BIT) : (instance->config.diskShowTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT);
|
||||
else if(strcasecmp(key, "--disk-show-hidden") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.diskShowTypes |= FF_DISK_TYPE_HIDDEN_BIT) : (instance->config.diskShowTypes &= ~FF_DISK_TYPE_HIDDEN_BIT);
|
||||
else if(strcasecmp(key, "--disk-show-subvolumes") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.diskShowTypes |= FF_DISK_TYPE_SUBVOLUME_BIT) : (instance->config.diskShowTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT);
|
||||
else if(strcasecmp(key, "--disk-show-unknown") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.diskShowTypes |= FF_DISK_TYPE_UNKNOWN_BIT) : (instance->config.diskShowTypes &= ~FF_DISK_TYPE_UNKNOWN_BIT);
|
||||
else if(strcasecmp(key, "--sound-type") == 0)
|
||||
{
|
||||
optionParseEnum(key, value, &instance->config.soundType,
|
||||
@ -1264,8 +1252,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
ffPrintMemory(instance);
|
||||
else if(strcasecmp(line, "swap") == 0)
|
||||
ffPrintSwap(instance);
|
||||
else if(strcasecmp(line, "disk") == 0)
|
||||
ffPrintDisk(instance);
|
||||
else if(strcasecmp(line, FF_DISK_MODULE_NAME) == 0)
|
||||
ffPrintDisk(instance, &instance->config.disk);
|
||||
else if(strcasecmp(line, FF_BATTERY_MODULE_NAME) == 0)
|
||||
ffPrintBattery(instance, &instance->config.battery);
|
||||
else if(strcasecmp(line, "poweradapter") == 0)
|
||||
|
@ -33,16 +33,6 @@ typedef enum FFLocalIpCompactType
|
||||
FF_LOCALIP_COMPACT_TYPE_ONELINE,
|
||||
} FFLocalIpCompactType;
|
||||
|
||||
typedef enum FFDiskType
|
||||
{
|
||||
FF_DISK_TYPE_NONE = 0,
|
||||
FF_DISK_TYPE_REGULAR_BIT = 1 << 0,
|
||||
FF_DISK_TYPE_HIDDEN_BIT = 1 << 1,
|
||||
FF_DISK_TYPE_EXTERNAL_BIT = 1 << 2,
|
||||
FF_DISK_TYPE_SUBVOLUME_BIT = 1 << 3,
|
||||
FF_DISK_TYPE_UNKNOWN_BIT = 1 << 4,
|
||||
} FFDiskType;
|
||||
|
||||
typedef enum FFBinaryPrefixType
|
||||
{
|
||||
FF_BINARY_PREFIX_TYPE_IEC, // 1024 Bytes = 1 KiB, 1024 KiB = 1 MiB, ... (standard)
|
||||
@ -122,7 +112,7 @@ typedef struct FFconfig
|
||||
FFGPUOptions gpu;
|
||||
FFModuleArgs memory;
|
||||
FFModuleArgs swap;
|
||||
FFModuleArgs disk;
|
||||
FFDiskOptions disk;
|
||||
FFBatteryOptions battery;
|
||||
FFModuleArgs powerAdapter;
|
||||
FFModuleArgs locale;
|
||||
@ -170,9 +160,6 @@ typedef struct FFconfig
|
||||
bool shellVersion;
|
||||
bool terminalVersion;
|
||||
|
||||
FFstrbuf diskFolders;
|
||||
FFDiskType diskShowTypes;
|
||||
|
||||
FFstrbuf localIpNamePrefix;
|
||||
FFLocalIpType localIpShowType;
|
||||
|
||||
@ -258,7 +245,6 @@ void ffPrintTerminal(FFinstance* instance);
|
||||
void ffPrintTerminalFont(FFinstance* instance);
|
||||
void ffPrintMemory(FFinstance* instance);
|
||||
void ffPrintSwap(FFinstance* instance);
|
||||
void ffPrintDisk(FFinstance* instance);
|
||||
void ffPrintPowerAdapter(FFinstance* instance);
|
||||
void ffPrintLocale(FFinstance* instance);
|
||||
void ffPrintPlayer(FFinstance* instance);
|
||||
|
@ -47,7 +47,7 @@ int main(int argc, char** argv)
|
||||
ffPrintGPU(&instance, &instance.config.gpu);
|
||||
ffPrintMemory(&instance);
|
||||
//ffPrintSwap(&instance);
|
||||
ffPrintDisk(&instance);
|
||||
ffPrintDisk(&instance, &instance.config.disk);
|
||||
ffPrintBattery(&instance, &instance.config.battery);
|
||||
ffPrintPowerAdapter(&instance);
|
||||
//ffPrintPlayer(&instance);
|
||||
|
@ -1,157 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/parsing.h"
|
||||
#include "common/bar.h"
|
||||
#include "detection/disk/disk.h"
|
||||
|
||||
#define FF_DISK_MODULE_NAME "Disk"
|
||||
#define FF_DISK_NUM_FORMAT_ARGS 10
|
||||
|
||||
static void printDisk(FFinstance* instance, const FFDisk* disk)
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
if(instance->config.disk.key.length == 0)
|
||||
{
|
||||
ffStrbufAppendF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffParseFormatString(&key, &instance->config.disk.key, 1, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->mountpoint}
|
||||
});
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY usedPretty;
|
||||
ffStrbufInit(&usedPretty);
|
||||
ffParseSize(disk->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY totalPretty;
|
||||
ffStrbufInit(&totalPretty);
|
||||
ffParseSize(disk->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
|
||||
|
||||
uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0;
|
||||
|
||||
if(instance->config.disk.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY str;
|
||||
ffStrbufInit(&str);
|
||||
|
||||
if(disk->bytesTotal > 0)
|
||||
{
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(instance, &str, bytesPercentage, 0, 5, 8);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
|
||||
if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
||||
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
||||
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
{
|
||||
ffAppendPercentNum(instance, &str, (uint8_t) bytesPercentage, 50, 80, str.length > 0);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
}
|
||||
else
|
||||
ffStrbufAppendS(&str, "Unknown ");
|
||||
|
||||
if(disk->type & FF_DISK_TYPE_EXTERNAL_BIT && !(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
||||
ffStrbufAppendS(&str, "[Removable]");
|
||||
|
||||
ffStrbufTrimRight(&str, ' ');
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t filesPercentage = disk->filesTotal > 0 ? (uint8_t) (((double) disk->filesUsed / (double) disk->filesTotal) * 100.0) : 0;
|
||||
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.disk.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &bytesPercentage},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &filesPercentage},
|
||||
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type & FF_DISK_TYPE_EXTERNAL_BIT)},
|
||||
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type & FF_DISK_TYPE_HIDDEN_BIT)},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void printMountpoint(FFinstance* instance, const FFlist* disks, const char* mountpoint)
|
||||
{
|
||||
for(uint32_t i = disks->length; i > 0; i--)
|
||||
{
|
||||
FFDisk* disk = ffListGet(disks, i - 1);
|
||||
if(strncmp(mountpoint, disk->mountpoint.chars, disk->mountpoint.length) == 0)
|
||||
{
|
||||
printDisk(instance, disk);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &instance->config.disk, "No disk found for mountpoint: %s", mountpoint);
|
||||
}
|
||||
|
||||
static void printMountpoints(FFinstance* instance, const FFlist* disks)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const char separator = ';';
|
||||
#else
|
||||
const char separator = ':';
|
||||
#endif
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY mountpoints;
|
||||
ffStrbufInitCopy(&mountpoints, &instance->config.diskFolders);
|
||||
ffStrbufTrim(&mountpoints, separator);
|
||||
|
||||
uint32_t startIndex = 0;
|
||||
while(startIndex < mountpoints.length)
|
||||
{
|
||||
uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, separator);
|
||||
mountpoints.chars[colonIndex] = '\0';
|
||||
|
||||
printMountpoint(instance, disks, mountpoints.chars + startIndex);
|
||||
|
||||
startIndex = colonIndex + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void printAutodetected(FFinstance* instance, const FFlist* disks)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
FF_LIST_FOR_EACH(FFDisk, disk, *disks)
|
||||
{
|
||||
if(!(disk->type & instance->config.diskShowTypes))
|
||||
continue;
|
||||
|
||||
printDisk(instance, disk);
|
||||
found = true;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &instance->config.disk, "No disk found");
|
||||
}
|
||||
|
||||
void ffPrintDisk(FFinstance* instance)
|
||||
{
|
||||
const FFDiskResult* disks = ffDetectDisks();
|
||||
if(disks->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &instance->config.disk, "%s", disks->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.diskFolders.length == 0)
|
||||
printAutodetected(instance, &disks->disks);
|
||||
else
|
||||
printMountpoints(instance, &disks->disks);
|
||||
}
|
291
src/modules/disk/disk.c
Normal file
291
src/modules/disk/disk.c
Normal file
@ -0,0 +1,291 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/parsing.h"
|
||||
#include "common/bar.h"
|
||||
#include "detection/disk/disk.h"
|
||||
#include "modules/disk/disk.h"
|
||||
|
||||
#define FF_DISK_NUM_FORMAT_ARGS 10
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
|
||||
static void printDisk(FFinstance* instance, FFDiskOptions* options, const FFDisk* disk)
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
if(options->moduleArgs.key.length == 0)
|
||||
{
|
||||
ffStrbufAppendF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffParseFormatString(&key, &options->moduleArgs.key, 1, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->mountpoint}
|
||||
});
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY usedPretty;
|
||||
ffStrbufInit(&usedPretty);
|
||||
ffParseSize(disk->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY totalPretty;
|
||||
ffStrbufInit(&totalPretty);
|
||||
ffParseSize(disk->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
|
||||
|
||||
uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0;
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY str;
|
||||
ffStrbufInit(&str);
|
||||
|
||||
if(disk->bytesTotal > 0)
|
||||
{
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(instance, &str, bytesPercentage, 0, 5, 8);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
|
||||
if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
||||
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
||||
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
{
|
||||
ffAppendPercentNum(instance, &str, (uint8_t) bytesPercentage, 50, 80, str.length > 0);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
}
|
||||
else
|
||||
ffStrbufAppendS(&str, "Unknown ");
|
||||
|
||||
if((disk->type & FF_DISK_TYPE_EXTERNAL_BIT) && !(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
||||
ffStrbufAppendS(&str, "[Removable]");
|
||||
|
||||
ffStrbufTrimRight(&str, ' ');
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t filesPercentage = disk->filesTotal > 0 ? (uint8_t) (((double) disk->filesUsed / (double) disk->filesTotal) * 100.0) : 0;
|
||||
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &options->moduleArgs.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &bytesPercentage},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &filesPercentage},
|
||||
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type & FF_DISK_TYPE_EXTERNAL_BIT)},
|
||||
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type & FF_DISK_TYPE_HIDDEN_BIT)},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void printMountpoint(FFinstance* instance, FFDiskOptions* options, const FFlist* disks, const char* mountpoint)
|
||||
{
|
||||
for(uint32_t i = disks->length; i > 0; i--)
|
||||
{
|
||||
FFDisk* disk = ffListGet(disks, i - 1);
|
||||
if(strncmp(mountpoint, disk->mountpoint.chars, disk->mountpoint.length) == 0)
|
||||
{
|
||||
printDisk(instance, options, disk);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "No disk found for mountpoint: %s", mountpoint);
|
||||
}
|
||||
|
||||
static void printMountpoints(FFinstance* instance, FFDiskOptions* options, const FFlist* disks)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const char separator = ';';
|
||||
#else
|
||||
const char separator = ':';
|
||||
#endif
|
||||
|
||||
FFstrbuf mountpoints;
|
||||
ffStrbufInitCopy(&mountpoints, &options->folders);
|
||||
ffStrbufTrim(&mountpoints, separator);
|
||||
|
||||
uint32_t startIndex = 0;
|
||||
while(startIndex < mountpoints.length)
|
||||
{
|
||||
uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, separator);
|
||||
mountpoints.chars[colonIndex] = '\0';
|
||||
|
||||
printMountpoint(instance, options, disks, mountpoints.chars + startIndex);
|
||||
|
||||
startIndex = colonIndex + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void printAutodetected(FFinstance* instance, FFDiskOptions* options, const FFlist* disks)
|
||||
{
|
||||
FF_LIST_FOR_EACH(FFDisk, disk, *disks)
|
||||
{
|
||||
if(!(disk->type & options->showTypes))
|
||||
continue;
|
||||
|
||||
printDisk(instance, options, disk);
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintDisk(FFinstance* instance, FFDiskOptions* options)
|
||||
{
|
||||
const FFDiskResult* disks = ffDetectDisks();
|
||||
if(disks->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "%s", disks->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
if(options->folders.length == 0)
|
||||
printAutodetected(instance, options, &disks->disks);
|
||||
else
|
||||
printMountpoints(instance, options, &disks->disks);
|
||||
}
|
||||
|
||||
|
||||
void ffInitDiskOptions(FFDiskOptions* options)
|
||||
{
|
||||
options->moduleName = FF_DISK_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
ffStrbufInit(&options->folders);
|
||||
options->showTypes = FF_DISK_TYPE_REGULAR_BIT | FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
}
|
||||
|
||||
bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_DISK_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if (strcasecmp(subKey, "folders") == 0)
|
||||
{
|
||||
ffOptionParseString(key, value, &options->folders);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-regular") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showTypes |= FF_DISK_TYPE_REGULAR_BIT;
|
||||
else
|
||||
options->showTypes &= ~FF_DISK_TYPE_REGULAR_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-removable") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showTypes |= FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
else
|
||||
options->showTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-hidden") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showTypes |= FF_DISK_TYPE_HIDDEN_BIT;
|
||||
else
|
||||
options->showTypes &= ~FF_DISK_TYPE_HIDDEN_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-subvolumes") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showTypes |= FF_DISK_TYPE_SUBVOLUME_BIT;
|
||||
else
|
||||
options->showTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-unknown") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showTypes |= FF_DISK_TYPE_UNKNOWN_BIT;
|
||||
else
|
||||
options->showTypes &= ~FF_DISK_TYPE_UNKNOWN_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyDiskOptions(FFDiskOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseDiskJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFDiskOptions __attribute__((__cleanup__(ffDestroyDiskOptions))) options;
|
||||
ffInitDiskOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
if (strcasecmp(key, "folders") == 0)
|
||||
{
|
||||
ffStrbufSetS(&options.folders, json_object_get_string(val));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "showRemovable") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showTypes |= FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
else
|
||||
options.showTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "showHidden") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showTypes |= FF_DISK_TYPE_HIDDEN_BIT;
|
||||
else
|
||||
options.showTypes &= ~FF_DISK_TYPE_HIDDEN_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "showSubvolumes") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showTypes |= FF_DISK_TYPE_SUBVOLUME_BIT;
|
||||
else
|
||||
options.showTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "show-unknown") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showTypes |= FF_DISK_TYPE_UNKNOWN_BIT;
|
||||
else
|
||||
options.showTypes &= ~FF_DISK_TYPE_UNKNOWN_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintDisk(instance, &options);
|
||||
}
|
||||
#endif
|
15
src/modules/disk/disk.h
Normal file
15
src/modules/disk/disk.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_DISK_MODULE_NAME "Disk"
|
||||
|
||||
void ffPrintDisk(FFinstance* instance, FFDiskOptions* options);
|
||||
void ffInitDiskOptions(FFDiskOptions* options);
|
||||
bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const char* value);
|
||||
void ffDestroyDiskOptions(FFDiskOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseDiskJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
24
src/modules/disk/option.h
Normal file
24
src/modules/disk/option.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef enum FFDiskType
|
||||
{
|
||||
FF_DISK_TYPE_NONE = 0,
|
||||
FF_DISK_TYPE_REGULAR_BIT = 1 << 0,
|
||||
FF_DISK_TYPE_HIDDEN_BIT = 1 << 1,
|
||||
FF_DISK_TYPE_EXTERNAL_BIT = 1 << 2,
|
||||
FF_DISK_TYPE_SUBVOLUME_BIT = 1 << 3,
|
||||
FF_DISK_TYPE_UNKNOWN_BIT = 1 << 4,
|
||||
} FFDiskType;
|
||||
|
||||
typedef struct FFDiskOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFstrbuf folders;
|
||||
FFDiskType showTypes;
|
||||
} FFDiskOptions;
|
@ -52,6 +52,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
return
|
||||
tryModule(instance, type, module, FF_DATETIME_MODULE_NAME, ffParseDateTimeJsonObject) ||
|
||||
tryModule(instance, type, module, FF_DISPLAY_MODULE_NAME, ffParseDisplayJsonObject) ||
|
||||
tryModule(instance, type, module, FF_DISK_MODULE_NAME, ffParseDiskJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "modules/cursor/cursor.h"
|
||||
#include "modules/custom/custom.h"
|
||||
#include "modules/datetime/datetime.h"
|
||||
#include "modules/disk/disk.h"
|
||||
#include "modules/display/display.h"
|
||||
#include "modules/gpu/gpu.h"
|
||||
#include "modules/host/host.h"
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "modules/custom/option.h"
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/datetime/option.h"
|
||||
#include "modules/disk/option.h"
|
||||
#include "modules/display/option.h"
|
||||
#include "modules/host/option.h"
|
||||
#include "modules/gpu/option.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user