From a0f579072f279ff8f483ee19fce5f82d93474750 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Wed, 21 May 2025 16:40:42 +0800 Subject: [PATCH] Disk: add `--disk-hide-fs` Fix #1762 --- doc/json_schema.json | 4 ++++ src/data/help.json | 10 +++++++++- src/detection/disk/disk.c | 9 --------- src/modules/disk/disk.c | 25 +++++++++++++++++++++++++ src/modules/disk/option.h | 1 + src/util/FFstrbuf.c | 29 +++++++++++++++++++++++++++++ src/util/FFstrbuf.h | 11 +++++++++++ tests/strbuf.c | 14 ++++++++++++++ 8 files changed, 93 insertions(+), 10 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index 597c2fe31..10634242c 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -1758,6 +1758,10 @@ "type": "string", "description": "A colon (semicolon on Windows) separated list of folder paths to hide from the disk output\nDefault: /efi:/boot:/boot/efi" }, + "hideFS": { + "type": "string", + "description": "A colon separated file systems to hide from the disk output" + }, "showExternal": { "type": "boolean", "description": "Set if external volume should be printed", diff --git a/src/data/help.json b/src/data/help.json index 7794deeaa..68f67d17f 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -845,7 +845,15 @@ "desc": "A colon (semicolon on Windows) separated list of folder paths to hide from the disk output", "arg": { "type": "path", - "default": "Auto detection using mount-points" + "default": "/efi:/boot:/boot/efi" + } + }, + { + "long": "disk-hide-fs", + "desc": "A colon separated list of file systems to hide from the disk output", + "arg": { + "type": "string", + "default": "" } }, { diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index d52ea948d..0b43027fa 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -54,14 +54,5 @@ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) } } - if (options->hideFolders.length) - { - FF_LIST_FOR_EACH(FFDisk, disk, *disks) - { - if (ffDiskMatchMountpoint(&options->hideFolders, disk->mountpoint.chars)) - disk->type |= FF_DISK_VOLUME_TYPE_HIDDEN_BIT; - } - } - return NULL; } diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index 480721739..edd84dc35 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -179,6 +179,12 @@ void ffPrintDisk(FFDiskOptions* options) if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes)) continue; + if (options->hideFolders.length && ffDiskMatchMountpoint(&options->hideFolders, disk->mountpoint.chars)) + continue; + + if (options->hideFS.length && ffStrbufMatchSeparated(&disk->filesystem, &options->hideFS, ':')) + continue; + printDisk(options, disk, ++index); } } @@ -211,6 +217,12 @@ bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const ch return true; } + if (ffStrEqualsIgnCase(subKey, "hide-fs")) + { + ffOptionParseString(key, value, &options->hideFS); + return true; + } + if (ffStrEqualsIgnCase(subKey, "show-regular")) { if (ffOptionParseBoolean(value)) @@ -305,6 +317,12 @@ void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module) continue; } + if (ffStrEqualsIgnCase(key, "hideFS")) + { + ffStrbufSetS(&options->hideFS, yyjson_get_str(val)); + continue; + } + if (ffStrEqualsIgnCase(key, "showExternal")) { if (yyjson_get_bool(val)) @@ -397,6 +415,9 @@ void ffGenerateDiskJsonConfig(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso if (!ffStrbufEqual(&options->hideFolders, &defaultOptions.hideFolders)) yyjson_mut_obj_add_strbuf(doc, module, "hideFolders", &options->hideFolders); + if (!ffStrbufEqual(&options->hideFS, &defaultOptions.hideFS)) + yyjson_mut_obj_add_strbuf(doc, module, "hideFS", &options->hideFS); + if (defaultOptions.calcType != options->calcType) yyjson_mut_obj_add_bool(doc, module, "useAvailable", options->calcType == FF_DISK_CALC_TYPE_AVAILABLE); @@ -516,6 +537,7 @@ void ffInitDiskOptions(FFDiskOptions* options) #else ffStrbufInitStatic(&options->hideFolders, "/efi:/boot:/boot/efi"); #endif + ffStrbufInit(&options->hideFS); options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT | FF_DISK_VOLUME_TYPE_READONLY_BIT; options->calcType = FF_DISK_CALC_TYPE_FREE; options->percent = (FFPercentageModuleConfig) { 50, 80, 0 }; @@ -524,4 +546,7 @@ void ffInitDiskOptions(FFDiskOptions* options) void ffDestroyDiskOptions(FFDiskOptions* options) { ffOptionDestroyModuleArg(&options->moduleArgs); + ffStrbufDestroy(&options->folders); + ffStrbufDestroy(&options->hideFolders); + ffStrbufDestroy(&options->hideFS); } diff --git a/src/modules/disk/option.h b/src/modules/disk/option.h index 855f6981c..427e159f9 100644 --- a/src/modules/disk/option.h +++ b/src/modules/disk/option.h @@ -30,6 +30,7 @@ typedef struct FFDiskOptions FFstrbuf folders; FFstrbuf hideFolders; + FFstrbuf hideFS; FFDiskVolumeType showTypes; FFDiskCalcType calcType; FFPercentageModuleConfig percent; diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 32692907a..3fcebbd2e 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -607,3 +607,32 @@ bool ffStrbufRemoveDupWhitespaces(FFstrbuf* strbuf) return changed; } + +/// @brief Check if a separated string contains a substring. +/// @param strbuf The substring to check. +/// @param compLength The length of the separated string to check. +/// @param comp The separated string to check. +/// @param separator The separator character. +bool ffStrbufMatchSeparatedNS(const FFstrbuf* strbuf, uint32_t compLength, const char* comp, char separator) +{ + if (strbuf->length == 0) + return true; + + if (compLength == 0) + return false; + + for (const char* p = comp; p < comp + compLength;) + { + const char* colon = memchr(p, separator, compLength); + if (colon == NULL) + return strcmp(strbuf->chars, p) == 0; + + uint32_t substrLength = (uint32_t) (colon - p); + if (strbuf->length == substrLength && memcmp(strbuf->chars, p, substrLength) == 0) + return true; + + p = colon + 1; + } + + return false; +} diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index cd99c863f..841a4634d 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -92,6 +92,7 @@ void ffStrbufLowerCase(FFstrbuf* strbuf); bool ffStrbufGetline(char** lineptr, size_t* n, FFstrbuf* buffer); void ffStrbufGetlineRestore(char** lineptr, size_t* n, FFstrbuf* buffer); bool ffStrbufRemoveDupWhitespaces(FFstrbuf* strbuf); +bool ffStrbufMatchSeparatedNS(const FFstrbuf* strbuf, uint32_t compLength, const char* comp, char separator); FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate) { @@ -506,4 +507,14 @@ static inline void ffStrbufTrim(FFstrbuf* strbuf, char c) ffStrbufTrimLeft(strbuf, c); } +static inline bool ffStrbufMatchSeparatedS(const FFstrbuf* strbuf, const char* comp, char separator) +{ + return ffStrbufMatchSeparatedNS(strbuf, (uint32_t) strlen(comp), comp, separator); +} + +static inline bool ffStrbufMatchSeparated(const FFstrbuf* strbuf, const FFstrbuf* comp, char separator) +{ + return ffStrbufMatchSeparatedNS(strbuf, comp->length, comp->chars, separator); +} + #define FF_STRBUF_AUTO_DESTROY FFstrbuf __attribute__((__cleanup__(ffStrbufDestroy))) diff --git a/tests/strbuf.c b/tests/strbuf.c index d3f9116ca..0b786e18d 100644 --- a/tests/strbuf.c +++ b/tests/strbuf.c @@ -635,6 +635,20 @@ int main(void) VERIFY(ffStrbufEqualS(&newStr, "")); } + { + ffStrbufSetStatic(&strbuf, "abc"); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "abc:def:ghi", ' ') == false); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "abc:def:ghi", ':') == true); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "def:ghi", ' ') == false); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "def:ghi", ':') == false); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "def", ':') == false); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "abc", ':') == true); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "", ' ') == false); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, ":abc:", ':') == true); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, "abc:", ':') == true); + VERIFY(ffStrbufMatchSeparatedS(&strbuf, ":abc", ':') == true); + } + //Success puts("\e[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET); }