Files
fastfetch/src/modules/disk.c
T

118 lines
3.6 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include <sys/statvfs.h>
2021-04-15 18:59:26 +02:00
#define FF_DISK_MODULE_NAME "Disk"
#define FF_DISK_NUM_FORMAT_ARGS 4
2021-04-03 21:51:50 +02:00
static void getKey(FFinstance* instance, FFstrbuf* key, const char* folderPath, bool showFolderPath)
{
if(instance->config.diskKey.length == 0)
{
if(showFolderPath)
2021-04-15 18:59:26 +02:00
ffStrbufAppendF(key, FF_DISK_MODULE_NAME" (%s)", folderPath);
2021-04-03 21:51:50 +02:00
else
2021-04-15 18:59:26 +02:00
ffStrbufSetS(key, FF_DISK_MODULE_NAME);
2021-04-03 21:51:50 +02:00
}
else
{
2021-04-15 18:59:26 +02:00
ffParseFormatString(key, &instance->config.diskKey, NULL, 1, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRING, folderPath}
});
2021-04-03 21:51:50 +02:00
}
}
2021-04-03 11:39:59 +02:00
static void printStatvfs(FFinstance* instance, FFstrbuf* key, struct statvfs* fs)
2021-02-18 22:25:36 +01:00
{
2021-03-28 15:38:52 +02:00
const uint32_t GB = 1024 * 1024 * 1024;
2021-02-18 22:25:36 +01:00
2021-03-28 15:38:52 +02:00
uint32_t total = (fs->f_blocks * fs->f_frsize) / GB;
uint32_t available = (fs->f_bfree * fs->f_frsize) / GB;
uint32_t used = total - available;
2021-03-19 20:57:07 +01:00
uint8_t percentage = (used / (double) total) * 100.0;
2021-02-18 22:25:36 +01:00
2021-03-28 15:38:52 +02:00
uint32_t files = fs->f_files - fs->f_ffree;
2021-03-23 19:48:52 +01:00
if(instance->config.diskFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey(instance, key->chars, 0, NULL);
2021-03-19 20:57:07 +01:00
printf("%uGB / %uGB (%u%%)\n", used, total, percentage);
}
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString(instance, key->chars, 0, NULL, &instance->config.diskFormat, NULL, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_UINT, &used},
{FF_FORMAT_ARG_TYPE_UINT, &total},
{FF_FORMAT_ARG_TYPE_UINT, &files},
{FF_FORMAT_ARG_TYPE_UINT8, &percentage}
});
2021-03-19 20:57:07 +01:00
}
2021-03-27 18:14:19 +01:00
}
2021-03-28 15:38:52 +02:00
2021-04-03 11:39:59 +02:00
static void printStatvfsCreateKey(FFinstance* instance, const char* folderPath, struct statvfs* fs)
{
FF_STRBUF_CREATE(key);
2021-04-03 21:51:50 +02:00
getKey(instance, &key, folderPath, true);
2021-04-03 11:39:59 +02:00
printStatvfs(instance, &key, fs);
ffStrbufDestroy(&key);
}
2021-03-28 15:38:52 +02:00
static void printFolder(FFinstance* instance, const char* folderPath)
{
FF_STRBUF_CREATE(key);
2021-04-03 21:51:50 +02:00
getKey(instance, &key, folderPath, true);
2021-03-28 15:38:52 +02:00
struct statvfs fs;
int ret = statvfs(folderPath, &fs);
if(ret != 0 && instance->config.diskFormat.length == 0)
{
2021-04-15 18:59:26 +02:00
ffPrintError(instance, key.chars, 0, NULL, &instance->config.diskFormat, FF_DISK_NUM_FORMAT_ARGS, "statvfs(\"%s\", &fs) != 0 (%i)", folderPath, ret);
2021-03-28 15:38:52 +02:00
ffStrbufDestroy(&key);
return;
}
2021-04-03 11:39:59 +02:00
printStatvfs(instance, &key, &fs);
2021-03-28 15:38:52 +02:00
ffStrbufDestroy(&key);
}
void ffPrintDisk(FFinstance* instance)
{
if(instance->config.diskFolders.length == 0)
{
2021-04-03 21:51:50 +02:00
2021-03-28 15:38:52 +02:00
struct statvfs fsRoot;
int rootRet = statvfs("/", &fsRoot);
struct statvfs fsHome;
int homeRet = statvfs("/home", &fsHome);
if(rootRet != 0 && homeRet != 0)
{
2021-04-03 21:51:50 +02:00
FF_STRBUF_CREATE(key);
getKey(instance, &key, "/", false);
2021-04-15 18:59:26 +02:00
ffPrintError(instance, key.chars, 0, NULL, &instance->config.diskFormat, FF_DISK_NUM_FORMAT_ARGS, "statvfs failed for both / and /home");
2021-04-03 21:51:50 +02:00
ffStrbufDestroy(&key);
2021-03-28 15:38:52 +02:00
return;
}
if(rootRet == 0)
2021-04-03 11:39:59 +02:00
printStatvfsCreateKey(instance, "/", &fsRoot);
2021-03-28 15:38:52 +02:00
if(homeRet == 0 && (rootRet != 0 || fsRoot.f_fsid != fsHome.f_fsid))
2021-04-03 11:39:59 +02:00
printStatvfsCreateKey(instance, "/home", &fsHome);
2021-03-28 15:38:52 +02:00
}
else
{
uint32_t lastIndex = 0;
while (lastIndex < instance->config.diskFolders.length)
{
2021-03-28 20:09:29 +02:00
uint32_t colonIndex = ffStrbufFirstIndexAfterC(&instance->config.diskFolders, lastIndex, ':');
instance->config.diskFolders.chars[colonIndex] = '\0';
2021-03-28 15:38:52 +02:00
printFolder(instance, instance->config.diskFolders.chars + lastIndex);
2021-03-28 20:09:29 +02:00
2021-03-28 15:38:52 +02:00
lastIndex = colonIndex + 1;
}
}
}