From 290e721ee1943086c99d21121ec514f76d946b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 13 Aug 2023 10:18:16 +0800 Subject: [PATCH] `--logo-source` / `--load-config`: support path with env variables (Windows) --- src/common/io/io.h | 1 + src/common/io/io_unix.c | 5 +++++ src/common/io/io_windows.c | 12 ++++++++++++ src/fastfetch.c | 11 +++++++++-- src/logo/logo.c | 5 +++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/common/io/io.h b/src/common/io/io.h index cfe730550..8cca6e147 100644 --- a/src/common/io/io.h +++ b/src/common/io/io.h @@ -82,6 +82,7 @@ typedef enum FFPathType #define FF_PATHTYPE_ANY (FF_PATHTYPE_FILE | FF_PATHTYPE_DIRECTORY) bool ffPathExists(const char* path, FFPathType pathType); +bool ffPathExpandEnv(const char* in, FFstrbuf* out); FF_C_SCANF(2, 3) const char* ffGetTerminalResponse(const char* request, const char* format, ...); diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index 7680cab81..f0a69c537 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -107,6 +107,11 @@ bool ffPathExists(const char* path, FFPathType type) return false; } +bool ffPathExpandEnv(const char* in, FFstrbuf* out) +{ + return false; +} + const char* ffGetTerminalResponse(const char* request, const char* format, ...) { struct termios oldTerm, newTerm; diff --git a/src/common/io/io_windows.c b/src/common/io/io_windows.c index bf19540a2..2cd916bd8 100644 --- a/src/common/io/io_windows.c +++ b/src/common/io/io_windows.c @@ -98,6 +98,18 @@ bool ffPathExists(const char* path, FFPathType type) return false; } +bool ffPathExpandEnv(const char* in, FFstrbuf* out) +{ + DWORD length = ExpandEnvironmentStringsA(in, NULL, 0); + if (length <= 1) return false; + + ffStrbufClear(out); + ffStrbufEnsureFree(out, (uint32_t)length); + ExpandEnvironmentStringsA(in, out->chars, length); + out->length = (uint32_t)length - 1; + return true; +} + bool ffSuppressIO(bool suppress) { (void) suppress; //Not implemented. diff --git a/src/fastfetch.c b/src/fastfetch.c index 8156b7244..e0e32f9d7 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -693,9 +693,16 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val if(isJsonConfig ? parseJsoncFile(value) : parseConfigFile(data, value)) return; - //Try to load as a relative path - FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateA(128); + if (ffPathExpandEnv(value, &absolutePath)) + { + bool success = isJsonConfig ? parseJsoncFile(value) : parseConfigFile(data, absolutePath.chars); + + if(success) + return; + } + + //Try to load as a relative path FF_LIST_FOR_EACH(FFstrbuf, path, instance.state.platform.dataDirs) { diff --git a/src/logo/logo.c b/src/logo/logo.c index 6cb49d1e9..f74818569 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -332,6 +332,11 @@ static void updateLogoPath(void) return; FF_STRBUF_AUTO_DESTROY fullPath = ffStrbufCreate(); + if (ffPathExpandEnv(options->source.chars, &fullPath) && ffPathExists(fullPath.chars, FF_PATHTYPE_FILE)) + { + ffStrbufSet(&options->source, &fullPath); + return; + } FF_LIST_FOR_EACH(FFstrbuf, dataDir, instance.state.platform.dataDirs) {