2023-04-02 00:13:11 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-04-02 00:13:11 +08:00
|
|
|
#include "common/parsing.h"
|
|
|
|
|
#include "common/bar.h"
|
|
|
|
|
#include "detection/disk/disk.h"
|
|
|
|
|
#include "modules/disk/disk.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
#define FF_DISK_NUM_FORMAT_ARGS 10
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
static void printDisk(FFDiskOptions* options, const FFDisk* disk)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
if(options->moduleArgs.key.length == 0)
|
|
|
|
|
{
|
2023-07-16 23:00:39 +08:00
|
|
|
if(instance.config.pipe)
|
|
|
|
|
ffStrbufAppendF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars);
|
|
|
|
|
else
|
2023-07-31 20:29:33 +08:00
|
|
|
{
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
if (getenv("WSL_DISTRO_NAME") != NULL && getenv("WT_SESSION") != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (ffStrbufEqualS(&disk->filesystem, "9p") && ffStrbufStartsWithS(&disk->mountpoint, "/mnt/"))
|
|
|
|
|
ffStrbufAppendF(&key, "%s (\e]8;;file:///%c:/\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, disk->mountpoint.chars[5], disk->mountpoint.chars);
|
|
|
|
|
else
|
|
|
|
|
ffStrbufAppendF(&key, "%s (\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->mountpoint.chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
2023-07-16 23:00:39 +08:00
|
|
|
ffStrbufAppendF(&key, "%s (\e]8;;file://%s\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, disk->mountpoint.chars, disk->mountpoint.chars);
|
2023-07-31 20:29:33 +08:00
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffParseFormatString(&key, &options->moduleArgs.key, 1, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->mountpoint}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
|
2023-07-27 15:17:54 +08:00
|
|
|
ffParseSize(disk->bytesUsed, &usedPretty);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
|
2023-07-27 15:17:54 +08:00
|
|
|
ffParseSize(disk->bytesTotal, &totalPretty);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintLogoAndKey(key.chars, 0, NULL, &options->moduleArgs.keyColor);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
if(disk->bytesTotal > 0)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffAppendPercentBar(&str, bytesPercentage, 0, 5, 8);
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(!(instance.config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffAppendPercentNum(&str, (uint8_t) bytesPercentage, 50, 80, str.length > 0);
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ffStrbufAppendS(&str, "Unknown ");
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(!(instance.config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
2023-06-15 00:30:27 +08:00
|
|
|
{
|
2023-06-19 16:17:31 +08:00
|
|
|
if(disk->filesystem.length)
|
|
|
|
|
ffStrbufAppendF(&str, "- %s ", disk->filesystem.chars);
|
2023-06-15 00:30:27 +08:00
|
|
|
|
|
|
|
|
if(disk->type & FF_DISK_TYPE_EXTERNAL_BIT)
|
|
|
|
|
ffStrbufAppendS(&str, "[External]");
|
|
|
|
|
else if(disk->type & FF_DISK_TYPE_SUBVOLUME_BIT)
|
|
|
|
|
ffStrbufAppendS(&str, "[Subvolume]");
|
|
|
|
|
else if(disk->type & FF_DISK_TYPE_HIDDEN_BIT)
|
|
|
|
|
ffStrbufAppendS(&str, "[Hidden]");
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
ffStrbufTrimRight(&str, ' ');
|
|
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uint8_t filesPercentage = disk->filesTotal > 0 ? (uint8_t) (((double) disk->filesUsed / (double) disk->filesTotal) * 100.0) : 0;
|
|
|
|
|
|
2023-06-17 15:38:50 +08:00
|
|
|
bool isExternal = !!(disk->type & FF_DISK_TYPE_EXTERNAL_BIT);
|
|
|
|
|
bool isHidden = !!(disk->type & FF_DISK_TYPE_HIDDEN_BIT);
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormatString(key.chars, 0, NULL, &options->moduleArgs.keyColor, &options->moduleArgs.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2023-04-02 00:13:11 +08:00
|
|
|
{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},
|
2023-06-17 15:38:50 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_BOOL, &isExternal},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_BOOL, &isHidden},
|
2023-04-02 00:13:11 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
static void printMountpoint(FFDiskOptions* options, const FFlist* disks, const char* mountpoint)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-07-05 14:33:17 +08:00
|
|
|
FF_LIST_FOR_EACH(FFDisk, disk, *disks)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-07-05 14:33:17 +08:00
|
|
|
if(ffStrbufEqualS(&disk->mountpoint, mountpoint))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
printDisk(options, disk);
|
2023-04-02 00:13:11 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "No disk found for mountpoint: %s", mountpoint);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
static void printMountpoints(FFDiskOptions* options, const FFlist* disks)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
const char separator = ';';
|
|
|
|
|
#else
|
|
|
|
|
const char separator = ':';
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY mountpoints = ffStrbufCreateCopy(&options->folders);
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufTrim(&mountpoints, separator);
|
|
|
|
|
|
|
|
|
|
uint32_t startIndex = 0;
|
|
|
|
|
while(startIndex < mountpoints.length)
|
|
|
|
|
{
|
|
|
|
|
uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, separator);
|
|
|
|
|
mountpoints.chars[colonIndex] = '\0';
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
printMountpoint(options, disks, mountpoints.chars + startIndex);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
startIndex = colonIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
static void printAutodetected(FFDiskOptions* options, const FFlist* disks)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
FF_LIST_FOR_EACH(FFDisk, disk, *disks)
|
|
|
|
|
{
|
|
|
|
|
if(!(disk->type & options->showTypes))
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
printDisk(options, disk);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintDisk(FFDiskOptions* options)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-12 22:09:52 +08:00
|
|
|
FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk));
|
|
|
|
|
const char* error = ffDetectDisks(&disks);
|
|
|
|
|
|
|
|
|
|
if(error)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
else
|
2023-06-12 22:09:52 +08:00
|
|
|
{
|
|
|
|
|
if(options->folders.length == 0)
|
2023-06-24 10:59:53 +08:00
|
|
|
printAutodetected(options, &disks);
|
2023-06-12 22:09:52 +08:00
|
|
|
else
|
2023-06-24 10:59:53 +08:00
|
|
|
printMountpoints(options, &disks);
|
2023-06-12 22:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFDisk, disk, disks)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&disk->mountpoint);
|
|
|
|
|
ffStrbufDestroy(&disk->filesystem);
|
|
|
|
|
ffStrbufDestroy(&disk->name);
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "folders"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
ffOptionParseString(key, value, &options->folders);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-regular"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
|
|
|
|
options->showTypes |= FF_DISK_TYPE_REGULAR_BIT;
|
|
|
|
|
else
|
|
|
|
|
options->showTypes &= ~FF_DISK_TYPE_REGULAR_BIT;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-external"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
|
|
|
|
options->showTypes |= FF_DISK_TYPE_EXTERNAL_BIT;
|
|
|
|
|
else
|
|
|
|
|
options->showTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-hidden"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
|
|
|
|
options->showTypes |= FF_DISK_TYPE_HIDDEN_BIT;
|
|
|
|
|
else
|
|
|
|
|
options->showTypes &= ~FF_DISK_TYPE_HIDDEN_BIT;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-subvolumes"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
|
|
|
|
options->showTypes |= FF_DISK_TYPE_SUBVOLUME_BIT;
|
|
|
|
|
else
|
|
|
|
|
options->showTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-unknown"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffParseDiskJsonObject(yyjson_val* module)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
FFDiskOptions __attribute__((__cleanup__(ffDestroyDiskOptions))) options;
|
|
|
|
|
ffInitDiskOptions(&options);
|
|
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
2023-06-10 15:01:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-04-02 00:13:11 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "folders"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
ffStrbufSetS(&options.folders, yyjson_get_str(val));
|
2023-04-02 00:13:11 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showExternal"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
if (yyjson_get_bool(val))
|
2023-04-02 00:13:11 +08:00
|
|
|
options.showTypes |= FF_DISK_TYPE_EXTERNAL_BIT;
|
|
|
|
|
else
|
|
|
|
|
options.showTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showHidden"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
if (yyjson_get_bool(val))
|
2023-04-02 00:13:11 +08:00
|
|
|
options.showTypes |= FF_DISK_TYPE_HIDDEN_BIT;
|
|
|
|
|
else
|
|
|
|
|
options.showTypes &= ~FF_DISK_TYPE_HIDDEN_BIT;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showSubvolumes"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
if (yyjson_get_bool(val))
|
2023-04-02 00:13:11 +08:00
|
|
|
options.showTypes |= FF_DISK_TYPE_SUBVOLUME_BIT;
|
|
|
|
|
else
|
|
|
|
|
options.showTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showUnknown"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
if (yyjson_get_bool(val))
|
2023-04-02 00:13:11 +08:00
|
|
|
options.showTypes |= FF_DISK_TYPE_UNKNOWN_BIT;
|
|
|
|
|
else
|
|
|
|
|
options.showTypes &= ~FF_DISK_TYPE_UNKNOWN_BIT;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_DISK_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintDisk(&options);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|