Multiple foreground colors in PowerShell in one command

后端 未结 13 1745
难免孤独
难免孤独 2020-11-28 07:19

I want to output many different foreground colors with one statement.

PS C:\\> Write-Host \"Red\" -ForegroundColor Red
Red

This output i

相关标签:
13条回答
  • 2020-11-28 08:06

    If you are in my situation, I've found a plain vanilla way from microsoft docs to set console mode. So here is an easy way to start and end 256 colors ansi console support in both cmd and powershell:

    // https://docs.microsoft.com/en-us/windows/console/setconsolemode
    #include <Windows.h>
    #include <iostream>
    struct  console_data {
        HANDLE hstdin;
        DWORD  mode;
    
        DWORD start()
        {
            hstdin = GetStdHandle(STD_OUTPUT_HANDLE);
            GetConsoleMode(hstdin, &mode);
            if (!SetConsoleMode(hstdin, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
                DWORD E = GetLastError();
                std::cout << " Error #" << E << "in:"  __FUNCTION__ "\n";
                return GetLastError();
            }
            std::cout << "\033[1;14mReady.\e[0m\n";
            return 0;
        }
    
        void end()
        {
            SetConsoleMode(hstdin, mode);
        }
        ~console_data() {
            end();
        }
        //...
    }RTConsole;
    
    //...
    int main()
    {
       //... 
       RTConsole.start();
       std::cout << "\033[38;5;192m Should be 'Orange'\n";
       RTConsole.end();
       return 0;
    }
    

    https://docs.microsoft.com/en-us/windows/console/setconsolemode

    Note: I did not find how to get 256 colors support into VS Code's poswershell extension.

    0 讨论(0)
提交回复
热议问题