p-ranav / indicators

Activity Indicators for Modern C++
MIT License
3.04k stars 237 forks source link

In Windows console, the progress bar does not stay on the same line #131

Open arklev opened 7 months ago

arklev commented 7 months ago

image

The code used for this bar: indicators::IndeterminateProgressBar bar{ indicators::option::BarWidth{40}, indicators::option::Start{"["}, indicators::option::Fill{"·"}, indicators::option::Lead{"<==>"}, indicators::option::End{"]"}, indicators::option::PostfixText{"Waiting For Mission Ready"}, indicators::option::ForegroundColor{indicators::Color::yellow}, indicators::option::FontStyles{std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}, indicators::option::Stream{std::cout} };

When running the program inside the new "Windows Terminal" it works as expected, but the CMD is not, Is it the expected result or there is some hidden option to trigger for it to work? I am on windows 10.

tnnnnt commented 3 months ago

Hi, I have the same problem as you, have you solved it?

arklev commented 3 months ago

Hi, I have the same problem as you, have you solved it?

Yes, i was able to make it work by setting the ENABLE_VIRTUAL_TERMINAL_PROCESSING.

Run this code before you initialize the bar.

        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hOut != INVALID_HANDLE_VALUE)
    {
        DWORD dwOriginalOutMode = 0;
        GetConsoleMode(hOut, &dwOriginalOutMode);

        // Enable ANSI escape code processing
        DWORD dwRequestedOutModes = dwOriginalOutMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
        if (!SetConsoleMode(hOut, dwRequestedOutModes)) {
            SetConsoleMode(hOut, dwOriginalOutMode);
        }
    }
tnnnnt commented 3 months ago

Thanks, that was a great help!