From 152b76fe8828fc8644119ba15aafa25eac044f0f Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Fri, 1 Nov 2024 17:58:12 +0700 Subject: [PATCH] fix: progress bar on CMD (#1609) --- engine/cli/utils/download_progress.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/engine/cli/utils/download_progress.cc b/engine/cli/utils/download_progress.cc index 9c38d4bdf..b47b4fc9a 100644 --- a/engine/cli/utils/download_progress.cc +++ b/engine/cli/utils/download_progress.cc @@ -36,6 +36,20 @@ bool DownloadProgress::Connect(const std::string& host, int port) { bool DownloadProgress::Handle(const DownloadType& event_type) { assert(!!ws_); +#if defined(_WIN32) + HANDLE h_out = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD dw_original_out_mode = 0; + if (h_out != INVALID_HANDLE_VALUE) { + GetConsoleMode(h_out, &dw_original_out_mode); + + // Enable ANSI escape code processing + DWORD dw_requested_out_mode = + dw_original_out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (!SetConsoleMode(h_out, dw_requested_out_mode)) { + SetConsoleMode(h_out, dw_original_out_mode); + } + } +#endif std::unordered_map totals; status_ = DownloadStatus::DownloadStarted; std::unique_ptr> bars; @@ -124,6 +138,11 @@ bool DownloadProgress::Handle(const DownloadType& event_type) { ws_->dispatch(handle_message); } indicators::show_console_cursor(true); +#if defined(_WIN32) + if (dw_original_out_mode != 0 && h_out != INVALID_HANDLE_VALUE) { + SetConsoleMode(h_out, dw_original_out_mode); + } +#endif if (status_ == DownloadStatus::DownloadError) return false; return true;