You are not logged in.
Pages: 1
This is a function for print text in color using native findstr.exe. Handle fine all characters, and works in Windows XP, 7 y 8.
Note: new version 20 here http://www.dostips.com/forum/viewtopic.php?f=3&t=4453
Last edited by carlos (04 Apr 2013 04:11)
Offline
I improved more the code. Maybe this is my final version.
Offline
Improved again. Removed unnecessary labels and gotos. Now in the version 11 the text is printed more speedy.
Last edited by carlos (27 Nov 2012 20:41)
Offline
I've tried this but only prints a bluish "// Version 11 is more speedy !" ...I still don't get the idea.
Offline
This is the idea. Print colored text using only the native findstr.exe (not external utilities), and with the compatibility of accept characters like /\:!" all, even ascii as ascii 1 that print a face.
Offline
ahaaam... /a switch... :-)
Offline
That's great in fact .You can use the same font and background ..Is there a way that this can be used for user input?
Last edited by npocmaka (27 Nov 2012 20:54)
Offline
Sorry, it only print text in color. It not change the color attribute for the coming prints.
If you need I can add the function for change the color to my bg external utility (if you want use a external utility).
Offline
I updated the code for last. Now, in version 12 if you put only the code under the label :Color in a batch file, you can run it even with extensions disabled.
Last edited by carlos (28 Nov 2012 00:19)
Offline
Updated again. In version 13 was fixed a compatibility problem with windows XP, now all is ok.
Offline
I posted the version 14.1 with fixes. I change the way of usage of the function, now is necessary pass a name of variable with the text.
Last edited by carlos (28 Nov 2012 05:23)
Offline
Improved and refined the code. Now, the version 17 have a code very faster and tiny).
Offline
@ECHO OFF
set "sp= "
for %%O in (
"00 00 00 ee ee ee ee 00 00 00 "
"00 00 ee ee ee ee ee ee 00 00 "
"00 ee 00 ee ee ee ee 00 ee 00 "
"ee ee 00 ee ee ee ee 00 ee ee "
"ee ee ee ee ee ee ee ee ee ee "
"ee 00 ee ee ee ee ee ee 00 ee "
"ee ee 00 ee ee ee ee 00 ee ee "
"00 ee ee 00 ee ee 00 ee ee 00 "
"00 00 ee ee 00 00 ee ee 00 00 "
"00 00 00 ee ee ee ee 00 00 00 "
"00 00 00 00 ee ee 00 00 00 00 "
) do (
call :color 00 sp \n
for %%I in (%%~O) do (
call :color %%I sp
)
)
call :color 00 sp \n
pause >nul
exit /b
:Color
:: v17. Arguments: hexColor variableName [\n]
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &Pushd . &`: &If Not Exist `.bat (
Set /P "=."<Nul >` &For /F "delims=;" %%# in (
'"Prompt;$H;&For %%_ in (1) Do Rem"') Do Echo(Set "b=%%#">`.bat
For %%# in ("(Set n=^" "" ")") Do Echo(%%~#>>`.bat)
Call ` &Set "c=%~1" &Set "t=!%~2!"
If Defined t For %%# in ("!n!") Do For %%_ in (\ / :
) Do Set "t=!t:%%_=%%~#%%_%%~#!"
For /F usebackq^ delims^=^ eol^= %%_ in ('!t:"=\"!') Do (
SetLocal DisableDelayedExpansion
For /F delims^=^ eol^= %%# in ("%%~_") Do If \==%%# (
Findstr /A:%c% "." "\`" Nul &Set /P "=%b%%b%%b%"<Nul
) Else If /==%%# (Findstr /A:%c% "." "/.\`" Nul
Set /P "=%b%%b%%b%%b%%b%"<Nul
) Else (Findstr /A:%c% "." "%%#\..\`" Nul
Set /P "=%b%%b%%b%%b%%b%%b%%b%"<Nul)
EndLocal)
If /I "\n"=="%~3" (Echo()
Popd &Goto :Eof
btw. seems does not work good with extended ascii characters.Probably because of the findstr.
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 77 77 77 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 66 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 66 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 99 99 00 00 00 00 77 77 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 99 99 00 00 00 00 66 66 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 99 99 00 00 00 66 66 00 00 00 99 99 99 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 99 99 00 00 66 66 00 00 99 99 99 99 66 66 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 99 99 99 99 99 99 99 99 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 99 99 99 99 99 99 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 aa 00 00 00 00 "
"00 00 00 00 00 00 00 99 99 99 99 99 00 00 00 00 00 00 00 00 00 00 aa aa 00 00 00 00 00 aa aa 00 00 "
"00 00 00 00 00 00 00 00 99 99 99 00 00 00 00 00 00 00 00 00 00 aa aa aa 00 aa 00 00 aa aa aa 00 00 "
"00 00 00 00 00 00 00 00 99 99 99 00 00 00 00 00 00 00 00 00 00 00 44 00 00 aa aa 00 00 44 00 00 00 "
"00 00 00 00 00 00 00 00 99 99 99 00 00 00 00 00 00 00 00 00 00 00 00 44 00 44 00 00 00 44 00 00 00 "
"00 00 00 00 00 00 00 00 cc cc cc 00 00 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00 44 44 00 00 00 "
"00 00 00 00 00 00 00 00 33 33 33 00 00 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00 44 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00 44 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 00 44 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 44 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 33 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 00 00 00 00 00 "
"00 00 00 00 00 00 00 dd dd 00 dd dd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
Last edited by npocmaka (14 Dec 2012 00:24)
Offline
npocmaka, I don't know what happens with the extended ascii characters in version 17. Anyways I posted the version 18 that is more speedy. I will try to fix the problem with extended ascii characters in a future version.
Last edited by carlos (14 Dec 2012 02:16)
Offline
I've solved the extended ascii issue. The problem is FINDSTR transforms some extended ascii into other characters when they appear on the command line. I new about this problem with regards to search strings, and previously documented the problem on my StackOverflow Q&A: What are the undocumented features and limitations of the Windows FINDSTR command?. Simon also posted the info on the SS64 FINDSTR help page after I invited him to do so.
But the fact that file names are also corrupted is new to me. I now realize that the problem occurs with any string that FINDSTR parses from the command line. I've updated my SO post.
The solution for printing extended ASCII in color is to write the name of the file to a temp file and use the /F:FILE option. Double quotes no longer need to be escaped when using the /F:FILE option.
Printing in color with native batch has evolved considerably over time. The earliest posts I'm aware of are on SO at how to have multiple colors in a batch file? jeb's answer is the earliest I've seen. You can see a progression in capability in three answers to that question. Recently Carlos made advances, and posted here and on DosTips: Color function vNN. I contributed to the DosTips thread with my own optimizations of Carlos's ideas. I've used my version of Carlos's code - I find it easier to read and a bit faster. (At least it was faster than v17. I'm not sure about v18).
I've updated the code to support all extended ASCII characters. It now supports all single byte characters except 0x00 (nul), 0x0A (linefeed), and 0x0D (carriage return). If run on XP then most control characters are printed as dots - there is no way to solve that problem - it is an inherent feature of FINDSTR on XP.
Unfortunately, adding support for XP, and now support for extended characters, has slowed the routine down from what it used to be. It is a trade off - faster with more limitations, or slower with fewer limitations.
My code has a couple of entry points to allow printing of both string literals and string variables. It also has routines to explicitly initialize and cleanup. The initialization runs automatically if needed.
Just for fun, I then grabbed some color ASCII art from joan stark's ASCII Art Gallery and adapted it for use with ColorPrint. I added a :c entry point just for shorthand, and to handle an issue with quote literals.
@echo off
setlocal disableDelayedExpansion
::
:: The following image was adapted from joan stark's ASCII Art Gallery
:: http://www.geocities.com/SoHo/7373/indexjava.htm
::
set q=^"
echo(
echo(
call :c 0E " , .-;" /n
call :c 0E " , |\ / / __," /n
call :c 0E " |\ '.`-.| |.'.-'" /n
call :c 0E " \`'-: `; : /" /n
call :c 0E " `-._'. \'|" /n
call :c 0E " ,_.-=` ` ` ~,_" /n
call :c 0E " '--,. "&call :c 0c ".-. "&call :c 0E ",=!q!." /n
call :c 0E " / "&call :c 0c "{ "&call :c 0A "* "&call :c 0c ")"&call :c 0E "`"&call :c 06 ";-."&call :c 0E "}" /n
call :c 0E " | "&call :c 0c "'-' "&call :c 06 "/__ |" /n
call :c 0E " / "&call :c 06 "\_,\|" /n
call :c 0E " | (" /n
call :c 0E " "&call :c 0c "__ "&call :c 0E "/ ' \" /n
call :c 02 " /\_ "&call :c 0c "/,'`"&call :c 0E "| ' "&call :c 0c ".-~!q!~~-." /n
call :c 02 " |`.\_ "&call :c 0c "| "&call :c 0E "/ ' , "&call :c 0c "/ \" /n
call :c 02 " _/ `, \"&call :c 0c "| "&call :c 0E "; , . "&call :c 0c "| , ' . |" /n
call :c 02 " \ `, "&call :c 0c "| "&call :c 0E "| , , "&call :c 0c "| : ; : |" /n
call :c 02 " _\ `, "&call :c 0c "\ "&call :c 0E "|. , "&call :c 0c "| | | | |" /n
call :c 02 " \` `. "&call :c 0c "\ "&call :c 0E "| ' "&call :c 0A "|"&call :c 0c "\_|-'|_,'\|" /n
call :c 02 " _\ `, "&call :c 0A "`"&call :c 0E "\ ' . ' "&call :c 0A "| | | | | "&call :c 02 "__" /n
call :c 02 " \ `, "&call :c 0E "| , ' "&call :c 0A "|_/'-|_\_/ "&call :c 02 "__ ,-;` /" /n
call :c 02 " \ `, "&call :c 0E "\ . , ' .| | | | | "&call :c 02 "_/' ` _=`|" /n
call :c 02 " `\ `, "&call :c 0E "\ , | | | | |"&call :c 02 "_/' .=!q! /" /n
call :c 02 " \` `, "&call :c 0E "`\ \/|,| ;"&call :c 02 "/' .=!q! |" /n
call :c 02 " \ `, "&call :c 0E "`\' , | ; "&call :c 02 "/' =!q! _/" /n
call :c 02 " `\ `, "&call :c 05 ".-!q!!q!-. "&call :c 0E "': "&call :c 02 "/' =!q! /" /n
call :c 02 " jgs _`\ ;"&call :c 05 "_{ ' ; "&call :c 02 "/' =!q! /" /n
call :c 02 " _\`-/__"&call :c 05 ".~ `."&call :c 07 "8"&call :c 05 ".'.!q!`~-. "&call :c 02 "=!q! _,/" /n
call :c 02 " __\ "&call :c 05 "{ '-."&call :c 07 "|"&call :c 05 ".'.--~'`}"&call :c 02 " _/" /n
call :c 02 " \ .=!q!` "&call :c 05 "}.-~!q!'"&call :c 0D "u"&call :c 05 "'-. '-..' "&call :c 02 "__/" /n
call :c 02 " _/ .!q! "&call :c 05 "{ -'.~('-._,.'"&call :c 02 "\_,/" /n
call :c 02 " / .!q! _/'"&call :c 05 "`--; ; `. ;" /n
call :c 02 " .=!q! _/' "&call :c 05 "`-..__,-'" /n
call :c 02 " __/'" /n
echo(
exit /b
:c
setlocal enableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:colorPrint Color Str [/n]
setlocal
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!"
)
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 (
>colorPrint.txt (echo %%s\..\')
findstr /a:%~1 /f:colorPrint.txt "."
<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%\'"
2>nul del "%temp%\colorPrint.txt"
>nul subst ': /d
exit /b
Dave Benham
Last edited by dbenham (14 Dec 2012 07:25)
Offline
Dave, thanks for the help. I have some commentaries.
The develop of the solution of print in color using only batch is more old, almost since 2007:
https://groups.google.com/forum/?hl=en&fromgroups=#!topic/alt.msdos.batch.nt/-Bms1FgtXeM
Here are all my versions from this year 2012:
https://groups.google.com/forum/?hl=es&fromgroups=#!topic/alt.msdos.batch.nt/9eHbeTwBKYM
I implement your solution using the /F option, but, now I test all before post, and under XP, extended ascii characters are printed as dots.
Also, I read your text of the conversions of extended asci characters by findstr in stackoverflow, but it depends of the codepage that you use, is not universal.
Last edited by carlos (14 Dec 2012 20:48)
Offline
Updated to version 19 that have support for extended ascii characters, please see remarks in code.
Offline
Thanks Carlos. I like having access to the history of the ideas.
Not all extended ASCII chars display as dots on XP. Only the characters that necessitated the use of the /F option.
So the /F option does not help XP, but it does enable display of all extended ASCII on Vista and later versions.
I've updated my SO post with clarified rules. I also restored the language disclaimer concerning which extended ASCII characters are transformed.
Also, I read your text of the conversions of extended asci characters by findstr in stackoverflow, but it depends of the codepage that you use, is not universal.
When you say it depends on the codepage, are you referring to how the characters appear on the screen? or which characters are transformed?
Dave Benham
Offline
I developed a method to show large Ascii-Art files in color, like this one:
You may read full details at this post.
Antonio
Offline
Pages: 1