Uses ANSI escape codes to print colored output when using MATLAB
from a terminal. If not running in a terminal, or if called by MATLAB's
datatipinfo function, tcprintf reverts to standard printf. The latter is
desirable if tcprintf is used within an object's disp() method to avoid
seeing the ANSI characters here.
The first argument is an style description that consists of space-separated
words. These words may include:
one of the following colors:
black, red, green, yellow, blue, purple, cyan, darkGray, lightGray, white
one of the following background colors:
onBlack, onRed, onGreen, onYellow, onBlue, onPurple, onCyan, onWhite
and any of the following modifiers:
bright : use the bright (or bold) form of the color, not applicable for
black, darkGray, lightGray, or white
underline : draw an underline under each character
blink : This is a mistake. Please don't use this ever.
tcprintf('lightGray onRed underline', 'Message: %20s\n', msg);
Author: Dan O'Shea (http://djoshea.com) (c) 2012
Released under the open source BSD license
perfect! exactly what I was looking for.
Very nice idea and implementation. I liked the blink [counter-]suggestion :-)
A few ideas for improvement:
- make the inputs case-insensitive
- revert to cprintf (if available) in case of usejava('desktop') and not datatip
- use the full 256 color indices use by xterm for fg,bg colors (ANSI codes 38,48 respectively); KDE's Konsole even supports full RGB here
- codes 35,45 are often called magenta, not purple; I suggest that you accept both of these as synonyms to this code
- similarly, 33,43 are displayed as brown on VGA monitors, so I suggest accepting "brown" as a synonym
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.