From 3dc1e30b7736988328c3d357cf4cf0193d6510a4 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sun, 8 Dec 2024 20:58:31 +0100 Subject: [PATCH] GDALTermProgress(): implement OSC 9;4 progress reporting protocol as supported by ConEmu, recent PowerShell and other terminals On Windows, only use it if ConEmu or Windows Terminal specific environment variables are detected. Fixes #11460 --- port/cpl_known_config_options.h | 1 + port/cpl_progress.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/port/cpl_known_config_options.h b/port/cpl_known_config_options.h index 1dfefb8400ea..88a949d1048b 100644 --- a/port/cpl_known_config_options.h +++ b/port/cpl_known_config_options.h @@ -378,6 +378,7 @@ constexpr static const char* const apszKnownConfigOptions[] = "GDAL_STACTA_SKIP_MISSING_METATILE", // from stactadataset.cpp "GDAL_SWATH_SIZE", // from gdalmultidim.cpp, rasterio.cpp "GDAL_TEMP_DRIVER_NAME", // from nearblack_lib_floodfill.cpp + "GDAL_TERM_PROGRESS_OSC_9_4", // from cpl_progress.cpp "GDAL_TIFF_DEFLATE_SUBCODEC", // from gtiffdataset.cpp "GDAL_TIFF_ENDIANNESS", // from gtiffdataset_write.cpp "GDAL_TIFF_INTERNAL_MASK", // from gtiffdataset_write.cpp diff --git a/port/cpl_progress.cpp b/port/cpl_progress.cpp index 43bb7d658fc5..5ee6786067c5 100644 --- a/port/cpl_progress.cpp +++ b/port/cpl_progress.cpp @@ -19,6 +19,7 @@ #include #include "cpl_conv.h" +#include "cpl_string.h" /************************************************************************/ /* GDALDummyProgress() */ @@ -276,6 +277,34 @@ int CPL_STDCALL GDALTermProgress(double dfComplete, fprintf(stdout, "\b"); nLastTick = -1; nCharacterCountLastTime = 0; + +#ifdef _WIN32 + constexpr const char *WINDOWS_TERMINAL_ENV_VAR = "WT_SESSION"; + constexpr const char *CONEMU_ENV_VAR = "ConEmuANSI"; +#endif + static const bool bAllowOSC94 = CPLTestBool(CPLGetConfigOption( + "GDAL_TERM_PROGRESS_OSC_9_4", +#ifdef _WIN32 + // Detect if we are running under Windows Terminal + (CPLGetConfigOption(WINDOWS_TERMINAL_ENV_VAR, nullptr) != nullptr || + // or ConEmu + CPLGetConfigOption(CONEMU_ENV_VAR, nullptr) != nullptr) + ? "YES" + : "NO" +#else + "YES" +#endif + )); + if (bAllowOSC94) + { + // Implement OSC 9;4 progress reporting protocol + // https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC + if (nThisTick == MAX_TICKS) + fprintf(stdout, "\x1b]9;4;0;100\x07"); + else + fprintf(stdout, "\x1b]9;4;1;%d\x07", + (nThisTick * 100) / MAX_TICKS); + } } while (nThisTick > nLastTick)