Add color to console app text output

Ideas for new features & functions

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Add color to console app text output

Post by Grovkillen » Tue Oct 29, 2013 1:00 pm

Hello,

Just a small question, is it possible to add color to console app text output (see example here)? It would be great if I could use green, yellow and red to indicate my script status when using the console app interface. Not a deal breaker but a nice feature to have.

My latest SolidWorks API application uses the command line to talk to SolidWorks and it would be nice if I could use colors to display the state of the app.

Concept picture:
Image
Let>ME=%Script%

Running: MS14.5.4

User avatar
Marcus Tettmar
Site Admin
Posts: 7118
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Nov 05, 2013 7:41 am

No, none that I am aware of.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Post by Grovkillen » Tue Nov 05, 2013 9:13 am

Ok, thanks.

But this might help some?
Let>ME=%Script%

Running: MS14.5.4

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Add color to console app text output

Post by Grovkillen » Tue Aug 11, 2015 10:26 am

I found this today. Is it possible to do something with this?

http://stackoverflow.com/a/5344911/463115

Save this as a Bacth.bat file and watch it print! :)

Code: Select all

@echo off
setlocal disableDelayedExpansion
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

EDIT: found this aswell:
http://www.dostips.com/forum/viewtopic.php?f=3&t=4881
Let>ME=%Script%

Running: MS14.5.4

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Add color to console app text output

Post by Grovkillen » Wed Aug 12, 2015 7:58 am

Python package:

https://pypi.python.org/pypi/colorama

How would I go about to use this?
Let>ME=%Script%

Running: MS14.5.4

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Add color to console app text output

Post by Grovkillen » Tue Sep 22, 2015 6:47 am

Sorry to lift this once again but it'd be great if someone could point me in the right direction on how to use the phyton-plugin (if possible)?
Let>ME=%Script%

Running: MS14.5.4

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Add color to console app text output

Post by Grovkillen » Thu Mar 03, 2016 6:25 am

I'm not able to try this on a Win10 computer right now but it seems like as of Win10 some colors are supported by the console.

http://www.nivot.org/blog/post/2016/02/ ... hancements

Any thoughts?
Let>ME=%Script%

Running: MS14.5.4

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Add color to console app text output

Post by Grovkillen » Thu Mar 03, 2016 8:14 am

I just complied following script and tried on my Win10-tablet and it indeed worked! It'll work on Win7 and Win8 by using the ConEmu software for those with older systems. But yes, it worked nativly in Win10!!! :)

Code: Select all

Let>TEXT1=Will it work....
SOWrite>%TEXT1%
Wait>1
Let>TEXT2=Yay!
//"^[0;30;47m" is just a random color I picked, "^[0m" is to turn the color back to default (none)
SOWriteLn>^[0;30;47m%TEXT2%^[0m

//"^" need to be replace with the "escape code" (please refer to my attached .scp file)
//Compile as a console app and start using the cmd
EDIT: Colors i found so far (ConEmu):
ESC[0;30;40m = Black
ESC[0;30;41m = Blue
ESC[0;30;42m = Green 1
ESC[0;30;43m = Green 2
ESC[0;30;44m = Red
ESC[0;30;45m = Magenta
ESC[0;30;46m = Brown
ESC[0;30;47m = White

(Win10 cmd)
ESC[0;30;40m = Black
ESC[0;30;41m = Red
ESC[0;30;42m = Green
ESC[0;30;43m = Yellow/Brown
ESC[0;30;44m = Blue
ESC[0;30;45m = Magenta
ESC[0;30;46m = Turquoise
ESC[0;30;47m = White

EDIT2: found all the available colors here(scroll down a bit)...
Attachments
test.scp
(634 Bytes) Downloaded 214 times
Let>ME=%Script%

Running: MS14.5.4

User avatar
Grovkillen
Automation Wizard
Posts: 406
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Add color to console app text output

Post by Grovkillen » Mon May 09, 2016 7:57 am

Just a nice to have feature: would it be possible to have "escape code character" as an internal variable or something similar. It would be a bit easier to call that variable than copy the escape charater from my notepad document. (I have no idea how to get this character using ascii command).
Let>ME=%Script%

Running: MS14.5.4

User avatar
JRL
Automation Wizard
Posts: 3318
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Add color to console app text output

Post by JRL » Mon May 09, 2016 12:54 pm

Its been about 20 years since I've used ansi codes but I believe ansi escape is ASCII character 27. Therefore you can assign the character to a variable like this:

Code: Select all

VBEval>chr(27),esc

Post Reply