Color Tool [expanded]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Color Tool [expanded]

#1 Post by Boombox » 29 Nov 2012 08:02

.
I'd like to colour specific words and variables in the CMD window.

Maybe even use FINDSTR to search a file, highlighting the string as found.

Is there a way to do this?

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Color Tool [expanded]

#2 Post by foxidrive » 29 Nov 2012 08:16

MF (Maxfind) can find keywords in a text file and display them coloured.

This recent post on this topic in another thread showed syntax highlighting in a bat file, which you will get in various programmers editors like Notepad ++

Boombox wrote:.
Regardless of the input file size, computational power...

This,

@echo off
echo %var%>test.txt
copy test.txt d:\test.txt
::This is green!
pause
exit

should look like this...

@echo off
echo %var%>test.txt
copy test.txt d:\test.txt
::This is green!
pause
exit

Thanks.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Color Tool [expanded]

#3 Post by Squashman » 29 Nov 2012 08:29

I believe Swiss File Knife can do colored text based on a filter or hit option.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color Tool [expanded]

#4 Post by abc0502 » 29 Nov 2012 08:30

I don't know if i understood you correctly but this all what i was able to come with:

Code: Select all

@Echo OFF

setlocal enabledelayedexpansion
set C=0
For /F "tokens=1* delims= " %%A in ('Type "text.txt"') Do (
   set /a C+=1
   SET "A=%%A"
   SET "B=%%B"
   
   :: @Echo
   IF /I "!A:~0!" == "@Echo" (
      Call :colorPrint 0D "@"
      Call :colorPrintVar 03 "A:~1"
      Call :colorPrint 08 " "
      IF /I "!B!" EQU "off" Call :colorPrint 07 "%%B"
      IF /I "!B!" EQU "on" Call :colorPrint 07 "%%B"
      Echo.)
      
   :: Comment
   IF /I "!A:~0,2!" EQU "::" (
      Call :colorPrintVar 02 "A"
      Call :colorPrint 08 " "
      Call :colorPrintVar 02 "B"
      Echo.)
   :: CLS
      IF /I "!A!" EQU "cls" (
      Call :colorPrintVar 02 "A"
      Call :colorPrint 08 " "
      IF Not "!B!" EQU "" Call :colorPrintVar 0F "B"
      Echo.)
   :: Labels
   IF /I "!A:~0,1!" EQU ":" (
      IF /I not "!A:~1,2!" EQU ":" (
         Call :colorPrintVar 60 "A"
         Echo.))
   :: Echo
   IF /I "!A!" EQU "echo" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      Call :colorPrintVar 0F "B"
      Echo.)
      
   :: Goto
   IF /I "!A!" EQU "goto" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      CAll :colorPrintVar 06 "B"
      Echo.)
   :: Pause / Pause >nul
   IF /I "!A!" EQU "pause" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      CAll :colorPrintVar 04 "B"
      Echo.)
   :: Pause>nul
   IF /I "!A!" EQU "pause>nul" (
      Call :colorPrintVar 03 "A:~0,5"
      CAll :colorPrintVar 04 "A:~-4"
      Echo.)
   :: Call
   IF /I "!A!" EQU "call" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      Call :colorPrintVar 06 "B"
      Echo.)
   :: Set
   IF /I "!A!" EQU "set" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      Call :colorPrintVar 0E "B"
      Echo.)
   :: IF
   IF /I "!A!" EQU "if" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      Call :colorPrintVar 0F "B"
      Echo.)
   
   :: For
   IF /I "!A!" EQU "for" (
      Call :colorPrintVar 03 "A"
      Call :colorPrint 08 " "
      Call :colorPrintVar 0F "B"
      Echo.)

      
)
pause>nul
exit

set "text=Dave's version works in Win XP, 7 & 8 "Hurray!""
call :colorPrintVar 9b text /n
call :colorPrint 3F " /:\ Part 1 "
call :colorPrint 4F " Part 2 /:\" /n
echo Success!
exit /b


:colorPrint  Color  Str  [/n]
if not defined DEL call :initColorPrint
setlocal disableDelayedExpansion
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b


