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.
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:

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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With