How to echo with different colors in the Windows command line

前端 未结 23 1199
野性不改
野性不改 2020-11-22 08:55

I know that the color bf command sets the colors of the whole command line window but I wanted to to print one single line in a different color.

相关标签:
23条回答
  • 2020-11-22 09:43

    you could use cecho.. you can also use it to embed right into your script so you dont have to carry along a .com or .exe

    http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files

    0 讨论(0)
  • 2020-11-22 09:43

    for me i found some solutions: it is a working solution

         @echo off
        title a game for youtube 
    explorer "https://thepythoncoding.blogspot.com/2020/11/how-to-echo-with-different-colors-in.html"
        SETLOCAL EnableDelayedExpansion
        for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
          set "DEL=%%a"
        )
        echo say the name of the colors, don't read
        
        call :ColorText 0a "blue"
        call :ColorText 0C "green"
        call :ColorText 0b "red"
        echo(
        call :ColorText 19 "yellow" 
        call :ColorText 2F "black"
        call :ColorText 4e "white"
        
        goto :Beginoffile
        
        :ColorText
        echo off
        <nul set /p ".=%DEL%" > "%~2"
        findstr /v /a:%1 /R "^$" "%~2" nul
        del "%~2" > nul 2>&1
        goto :eof
        
        :Beginoffile
    
    0 讨论(0)
  • 2020-11-22 09:46

    This is a self-compiled bat/.net hybrid (should be saved as .BAT) that can be used on any system that have installed .net framework (it's a rare thing to see an windows without .NET framework even for the oldest XP/2003 installations) . It uses jscript.net compiler to create an exe capable to print strings with different background/foreground color only for the current line.

    @if (@X)==(@Y) @end /* JScript comment
    @echo off
    setlocal
    
    for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
       set "jsc=%%v"
    )
    
    if not exist "%~n0.exe" (
        "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
    )
    
    %~n0.exe %*
    
    endlocal & exit /b %errorlevel%
    
    */
    
    import System;
    
    var arguments:String[] = Environment.GetCommandLineArgs();
    
    var newLine = false;
    var output = "";
    var foregroundColor = Console.ForegroundColor;
    var backgroundColor = Console.BackgroundColor;
    var evaluate = false;
    var currentBackground=Console.BackgroundColor;
    var currentForeground=Console.ForegroundColor;
    
    
    //http://stackoverflow.com/a/24294348/388389
    var jsEscapes = {
      'n': '\n',
      'r': '\r',
      't': '\t',
      'f': '\f',
      'v': '\v',
      'b': '\b'
    };
    
    function decodeJsEscape(_, hex0, hex1, octal, other) {
      var hex = hex0 || hex1;
      if (hex) { return String.fromCharCode(parseInt(hex, 16)); }
      if (octal) { return String.fromCharCode(parseInt(octal, 8)); }
      return jsEscapes[other] || other;
    }
    
    function decodeJsString(s) {
      return s.replace(
          // Matches an escape sequence with UTF-16 in group 1, single byte hex in group 2,
          // octal in group 3, and arbitrary other single-character escapes in group 4.
          /\\(?:u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([0-3][0-7]{0,2}|[4-7][0-7]?)|(.))/g,
          decodeJsEscape);
    }
    
    
    function printHelp( ) {
       print( arguments[0] + "  -s string [-f foreground] [-b background] [-n] [-e]" );
       print( " " );
       print( " string          String to be printed" );
       print( " foreground      Foreground color - a " );
       print( "                 number between 0 and 15." );
       print( " background      Background color - a " );
       print( "                 number between 0 and 15." );
       print( " -n              Indicates if a new line should" );
       print( "                 be written at the end of the ");
       print( "                 string(by default - no)." );
       print( " -e              Evaluates special character " );
       print( "                 sequences like \\n\\b\\r and etc ");
       print( "" );
       print( "Colors :" );
       for ( var c = 0 ; c < 16 ; c++ ) {
            
            Console.BackgroundColor = c;
            Console.Write( " " );
            Console.BackgroundColor=currentBackground;
            Console.Write( "-"+c );
            Console.WriteLine( "" );
       }
       Console.BackgroundColor=currentBackground;
       
       
    
    }
    
    function errorChecker( e:Error ) {
            if ( e.message == "Input string was not in a correct format." ) {
                print( "the color parameters should be numbers between 0 and 15" );
                Environment.Exit( 1 );
            } else if (e.message == "Index was outside the bounds of the array.") {
                print( "invalid arguments" );
                Environment.Exit( 2 );
            } else {
                print ( "Error Message: " + e.message );
                print ( "Error Code: " + ( e.number & 0xFFFF ) );
                print ( "Error Name: " + e.name );
                Environment.Exit( 666 );
            }
    }
    
    function numberChecker( i:Int32 ){
        if( i > 15 || i < 0 ) {
            print("the color parameters should be numbers between 0 and 15");
            Environment.Exit(1);
        }
    }
    
    
    if ( arguments.length == 1 || arguments[1].toLowerCase() == "-help" || arguments[1].toLowerCase() == "-help"   ) {
        printHelp();
        Environment.Exit(0);
    }
    
    for (var arg = 1; arg <= arguments.length-1; arg++ ) {
        if ( arguments[arg].toLowerCase() == "-n" ) {
            newLine=true;
        }
        
        if ( arguments[arg].toLowerCase() == "-e" ) {
            evaluate=true;
        }
        
        if ( arguments[arg].toLowerCase() == "-s" ) {
            output=arguments[arg+1];
        }
        
        
        if ( arguments[arg].toLowerCase() == "-b" ) {
            
            try {
                backgroundColor=Int32.Parse( arguments[arg+1] );
            } catch(e) {
                errorChecker(e);
            }
        }
        
        if ( arguments[arg].toLowerCase() == "-f" ) {
            try {
                foregroundColor=Int32.Parse(arguments[arg+1]);
            } catch(e) {
                errorChecker(e);
            }
        }
    }
    
    Console.BackgroundColor = backgroundColor ;
    Console.ForegroundColor = foregroundColor ;
    
    if ( evaluate ) {
        output=decodeJsString(output);
    }
    
    if ( newLine ) {
        Console.WriteLine(output);  
    } else {
        Console.Write(output);
        
    }
    
    Console.BackgroundColor = currentBackground;
    Console.ForegroundColor = currentForeground;
    

    Here's the help message:

    Example:

    coloroutput.bat -s "aa\nbb\n\u0025cc" -b 10 -f 3 -n -e

    You can also find this script here.

    You can also check carlos' color function -> http://www.dostips.com/forum/viewtopic.php?f=3&t=4453

    0 讨论(0)
  • 2020-11-22 09:47

    An alternative is to use NodeJS.

    Here is an example:

    const os = require('os');
    const colors = require('colors');
    
    console.log("Operative System:".green,os.type(),os.release());
    console.log("Uptime:".blue,os.uptime());
    

    And this is the result:

    0 讨论(0)
  • 2020-11-22 09:47
    call :color_echo "blue" "blue txt"
    call :color_echo "red" "red txt"
    echo "white txt"
    
    
    REM : https://www.robvanderwoude.com/ansi.php
    :color_echo
        @echo off
    
        set "color=%~1"
        set "txt=%~2"
    
        set ESC=
        set black=%ESC%[30m
        set red=%ESC%[31m
        set green=%ESC%[32m
        set yellow=%ESC%[33m
        set blue=%ESC%[34m
        set magenta=%ESC%[35m
        set cyan=%ESC%[36m
        set white=%ESC%[37m
    
        if "%~1" == "black"   set "color=!black!"
        if "%~1" == "red"     set "color=!red!"
        if "%~1" == "green"   set "color=!green!"
        if "%~1" == "yellow"  set "color=!yellow!"
        if "%~1" == "blue"    set "color=!blue!"
        if "%~1" == "magenta" set "color=!magenta!"
        if "%~1" == "cyan"    set "color=!cyan!"
        if "%~1" == "white"   set "color=!white!"
    
        echo | set /p="!color!!txt!"
        echo.
    
        REM : return to standard white color
        echo | set /p="!white!"
    
        REM : exiting the function only
        EXIT /B 0
    
    0 讨论(0)
  • 2020-11-22 09:47

    You can use the color command to change the color of the whole console

    Color 0F
    

    Is black and white

    Color 0A 
    

    Is black and green

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