:colorPrintVar  Color  StrVar  [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE
for %%n in (^"^

^") do (
  set "s=!s:\=%%~n\%%~n!"
  set "s=!s:/=%%~n/%%~n!"
  set "s=!s::=%%~n:%%~n!"
)
set "s=!s:"=\"!"
for /f delims^=^ eol^= %%s in ("!s!") do (
  if "!" equ "" setlocal disableDelayedExpansion
  if %%s==\ (
    findstr /a:%~1 "." "\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%"
  ) else if %%s==/ (
    findstr /a:%~1 "." "/.\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
  ) else (
    findstr /a:%~1 "." "%%s\..\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
  )
)
if /i "%~3"=="/n" echo(
popd
exit /b


:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%temp%\'" set /p "=."
subst ': "%temp%" >nul
exit /b


:cleanupColorPrint
2>nul del "%temp%\'"
>nul subst ': /d
exit /b


And This is the TXT file:
@Echo Off
@Echo on
CLS
:Test_Label
:: This is a comment
echo This is a normal text
pause>nul
pause >nul
Call :test_function "input"
goto :eof
set "test=sdasdasd"
if %test%==esasdasd
For %%a in (test) do echo %%a

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Color Tool [expanded]

#5 Post by Boombox » 29 Nov 2012 08:57

.
Well done ABC! and thank you very much. That's exactly what I wanted.

It has a few problems when I enter garbage into the text file.

But this will give me everything I need to work with, thanks again.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color Tool [expanded]

#6 Post by abc0502 » 29 Nov 2012 09:23

I tried to make it display the empty lines as well but no luck also didn't test on the special chars or if the command isn't at the start.
but if you want to do such thing it will be a lot easier if you try by another programming language

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Color Tool [expanded]

#7 Post by Aacini » 29 Nov 2012 19:18

Would you accept a solution that use an .exe file other than FINDSTR.EXE? Previous solution use FINDSTR command that is an .exe file about 27 KB size. There is another solution based on a different .exe file (ColorShow.exe of just 3.5 KB size) that run much faster than FINDSTR and is easier to use. The only detail is that FINDSTR.exe file is already installed in your computer, but you must copy ColorShow.exe file from this site; look for: 12- ColorShow.exe.hex.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "listedCommands= echo cls goto pause call exit set if for "
set "compoundCommands= if for "
set compoundCommandsWords=lss leq equ neq geq gtr  in do


for /F "tokens=1*" %%a in (text.txt) do (
   set "command=%%a"

   rem If line begins with colon: is special case
   if "!command:~0,1!" equ ":" (
      if "!command:~1,1!" equ ":" (
         rem Double colon at beginning: whole line in green
         set colorShow=/02 "%%a " "%%b"
      ) else (
         rem Label: in light green
         set colorShow=/0A "%%a"
      )
   ) else (

      rem Normal commands: command params
      set "params=%%b"
      set colorShow=

      rem Arroba in first position: in magenta
      if "!command:~0,1!" equ "@" (
         set colorShow=/0D "@"
         set "command=!command:~1!"
      )

      rem Identify the type of command
      for %%a in (!command!) do if "!listedCommands: %%a =!" neq "%listedCommands%" (
         rem Listed commands: in blue
         set colorShow=!colorShow! /09 "%%a "
      ) else (
         rem Unlisted commands: in original color
         set colorShow=!colorShow! / "%%a "
      )

      rem Additional processing of FOR and IF commands
      for %%a in (!command!) do if "!compoundCommands: %%a =!" neq "%compoundCommands%" (
         rem Special words in FOR and IF: in pink
         for %%c in (%compoundCommandsWords%) do set params=!params:%%c=" /0C "%%c" / "!
         rem Nested commands in FOR and IF: in blue
         for %%c in (%listedCommands%) do set params=!params:%%c=" /09 "%%c" / "!
      )

      rem Unprocessed parameters: in original color
      set colorShow=!colorShow! / "!params!"

   )
   colorShow !colorShow! 13 10
)


Antonio

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Color Tool [expanded]

#8 Post by Boombox » 30 Nov 2012 05:01

.
Thanks all. But I'm still struggling.

I have a text file containing colour codes.

a=44
b=17
c=29
d=4f

etc.

And a text file containing simple ascii art.

How can I compare the two files and print the ascii art to the command window with the colours I specify?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Color Tool [expanded]

#9 Post by Aacini » 30 Nov 2012 08:27

Boombox wrote:How can I compare the two files and print the ascii art to the command window with the colours I specify?

If you mean How to print each letter in the ascii art file with the colour code (for the same letter) given in first file?, then this is the solution (using colorShow command, I am pretty sure that doing the same task with FINDSTR will be a nightmare!)

Code: Select all

@echo off
setlocal EnableDelayedExpansion
rem Load color codes from first file
for /F "delims=" %%a in (colourCodes.txt) do (
   set %%a
)
rem Print the Ascii art file
set line=
call :PrintAsciiArt < asciiArt.txt
goto :EOF

:PrintAsciiArt
   rem Read each line in the file (an empty line stop the process!)
   set /P line=
   if not defined line exit /B

   rem Separate each letter in the line and assemble colorShow parameters
   set colorShow=
   :nextLetter
      set letter=%line:~0,1%
      rem For each Letter, insert: /attribute of the Letter, and the "Letter"
      set colorShow=%colorShow% /!%letter%! "%letter%"
      rem Pass to next letter
      set line=%line:~1%
   if defined line goto nextLetter

   rem Print the resulting colorShow line
   colorShow %colorShow% 13 10

   rem And pass to next line
goto PrintAsciiArt
Please, test this program and report the result. I would like to see a picture of the output! :D

Antonio

PS - Note that you can NOT specify different colors for lowcase and upcase letters; they both will have the same (last defined) color.

PPS - Could you post a color codes and ascii art files, please?

Post Reply