3

The choice command is often used in batch coding. It has the /n option to hide ChoiceKeys. This robvanderwoude.com page explains how to use ANSI sequences to highlight in particular text attributes. But it doesn't give usage examples in real batch code.

So, I ask if it's possible to use an ANSI sequence to highlight a default option in the CHOICE command, i.e. make it bold or underlined, possibly by using a variable message that includes one or all choice letters? Or, maybe a different way exists to accomplish that, except using a (C)apital letter for default choice in its text message?

choice /c ox /n /m "C(o)ntinue or e(x)it?" /t 20 /d o

ANSI sequences support is back in Windows 10, but no code usage examples are given by Microsoft.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sambul35
  • 1,058
  • 14
  • 22
  • 1
    You might be interested in this post: [How to have multiple colors in a Windows batch file?](http://stackoverflow.com/a/5344911) You cannot modify the style/colour of the `choice` prompt, but if you do not use the message of `choice` and place `/N`, you can print the message on the screen using the linked method to highlight a substring by colouring... – aschipfl Jul 06 '16 at 22:47
  • Many things on robvanderwoude.com are outdated. The entire debug page, for example. – SomethingDark Jul 06 '16 at 22:52
  • @aschipfl I still wonder if its possible to highlight a default choice in the choice message, if I use variable instead of text: `choice /c yn /n /m "%mes1%" /t 20 /d y` The variable should be formattable any way possible outside of the Choice command - correct? – sambul35 Jul 06 '16 at 22:59
  • No, the variable simply contains a string, that is it, no formatting at all. – aschipfl Jul 06 '16 at 23:07
  • Another important thing is the amount of extra code required for such a basic thing as one letter highlighting. That's why robvanderwoude.com page sounds promising by offering very short ANSI sequences. Sadly, it doesn't give any code examples whatsoever, whether obsolete or not. Can someone post a code example that implements a bold text formatting ANSI sequence posted on Rob's site? – sambul35 Jul 06 '16 at 23:10
  • Note this part from the page you linked: `Windows NT doesn't support ANSI by default`. – dxiv Jul 06 '16 at 23:16
  • Example of use of Ansi sequences from a Batch file: `for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a" & echo Normal text!ESC![31mRED TEXT!ESC![32mGREEN TEXT` – Aacini Jul 07 '16 at 00:24
  • As noted, ansi sequences doesn't work in NT, XP, Win7, Win8. But in W10 it could work again. For the other systems you need to use the findstr technic – jeb Jul 07 '16 at 09:57
  • Thanks _aacini_ and _jeb_. As always top notch! I did some digging, and found that as you mentioned ANSI sequences are [back](https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx) in Win10. With this in mind, how can I modify CHOICE command message or its %mes1% variable to show one option in bold, like: "C(**o**)ntinue or e(x)it?" Such code example would help many, who never worked with Win95 ANSI features, and ,may alos serve as the above robvanderwoude.com page complement. – sambul35 Jul 07 '16 at 14:39
  • If you want to be portable (running on any computer), ANSI is obviously not an option. Some possible suggestions: `C(o)ntinue or e(x)it? [o] `, `c(O)ntintue or e(x)it?`, `C(o)ntinue or e(x)it? [default: cOntinue]`, `C(o)ntinue or e(x)it [Ox]?`, `C(_o_)ntinue or e(x)it?` – Stephan Jul 07 '16 at 15:14

2 Answers2

3

I tried this method at first:

choice /c ox /n /m "C(%ESC%[1;33mo%ESC%[0m)ntinue or e(x)it?" /t 20 /d o

However, it seems that the choice command send its message directly to the screen, bypassing the ANSI driver management, so this method don't work (at least not on my system).

The way to solve this point is using a separate set /P command to show the message, and then execute the choice command with no message:

@echo off
setlocal

for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
echo Normal text%ESC%[1;31m RED TEXT%ESC%[1;32m GREEN TEXT%ESC%[0m
echo/
set /P "=C(%ESC%[1;33mo%ESC%[0m)ntinue or e(x)it? " < NUL
choice /c ox /n /t 20 /d o
echo Option: %errorlevel%

This is the output:

ANSI test

NOTE: This answer just solves the point about "How to use an ANSI sequence to highlight a default option in CHOICE command" as stated in the original question. I don't touch the point about the availability of ANSI sequences at any particular machine; that is an entirely different matter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Great! Since code formatting doesn't work well in Stack Overflow comments, I post it as an answer. Aacini's code for CHOICE message works well in Windows 10 64-bit without set /P (on my PC).

However, there might be a simpler way to accomplish that? :) Maybe we should look back, how it was done in Windows 95 in a similar case (despite the CHOICE command wasn't there). Anyway, it seems the FOR line can be added just once in a batch with multiple color messages. And the above code works only in Windows versions supporting ANSI sequences.

@echo off
setlocal

for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
choice /c ox /n /m "C(%ESC%[1;33mo%ESC%[0m)ntinue or e(x)it?" /t 20 /d o
echo Option: %errorlevel%

:eol
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sambul35
  • 1,058
  • 14
  • 22
  • suggestion: `set "red=%ESC%[1;31m"`,`set "normal=%ESC%[0m"`, `choice /c ox /n /m "C(%red%o%normal%)ntinue or e(x)it?" /t 20 /d o` – Stephan Jul 07 '16 at 19:49
  • It still doesn't eliminate the need for FOR statement in Win10. :) – sambul35 Jul 07 '16 at 19:57
  • 1
    use an editor, where you can input `ESC`. Alternative: `echo ^[>>mybatch.bat` echoes `ESC` to the end of your `mybatch.bat` (notepad will show it as a little box) and you can copy-paste it to wherever you need it. Generate `^[` by pressing and holding `ALT`, entering `027`, then release `ALT` – Stephan Jul 07 '16 at 20:20
  • Yes, if entered like that it works perfect in CHOICE command in Win10 without FOR line. – sambul35 Jul 07 '16 at 20:46
  • 1
    @Stephan: The keyboard in all modern laptops lacks the numeric keypad (or any way to emulate it), so there is no way to generate control chars in these computers via the `Alt-nnn` method anymore... **`:-(`** – Aacini Jul 08 '16 at 00:26
  • @Aacini: I just verified: Switch on "NumLock" and use [press ALT]-M-K-7-[release ALT]. Works fine. The numbers are even printed on the keys in little boxes. – Stephan Jul 08 '16 at 05:21
  • @Stephan: my laptop keyboard have NOT "NumLock" key! See [this image](http://www.ebay.com/itm/HP-Compaq-CQ43-CQ45-CQ57-CQ58-Laptop-Replacement-US-Keyboard-646125-001-/322127052737?hash=item4b0044bfc1:g:i8oAAOSwNsdXSiXw). Most of the new laptops on sale in computer stores right now have the same type of keyboard... – Aacini Jul 08 '16 at 05:56
  • Is there any functional difference between left and right Alt keys on an extended PC keyboard? Can any Alt key be used for similar tasks interchangeably? – sambul35 Jul 08 '16 at 13:44
  • @sambul35 let me try it for you ... no, it only works with the left one. Also the right one is labeled `Alt Gr`, so it's different. – Stephan Jul 08 '16 at 14:20
  • I wonder if there's a similar way in Win10 to highlight one of default ChoiceKeys like [Y,N] that show up when CHOICE command option /n is not used? Likely not, as _aschipfl_ mentioned above. – sambul35 Jul 08 '16 at 19:43
  • You ought to keep to form here, not posting a comment as an answer. Perhaps, as there seems to be part of a real answer here, split it up into a comment to Aacini's answer and your own answer? And then use comments in both places for cross-referencing. You have two days to fix it before I flag this answer. – Peter Mortensen Jul 21 '16 at 09:21