Display matches in color

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Display matches in color

#1 Post by miskox » 11 Nov 2015 15:32

Hi all!

Let's have a file with some 'graphic' (ascii art to be exact):

Code: Select all

                       _____
         5     6      /ELEV/    1
     ________________/    /_______
    /               /    /       /
  4/  1st floor    /    /       /
  /_______________/____/   ____/
      3               /   /  2
                   56/___/


Desired output (number 6 in this example should be in color - it does not display correctly (at least for me)):

(only 6 should be matched, 56 should not be matched). It is possible to have a nonnumeric character before/after but if numeric character is before/after this should not be considered a correct match (regex should be used?).

Code: Select all

                       _____
         5     [color=#FF0000]6[/color]      /ELEV/    1
     ________________/    /_______
    /               /    /       /
  4/  1st floor    /    /       /
  /_______________/____/   ____/
      3               /   /  2
                   56/___/


Things I need (should work on XP):
- I supply a number (or string) that should be searched for and this number (string) should be displayed in color. In the example number 6. This number/string can be more than 1 character in size.

One of the ways:
- read each line from the 'graphic' file
- if it does not contain a 'match' -> display it
- if it contains a match then:
- find the offset of the character
- use Carlos' COLOR function (viewtopic.php?p=25305#p25305) to display it
- continue with the rest of the lines

Is it possible to display flashing characters? (on ZX Spectrum: FLASH 1) If so this might be even better.

Here is version 1:

Code: Select all

@echo off
rem create graphic file
>graphic.txt (echo                       _____)
>>graphic.txt (echo         5     6      /ELEV/    1)
>>graphic.txt (echo     ________________/    /_______)
>>graphic.txt (echo    /               /    /       /)
>>graphic.txt (echo  4/  1st floor    /    /       /)
>>graphic.txt (echo  /_______________/____/   ____/)
>>graphic.txt (echo      3               /   /  2)
>>graphic.txt (echo                   56/___/)

set string_to_search=6

for /f "tokens=1 delims=" %%f in (graphic.txt) do call :dosomework "%%f"

goto :EOF

:dosomework
set par1=%~1

set /a fillen=-1
set /a cnt=0
:goback
call set current_character=%%par1:~%cnt%,1%%
if "%current_character%"=="" set fillen=%cnt%&goto :work1
REM echo CURR_CHR=_%current_character%_

REM Here should make a test for a character before a match to not be a number
if "%current_character%"=="%string_to_search%" goto :work1

set /a cnt+=1
goto :goback

:work1
set /a cnt1=cnt - 0
REM 1 below assumes one character width - needs correction to support string of
REM different lengths.
set /a cnt2=cnt + 1
if "%fillen%"=="%cnt%" echo(%par1%&goto :EOF
call set  left_part=%%par1:~0,%cnt1%%%
call set right_part=%%par1:~%cnt2%%%

call :color 07 "%left_part%" 0E "%string_to_search%" 07 "%right_part%"
echo.

goto :EOF

:Color
:: v23c Carlos
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
For %%_ In ("&" "|" ">" "<"
) Do Set "Text=!Text:%%~_=^%%~_!"
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color


Output:

Image

This can't be done in just a few commands (FINDSTR /A), right?

Thanks.
Saso

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

Re: Display matches in color

#2 Post by Aacini » 12 Nov 2015 00:48

May I suggest you an entirely different approach? Use an .hta file to show the output:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem create graphic file
(
echo                       _____
echo         5     6      /ELEV/    1
echo     ________________/    /_______
echo    /               /    /       /
echo  4/  1st floor    /    /       /
echo  /_______________/____/   ____/
echo      3               /   /  2
echo                   56/___/
) > graphic.txt

set string_to_search=6

(
set /P "=<style type='text/css'>body {color:white; background:black; font-family:"Lucida Console"; font-size:150%%;}</style> <pre>" < NUL
echo/
for /F "delims=" %%a in (graphic.txt) do (
   set "line=%%a"
   echo(!line:%string_to_search%=^<font color="Yellow"^>%string_to_search%^</font^>!
)
echo ^</pre^>
) > graphic.hta
mshta "%cd%\graphic.hta"

Antonio

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Display matches in color

#3 Post by miskox » 12 Nov 2015 09:43

I really like yuor solution - short, fast but... mshta does not return to .cmd - it just stays frozen. Any ideas?

Also one thing I must solve: example shows two matches: '6' which is correct and part of the number '56' which is not correct. If a character before or after is numeric then this should not be matched. I think this might be solvable by regex but I really have no ideas how to use them. I tried with no luck.

String that can be searched for looks like:

- number only (from 1 to 4 digits)
- number/number (for example: 9/3). If I search for 0/3 this should be matched only with 0/3 and not 10/3.

Thanks.
Saso

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

Re: Display matches in color

#4 Post by Squashman » 12 Nov 2015 09:58

miskox wrote:I really like yuor solution - short, fast but... mshta does not return to .cmd - it just stays frozen. Any ideas?

Code: Select all

start "" mshta "%cd%\graphic.hta"

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

Re: Display matches in color

#5 Post by Aacini » 12 Nov 2015 15:00

miskox wrote:I really like yuor solution - short, fast but... mshta does not return to .cmd - it just stays frozen. Any ideas?

Hummm... The mshta window is not frozen, it is just another active window that have the focus! As soon as you close it via the red cross, the control returns to .cmd; you may grasp this point in an easier way if you replace the MSHTA command by NOTEPAD one: the Notepad window will not return to .cmd until you close it! The solution suggested by Squashman (START command) does not solve this point, it just makes .cmd to continue running in parallel with mshta, but the HTA window will still keep the focus (unless you click on the .cmd one).

The HTA window looks "frozen" because it have not any control programmed. Do you want that it closes when any key is pressed? It is very easy to do that (and add a "Press any key when ready" message at bottom).

miskox wrote:Also one thing I must solve: example shows two matches: '6' which is correct and part of the number '56' which is not correct. If a character before or after is numeric then this should not be matched. I think this might be solvable by regex but I really have no ideas how to use them. I tried with no luck.

Well, the regex may be used to find the proper line the have the searched string, but this point does not solve the case when both "6" and "56" strings (for example) appears in the same line. The new code below use the proper regex:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem create graphic file
(
echo                       _____
echo         5     6      /ELEV/    1
echo     ________________/    /_______
echo    /               /    /       /
echo  4/  1st floor    /    /       /
echo  /_______________/____/   ____/
echo      3               /   /  2
echo                   56/___/
) > graphic.txt

set "string_to_search=6"

(for /F "delims=:" %%a in ('findstr /N "[^0-9]%string_to_search%[^0-9]" graphic.txt') do echo %%a) > matchedLines.txt

< matchedLines.txt (
   echo ^<style type="text/css"^>body {color:white; background:black; font-family:"Lucida Console"; font-size:150%%;}^</style^> ^<pre^>
   set "lineNum=0"
   set /P "matchedLine="
   for /F "delims=" %%a in (graphic.txt) do (
      set "line=%%a"
      set /A lineNum+=1
      if !lineNum! equ !matchedLine! (
         set "line=!line:%string_to_search%=<font color="Yellow">%string_to_search%</font>!"
         set /P "matchedLine="
      )
      echo !line!
   )
   echo ^</pre^>
) > graphic.hta
mshta "%cd%\graphic.hta"

Antonio

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Display matches in color

#6 Post by miskox » 13 Nov 2015 04:20

Thank you both. I am getting close.

I will incorporate everything into my programs and do some more tests.

Thanks for the moment.

Saso

Post Reply