2021-02-18 22:25:36 +01:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2022-08-26 16:02:04 +02:00
|
|
|
#include "common/parsing.h"
|
2022-10-18 22:55:28 +08:00
|
|
|
#include "common/bar.h"
|
2022-09-23 19:45:23 +08:00
|
|
|
#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-18 18:39:28 +08:00
|
|
|
#define FF_DISK_NUM_FORMAT_ARGS 10
|
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-10-18 18:50:38 +08:00
|
|
|
uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0;
|
|
|
|
|
|
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-18 22:55:28 +08:00
|
|
|
|
|
|
|
|
FFstrbuf str;
|
|
|
|
|
ffStrbufInit(&str);
|
|
|
|
|
|
2022-10-18 18:50:38 +08:00
|
|
|
if(disk->bytesTotal > 0)
|
2022-10-18 22:55:28 +08:00
|
|
|
{
|
|
|
|
|
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
|
|
|
|
{
|
|
|
|
|
ffAppendPercentBar(instance, &str, bytesPercentage, 0, 5, 8);
|
|
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
2023-01-12 22:41:11 +08:00
|
|
|
|
|
|
|
|
if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
|
|
|
|
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
2022-10-18 22:55:28 +08:00
|
|
|
|
|
|
|
|
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
2023-01-12 22:41:11 +08:00
|
|
|
{
|
|
|
|
|
ffStrbufAppendF(&str, str.length > 0 ? "(%u%%) " : "%u%% ", bytesPercentage);
|
|
|
|
|
}
|
2022-10-18 22:55:28 +08:00
|
|
|
}
|
2022-10-18 18:50:38 +08:00
|
|
|
else
|
2023-01-12 22:41:11 +08:00
|
|
|
ffStrbufAppendS(&str, "Unknown ");
|
2022-10-18 18:50:38 +08:00
|
|
|
|
2023-01-12 22:41:11 +08:00
|
|
|
if(disk->type == FF_DISK_TYPE_EXTERNAL && !(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
|
|
|
|
ffStrbufAppendS(&str, "[Removable]");
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2023-01-12 22:41:11 +08:00
|
|
|
ffStrbufTrimRight(&str, ' ');
|
2022-10-18 22:55:28 +08:00
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
ffStrbufDestroy(&str);
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-18 18:50:38 +08:00
|
|
|
uint8_t filesPercentage = disk->filesTotal > 0 ? (uint8_t) (((double) disk->filesUsed / (double) disk->filesTotal) * 100.0) : 0;
|
|
|
|
|
|
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-18 18:50:38 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT8, &bytesPercentage},
|
2022-10-17 13:19:48 +02:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal},
|
2022-10-18 18:50:38 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT8, &filesPercentage},
|
2022-10-17 13:19:48 +02:00
|
|
|
{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)},
|
2022-10-18 18:39:28 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name}
|
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)
|
|
|
|
|
{
|
2022-10-22 13:07:38 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
const char separator = ';';
|
|
|
|
|
#else
|
|
|
|
|
const char separator = ':';
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
FFstrbuf mountpoints;
|
|
|
|
|
ffStrbufInitCopy(&mountpoints, &instance->config.diskFolders);
|
2022-10-22 13:07:38 +08:00
|
|
|
ffStrbufTrim(&mountpoints, separator);
|
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-22 13:07:38 +08:00
|
|
|
uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, separator);
|
2022-10-17 13:19:48 +02:00
|
|
|
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;
|
|
|
|
|
|
2023-01-10 17:31:47 +08:00
|
|
|
if(disk->bytesTotal == 0 && !instance->config.diskShowUnknown)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
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
|
|
|
}
|