Host: detect WSL version

This commit is contained in:
李通洲
2022-11-17 22:35:40 +08:00
parent 70847857f7
commit f1cc94207e
3 changed files with 25 additions and 9 deletions
-3
View File
@@ -5,9 +5,6 @@
#include "fastfetch.h"
#define FF_HOST_PRODUCT_NAME_WSL "Windows Subsystem for Linux"
#define FF_HOST_PRODUCT_NAME_MSYS "Windows on MSYS"
typedef struct FFHostResult
{
FFstrbuf productFamily;
+23 -1
View File
@@ -1,5 +1,6 @@
#include "host.h"
#include "common/io.h"
#include "common/processing.h"
#include <stdlib.h>
@@ -94,6 +95,27 @@ void ffDetectHostImpl(FFHostResult* host)
{
//On WSL, the real host can't be detected. Instead use WSL as host.
if(getenv("WSL_DISTRO") != NULL || getenv("WSL_INTEROP") != NULL)
ffStrbufAppendS(&host->productName, FF_HOST_PRODUCT_NAME_WSL);
{
ffStrbufAppendS(&host->productName, "Windows Subsystem for Linux");
FFstrbuf wslVer; //Wide charactors
ffStrbufInit(&wslVer);
if(!ffProcessAppendStdOut(&wslVer, (char* const[]){
"wsl.exe",
"--version",
NULL
}) && wslVer.length > 0)
{
ffStrbufSubstrBeforeFirstC(&wslVer, '\r'); //CRLF
ffStrbufSubstrAfterLastC(&wslVer, ' ');
ffStrbufAppendS(&host->productName, " (");
for(uint32_t i = 0; i < wslVer.length; ++i) {
if(wslVer.chars[i]) //don't append \0
ffStrbufAppendC(&host->productName, wslVer.chars[i]);
}
ffStrbufAppendC(&host->productName, ')');
}
ffStrbufDestroy(&wslVer);
}
}
}
@@ -1,5 +1,4 @@
#include "fastfetch.h"
#include "detection/host/host.h"
#include "common/io.h"
#include "common/parsing.h"
#include "common/processing.h"
@@ -239,10 +238,8 @@ static void getTerminalFromEnv(FFTerminalShellResult* result)
if(!ffStrSet(term))
{
//We are in WSL but not in Windows Terminal
const FFHostResult* host = ffDetectHost();
if(ffStrbufCompS(&host->productName, FF_HOST_PRODUCT_NAME_WSL) == 0 ||
ffStrbufCompS(&host->productName, FF_HOST_PRODUCT_NAME_MSYS) == 0) //TODO better WSL or MSYS detection
term = "conhost";
if(getenv("WSL_DISTRO") != NULL || getenv("WSL_INTEROP") != NULL)
term = "conhost";
}
#endif