Rework stdout buffering on Windows.

Windows doesn't support line-buffering. We enable full-buffering instead and flush buffers per module.
This commit is contained in:
李通洲 2022-12-14 16:27:04 +08:00
parent d151901049
commit e4a5194115
7 changed files with 22 additions and 19 deletions

View File

@ -35,7 +35,7 @@ jobs:
uses: github/codeql-action/analyze@v2
- name: run fastfetch
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all --unbuffered
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all
- name: run flashfetch
run: time ./flashfetch
@ -81,7 +81,7 @@ jobs:
uses: github/codeql-action/analyze@v2
- name: run fastfetch
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all --unbuffered
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all
- name: run flashfetch
run: time ./flashfetch
@ -113,7 +113,7 @@ jobs:
run: |
cmake -DSET_TWEAK=Off -DBUILD_TESTS=On .
cmake --build . --target package
time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all --unbuffered
time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all
time ./flashfetch
ctest
@ -180,7 +180,7 @@ jobs:
run: cp /clang64/bin/{libcjson,libOpenCL,vulkan-1}.dll .
- name: run fastfetch
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all --unbuffered
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all
- name: run flashfetch
run: time ./flashfetch
@ -242,7 +242,7 @@ jobs:
languages: c, cpp
- name: configure project
run: env PKG_CONFIG_PATH=/mingw64/lib/pkgconfig/:$PKG_CONFIG_PATH cmake -DSET_TWEAK=Off -DBUILD_TESTS=On .
run: env PKG_CONFIG_PATH=/mingw64/lib/pkgconfig/:$PKG_CONFIG_PATH cmake -DSET_TWEAK=Off -DBUILD_TESTS=On -DENABLE_BUFFER=Off .
- name: build project
run: cmake --build .
@ -254,7 +254,7 @@ jobs:
run: cp /mingw64/bin/{libcjson,libOpenCL,vulkan-1}.dll .
- name: run fastfetch
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all --unbuffered
run: time ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all
- name: run flashfetch
run: time ./flashfetch

View File

@ -70,6 +70,7 @@ cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX OR BSD OR WIN32"
cmake_dependent_option(ENABLE_LIBCJSON "Enable libcjson" ON "LINUX OR WIN32" OFF)
cmake_dependent_option(ENABLE_FREETYPE "Enable freetype" ON "ANDROID" OFF)
cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND AND NOT ANDROID" OFF)
cmake_dependent_option(ENABLE_BUFFER "Enable stdout buffer" ON "LINUX OR APPLE OR BSD OR WIN32 OR ANDROID" OFF)
cmake_dependent_option(USE_WIN_NTAPI "Allow using internal NTAPI" ON "WIN32" OFF)
cmake_dependent_option(USE_WIN_GPU_DXGI "Use DXGI to detect GPUs instead of WMI. Faster, but may ignore GPUs that only support DX9" ON "WIN32" OFF)
@ -639,6 +640,11 @@ if(ENABLE_THREADS)
endif()
endif()
if(ENABLE_BUFFER)
target_compile_definitions(libfastfetch PRIVATE FF_ENABLE_BUFFER)
endif()
if(APPLE)
target_link_libraries(libfastfetch
PRIVATE "-framework CoreFoundation"

View File

@ -176,7 +176,6 @@ __fastfetch_completion()
"--multithreading"
"--stat"
"--allow-slow-operations"
"--unbuffered"
"--disable-linewrap"
"--hide-cursor"
"--cpu-temp"

View File

@ -354,6 +354,9 @@ void ffStart(FFinstance* instance)
ffHideCursor = instance->config.hideCursor && !instance->config.pipe;
#ifdef _WIN32
#ifdef FF_ENABLE_BUFFER
setvbuf(stdout, NULL, _IOFBF, 4096);
#endif
SetConsoleCtrlHandler(consoleHandler, TRUE);
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
@ -361,6 +364,9 @@ void ffStart(FFinstance* instance)
SetConsoleMode(hStdout, mode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
SetConsoleOutputCP(CP_UTF8);
#else
#ifndef FF_ENABLE_BUFFER
setvbuf(stdout, NULL, _IONBF, 0);
#endif
struct sigaction action = { .sa_handler = exitSignalHandler };
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);

View File

@ -40,12 +40,6 @@
# Default is false.
#--stat true
# Disable stdout buffer option:
# Sets if fastfetch should disable application buffer of stdout. Mainly for debugging purpose.
# Must be true or false.
# Default is false.
#--unbuffered false
# Slow operations option:
# Sets if fastfetch is allowed to use known slow operations to detect more / better values.
# Must be true or false.

View File

@ -20,7 +20,6 @@ General options:
--multithreading <?value>: use multiple threads to detect values
--stat <?value>: print time usage (in ms) for individual modules
--allow-slow-operations <?value>: allow operations that are usually very slow for more detailed output
--unbuffered <?value>: disable stdout application buffer
--escape-bedrock <?value>: on bedrock linux, sets if it should escape the bedrock jail or not
--pipe <?value>: disable logo and all escape sequences

View File

@ -916,11 +916,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
}
else if(strcasecmp(key, "--allow-slow-operations") == 0)
instance->config.allowSlowOperations = optionParseBoolean(value);
else if(strcasecmp(key, "--unbuffered") == 0)
{
if(optionParseBoolean(value))
setvbuf(stdout, NULL, _IONBF, 0);
}
else if(strcasecmp(key, "--escape-bedrock") == 0)
instance->config.escapeBedrock = optionParseBoolean(value);
else if(strcasecmp(key, "--pipe") == 0)
@ -1435,6 +1430,10 @@ int main(int argc, const char** argv)
printf("\033[s\033[1A\033[9999999C\033[%dD%s\033[u", len, str); // Save; Up 1; Right 9999999; Left <len>; Print <str>; Load
}
#if defined(_WIN32) && defined(FF_ENABLE_BUFFER)
fflush(stdout);
#endif
startIndex = colonIndex + 1;
}