Strange behaviour with ansi sequence

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Strange behaviour with ansi sequence

#1 Post by J'aitreschaud » 12 Nov 2018 23:05

Bonjour everyone. I recently found out bout ansi escape sequences and I found this strange behaviour.
I have this code:

Code: Select all

@echo off
for /f %%A in ('echo prompt $E^| cmd') do set "ESC=%%A"
echo %ESC%[100m
pause
It should make the background grey. However, it seems like only the text's background is grey.
ansi.png
ansi.png (4.68 KiB) Viewed 217 times
What's more strange is that after inserting a CLS after the sequence, the whole background turns grey (expected behaviour)
ansi.png
ansi.png (4.62 KiB) Viewed 217 times
Why is that? Another strange thing is that I can't seem to replicate the behavior after opening the file; I can't revert it back to black with "only grey background only letters" after I clear the screen. Is there an explanation on why this happens?

jeb
Expert
Posts: 884
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Strange behaviour with ansi sequence

#2 Post by jeb » 13 Nov 2018 00:36

Hi J'aitreschaud,

ESC[ <n> m is the Select Graphic Rendition sequence, it sets the display attributes, they affect each NEW character that will be written, that's all.
In your case, it changes the complete background after the cls, as the complete screen will be filled with spaces.
To reset that setting, you can use

Code: Select all

REM Reset display attributes
echo %ESC%[0m

REM Clear screen, move cursor to home position
echo %ESC%[2J
jeb

IcarusLives
Posts: 97
Joined: 17 Jan 2016 23:55

Re: Strange behaviour with ansi sequence

#3 Post by IcarusLives » 13 Nov 2018 13:45

ECHO is providing CRLF so use this instead.

Code: Select all

<nul set /p "=%esc%[100m"

J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Re: Strange behaviour with ansi sequence

#4 Post by J'aitreschaud » 13 Nov 2018 21:29

@IcarusLives @jeb
Thank you both for your answers.
Also IcarusLives, I love your username; great reference.

Post Reply