Batch change color of user input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Batch change color of user input

#1 Post by batchcc » 17 Aug 2015 06:09

Is there a way I can change the color of what the user types when they are typing it
Set /p = what user types would be a different color than the rest of the fle
Thanks


Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Batch change color of user input

#3 Post by Meerkat » 17 Aug 2015 07:31

According to the page...
because right now the answer is simply "yes."

Sorry, but I do not understand that part. :?


Maybe this could help...
http://stackoverflow.com/questions/1514 ... ut-by-user

However, I think the task is not really good to do...

Meerkat

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Batch change color of user input

#4 Post by npocmaka_ » 17 Aug 2015 08:48

I think he/she wants to change the color of the content written by the user at the moment its written.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Batch change color of user input

#5 Post by npocmaka_ » 17 Aug 2015 10:30

At the end of the post is my attempt to achieve what I think you want.
The input of the user is colored , though the colors will be lost if the script is redirected , piped or processed with FOR /F.
But I suppose you'll want to preserve the output. The only option that I found working was (optional) writing to a file (later you can read it) . Here's the script:


Code: Select all

@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;
import System.IO;

var arguments:String[] = Environment.GetCommandLineArgs();

var foregroundColor = Console.ForegroundColor;
var backgroundColor = Console.BackgroundColor;

var currentBackground=Console.BackgroundColor;
var currentForeground=Console.ForegroundColor;
var promptString:String="";
var outFile="";

function printHelp( ) {
   print( arguments[0] + " [-f foreground] [-b background] [-p prompt string] [-o out file]" );

   print( "   foreground       Foreground color - a " );
   print( "               number between 0 and 15." );
   print( "   background       Background color - a " );
   print( "               number between 0 and 15." );
   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);
   }
}


function parseArgs(){
   if ( arguments.length == 1 || arguments[1].toLowerCase() == "-help" || arguments[1].toLowerCase() == "-help"   ) {
      printHelp();
      Environment.Exit(0);
   }
   
   for (var i=1;i<arguments.length-1;i=i+2){
      try{
         switch(arguments[i].toLowerCase()){
            case '-f':
               foregroundColor=Int32.Parse(arguments[i+1]);
               break;
            case '-b':
               backgroundColor=Int32.Parse(arguments[i+1]);
               break;
            case '-p':
               promptString=arguments[i+1];
               break;
            case '-o':
               outFile=arguments[i+1];
               break;
            default:
               Console.WriteLine("Invalid Argument "+arguments[i]);
               break;
      }
      }catch(e){
         errorChecker(e);
      }
   }
}

parseArgs();
numberChecker(backgroundColor);
numberChecker(foregroundColor);

Console.BackgroundColor = backgroundColor ;
Console.ForegroundColor = foregroundColor ;

Console.Write(promptString);

var key;
var input="";
do {
    key = Console.ReadKey(true);
    if ( (key.KeyChar.ToString().charCodeAt(0)) >= 20 && (key.KeyChar.ToString().charCodeAt(0) <= 126) ) {
      input=input+(key.KeyChar.ToString());
        Console.Error.Write(key.KeyChar.ToString());
    }   
} while (key.Key != ConsoleKey.Enter);
Console.Error.WriteLine();
Console.BackgroundColor = currentBackground;
Console.ForegroundColor = currentForeground;

try {
   if (outFile != ""){
      File.WriteAllText(outFile , input);
   }
}catch(e){
   errorChecker(e);
}

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Batch change color of user input

#6 Post by batchcc » 17 Aug 2015 14:24

Dear foxdrive than you this was just what I was looking for but is there a way to change the colors used in the code? Thanks

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch change color of user input

#7 Post by ShadowThief » 17 Aug 2015 16:08

Meerkat wrote:According to the page...
because right now the answer is simply "yes."

Sorry, but I do not understand that part. :?

The way it was written, the question was effectively, "can ___ be done in batch?"

The answer is yes, it can be done in batch.

Post Reply