File Exchange

image thumbnail

tcprintf : ANSI colored output in terminal

version 1.0.0.0 (161 KB) by Daniel O'Shea
tcprintf provides colored output when using MATLAB from a terminal using ANSI escape codes.

1 Download

Updated 20 Sep 2012

View License

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.

Example:
tcprintf('lightGray onRed underline', 'Message: %20s\n', msg);

Author: Dan O'Shea (http://djoshea.com) (c) 2012

Released under the open source BSD license
http://opensource.org/licenses/bsd-license.php

Comments and Ratings (5)

Hi Ali, still working for me in iTerm2 on MacOS. This utility just outputs ANSI escape codes for the 256 colors in the palette. If your terminal isn't displaying it, the terminal may not be setup for color. If you're on Mac or Linux, you can try setting the environment variables:

COLORTERM=truecolor
TERM=xterm-256color

You can check whether a command like "ls" produces color output too to see if the problem is with your terminal.

If you're on Windows, apparently the Windows console should display colors, but I can't adequately help you debug as I don't have access to a Windows machine.
https://blogs.msdn.microsoft.com/commandline/2016/09/22/24-bit-color-in-the-windows-console/

Thanks!

Even the demo didn't work well for me. everything displayed in black.

Vincent

perfect! exactly what I was looking for.

Yair Altman

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

Yair Altman

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux