Color output - Where is the <CR><LF>?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Color output - Where is the <CR><LF>?

#1 Post by dbenham » 29 Oct 2011 21:38

jeb posted a really cool method for printing in color to the screen 8)

Here is his code

Code: Select all

@echo off
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 :eof

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

I understand everything about this EXCEPT - Why doesnt the FINDSTR output a new line :?:

This seems an interesting alternative to SET /P for printing text without a new line.

Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Color output - Where is the <CR><LF>?

#2 Post by Ed Dyreen » 29 Oct 2011 22:18

'
:D :D :D

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Color output - Where is the <CR><LF>?

#3 Post by dbenham » 29 Oct 2011 22:52

OK, I found the answer to my own question here: findstr problem

I'm used to seeing a newline after each line found by FINDSTR, and I assumed it was FINDSTR that was generating the newline. But the newline actually comes from the matching line from the searched file. So if the matching line does not have a newline, then none is output :!: Of course this can only happen on the last line of the file. And the file used for jeb's color output only has one line with no newline.

Note: jeb's explanation is slightly off in the link I posted. The temporary DEL variable actually contains <BS><space><BS> instead of just <BS>. The extra <space><BS> is actually needed to make sure that the final : on the line gets over-written.

Dave Benham

FluiT
Posts: 1
Joined: 16 Jul 2012 10:53

Re: Color output - Where is the <CR><LF>?

#4 Post by FluiT » 16 Jul 2012 11:00

Hello :)

When using

call :ColorText 0a "blue" (or any other color)

the colon (:) at the end of the colored string seems to be unavoidable (this in some cases might be a drawback)... Does anybody know a way to suppress that colon?

Thanks :mrgreen:

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

Re: Color output - Where is the <CR><LF>?

#5 Post by abc0502 » 16 Jul 2012 12:49

I Use this Code for Printing in colors:

Code: Select all

:Color
set /p ".=." > "%~2" <nul
findstr /v /a:%1 /R "^$" "%~2" nul 2>nul
set /p ".=" <nul
if "%3" == "end" set /p ".= " <nul
del "%~2" >nul 2>nul
exit /b

It has it's limitation but it does the job :)
Here is The Original batch I found this Code in:

Code: Select all

@echo off
:: Original author: Unknown
:: Editor: GrellesLicht28
REM To write a text before colored text, use a SET/P-command before the CALL-command.
REM To write a text between two colored texts, use a SET/P-command between two CALL-commands.
REM To write a text after all colored texts, use an ECHO-command. If you do not want to write anything after it, use "ECHO."
REM The last colored text should have "end" as third parameter.
title Colour Text
echo These are the colored texts:
echo.
call :ColorText 0a "GREEN"
set /p ".= " <nul
call :ColorText 0c "RED"
set /p ".= " <nul
call :ColorText 09 "BLUE"
set /p ".= " <nul
call :ColorText 0d "PINK"
set /p ".= " <nul
call :ColorText 0e "YELLOW"
set /p ".= " <nul
call :ColorText 08 "GRAY" end
echo.
echo.


set /p ".=Text before " <nul
call :ColorText 0a "GREEN"
set /p ".= and between green and " <nul
call :ColorText 0c "RED" end
echo. and after red.
echo.
echo.
pause
exit

:: Keep this label exactly as it is and do not change anything here!
:ColorText [%1 = Color] [%2 = Text]
set /p ".=." > "%~2" <nul
findstr /v /a:%1 /R "^$" "%~2" nul 2>nul
set /p ".=" <nul
if "%3" == "end" set /p ".=  " <nul
del "%~2" >nul 2>nul
exit /b

Any Question Ask here.
Last edited by abc0502 on 16 Jul 2012 13:09, edited 1 time in total.

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

Re: Color output - Where is the <CR><LF>?

#6 Post by abc0502 » 16 Jul 2012 13:00

One more thing Instead of writing like this:

Code: Select all

call :ColorText 0a "GREEN"
set /p ".= " <nul
call :ColorText 0c "RED"
set /p ".= " <nul

You can use the "&" to make all this text in one line like this
call :ColorText 0a "GREEN" &set /p ".= " <nul &call :ColorText 0c "RED" end&echo.

so you don't confuse yourself when writing, after all this words will be in one line

And if this line is finished u write the word "end" to tell this function that this is the end of the line.
And the command "echo." to make any other commands or words that will be witten after this line be in a new line

e4nd
Posts: 12
Joined: 17 Mar 2012 02:28

Re: Color output - Where is the <CR><LF>?

#7 Post by e4nd » 17 Jul 2012 01:36

Code: Select all

@echo off & setlocal enabledelayedexpansion
echo.
Call :Color 0B "D"
Call :Color 0c "o"
Call :Color 0d "s"
Call :Color 0e "T"
Call :Color 2b "i"
Call :Color 2c "p"
Call :Color 2d "s"
Call :Color 0B " D"
Call :Color 0c "o"
Call :Color 0d "s"
Call :Color 0e "T"
Call :Color 2b "i"
Call :Color 2c "p"
Call :Color 2d "s"
echo.
echo.
Call :Color 0C "bla " & Call :Color 0D " bla" & Call :Color 0e " bla"
echo.
echo.
for %%! in (0b-D
         0c-o
         0d-s
         0e-t
         2b-i
         2c-p
         2d-s) do (
         echo %%! > $$
for /f "tokens=1,2 delims=-" %%a in ($$) do (
      Call :Color %%a "%%b"
      del $$
      )
   )
pause>nul
exit

:Color [%1 = Color] [%2 = Text]
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set /p "= " > "%~2" <nul
findstr /v /a:%~1 "^$" "%~2" nul
set /p "=" <nul
<nul set /p ".=%DEL%%DEL%"
del "%~2" >nul 2>nul
goto :EOF

Post Reply