Files
fastfetch/src/modules/disk.c
T

124 lines
3.9 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include "common/printing.h"
2022-08-26 16:02:04 +02:00
#include "common/parsing.h"
#include "detection/disk/disk.h"
2021-02-18 22:25:36 +01:00
2021-04-15 18:59:26 +02:00
#define FF_DISK_MODULE_NAME "Disk"
2022-10-17 13:19:48 +02:00
#define FF_DISK_NUM_FORMAT_ARGS 9
2021-04-15 18:59:26 +02:00
2022-10-17 13:19:48 +02:00
static void printDisk(FFinstance* instance, const FFDisk* disk)
2021-04-03 21:51:50 +02:00
{
2022-10-06 22:56:56 +08:00
FFstrbuf key;
ffStrbufInit(&key);
2022-06-17 18:15:36 +02:00
if(instance->config.disk.key.length == 0)
2021-04-03 21:51:50 +02:00
{
2022-10-17 13:19:48 +02:00
ffStrbufAppendF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars);
2021-04-03 21:51:50 +02:00
}
else
{
2022-10-06 22:56:56 +08:00
ffParseFormatString(&key, &instance->config.disk.key, 1, (FFformatarg[]){
2022-10-17 13:19:48 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->mountpoint}
2021-04-15 18:59:26 +02:00
});
2021-04-03 21:51:50 +02:00
}
2022-09-06 13:24:57 +02:00
2022-08-26 16:02:04 +02:00
FFstrbuf usedPretty;
ffStrbufInit(&usedPretty);
2022-10-17 13:19:48 +02:00
ffParseSize(disk->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
2021-02-18 22:25:36 +01:00
2022-08-26 16:02:04 +02:00
FFstrbuf totalPretty;
ffStrbufInit(&totalPretty);
2022-10-17 13:19:48 +02:00
ffParseSize(disk->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
2021-03-28 15:38:52 +02:00
2022-06-17 18:15:36 +02:00
if(instance->config.disk.outputFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2022-10-06 22:56:56 +08:00
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
2022-10-17 13:19:48 +02:00
printf("%s / %s (%u%%)", usedPretty.chars, totalPretty.chars, disk->bytesPercentage);
if(disk->type == FF_DISK_TYPE_EXTERNAL)
printf(" [Removable]");
putchar('\n');
2021-03-19 20:57:07 +01:00
}
else
{
2022-10-06 22:56:56 +08:00
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.disk.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
2022-08-26 16:02:04 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
2022-10-17 13:19:48 +02:00
{FF_FORMAT_ARG_TYPE_UINT8, &disk->bytesPercentage},
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed},
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal},
{FF_FORMAT_ARG_TYPE_UINT8, &disk->filesPercentage},
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_EXTERNAL)},
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_HIDDEN)},
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem}
2021-04-15 18:59:26 +02:00
});
2021-03-19 20:57:07 +01:00
}
2022-09-06 13:24:57 +02:00
ffStrbufDestroy(&totalPretty);
ffStrbufDestroy(&usedPretty);
2022-10-17 13:19:48 +02:00
ffStrbufDestroy(&key);
2021-03-27 18:14:19 +01:00
}
2021-03-28 15:38:52 +02:00
2022-10-17 13:19:48 +02:00
static void printMountpoint(FFinstance* instance, const FFlist* disks, const char* mountpoint)
2022-09-06 13:24:57 +02:00
{
2022-10-17 13:19:48 +02:00
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;
}
}
2021-03-28 15:38:52 +02:00
2022-10-17 13:19:48 +02:00
ffPrintError(instance, FF_DISK_MODULE_NAME, 0, &instance->config.disk, "No disk found for mountpoint: %s", mountpoint);
}
2021-03-28 15:38:52 +02:00
2022-10-17 13:19:48 +02:00
static void printMountpoints(FFinstance* instance, const FFlist* disks)
{
FFstrbuf mountpoints;
ffStrbufInitCopy(&mountpoints, &instance->config.diskFolders);
ffStrbufTrim(&mountpoints, ':');
2021-03-28 15:38:52 +02:00
2022-10-17 13:19:48 +02:00
uint32_t startIndex = 0;
while(startIndex < mountpoints.length)
2022-10-06 22:56:56 +08:00
{
2022-10-17 13:19:48 +02:00
uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, ':');
mountpoints.chars[colonIndex] = '\0';
printMountpoint(instance, disks, mountpoints.chars + startIndex);
startIndex = colonIndex + 1;
2022-10-06 22:56:56 +08:00
}
2022-10-17 13:19:48 +02:00
}
static void printAutodetected(FFinstance* instance, const FFlist* disks)
{
for(uint32_t i = 0; i < disks->length; i++)
2021-03-28 15:38:52 +02:00
{
2022-10-17 13:19:48 +02:00
const FFDisk* disk = ffListGet(disks, i);
if(disk->type == FF_DISK_TYPE_EXTERNAL && !instance->config.diskShowRemovable)
continue;
if(disk->type == FF_DISK_TYPE_HIDDEN && !instance->config.diskShowHidden)
continue;
printDisk(instance, disk);
2021-03-28 15:38:52 +02:00
}
2022-10-17 13:19:48 +02:00
}
2022-09-06 13:24:57 +02:00
2022-10-17 13:19:48 +02:00
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);
2021-03-28 15:38:52 +02:00
}