Monday, November 7, 2011

Displaying ANSI Escape Sequences in Windows

I installed Busybox on my Samsung Galaxy S II to enjoy the Swiss Army Knife of Embedded Linux. When I want to look at my phone SD card content, I often use adb if the phone is connected with my laptop. So I typed the following command at Windows command prompt:

adb shell ls /sdcard

What I got is somehow scary as shown below (only first line is shown):

←[1;34mAndroid←[0m ←[1;34mTitaniumBackup←[0m

After a while, I realized that what is shown is actually ANSI escape codes. The ls command provided by Busybox is actually showing color codes by default.

So what shall I do? I know option --color=never can suppress ANSI escape sequences but I don't like typing such long keystrokes. On the other hand, getting useful information out of those ANSI codes is very difficult. My first reaction is trying to see whether Windows shell can show those ANSI codes. I remember my old friend DOS can display ANSI codes, so a modern Windows 7 should be able to do so. But I cannot find relevant configuration in Properties dialog of command prompt. After reading Wikipedia page on ANSI escape codes, I found the following sentence Console windows in all versions of Windows do not support ANSI escape sequences at all. Therefore the only solution is to find some console replacements which can show ANSI codes.

I do found some by Googling. But before trying one, I asked myself why I should install one software for just such a trivial task? I suddenly thought: how about Emacs shell? Maybe Emacs folks have already solved this issue? So I fired up Emacs, typed M-x shell, and typed adb shell ls /sdcard in Emacs shell. Bingo! All ANSI color codes are correctly shown, just as in an Linux box when typing ls --color.

Ubiquitous Emacs to the rescue again.