

ANSI Escape Codes Explained - cwoebker
http://cwoebker.com/posts/ansi-escape-codes

======
SeoxyS
I was a little disappointed by the _explanation_ – I was expecting this to
dive deep into how ANSI code came to be, and how they worked. Not just "hey
they exist and are neat."

You're probably better off reading the Wikipedia article:

<https://en.wikipedia.org/wiki/ANSI_escape_code>

Some of the much more interesting codes are the ones that control cursor
positioning and redrawing, and are used to create ASCII UIs by libraries like
ncurses & readline, and programs like vim & tmux.

I've written a rudimentary chat interface using native ANSI code, which was
quite a bit trickier than it looks, but a fun exercise. The tricky part was
ensuring that an interactive line of input remained at the bottom of the
screen, while the chat room history keeps scrolling above[1].

[https://github.com/ChartBoost/zmq-
examples/blob/master/chat/...](https://github.com/ChartBoost/zmq-
examples/blob/master/chat/client.rb)

[1]: of course, if you're actually building something for production, don't be
an idiot like me and use a real library like ncurses.

~~~
duskwuff
Also an interesting, useful reference:

<http://vt100.net/docs/vt100-ug/>

Note that this doesn't include all VT100 escape codes supported by modern
terminals, but it is the basis for how they all work.

~~~
thristian
That site is historically interesting (and very useful if you want to write
your own terminal emulator) but most actual terminals these days are actually
xterm emulators, so it makes sense to look at the xterm docs:
<http://invisible-island.net/xterm/ctlseqs/ctlseqs.pdf>

------
afhof
Weak explanation and crummy code. It didn't answer one of my long standing
ansi escape code questions: whats the deal with that 256 (really 216) color
space that programs like htop use? How do I get that, and why does it exist?
Also:

"cls.ESCAPE % cls.BOLD % cls.UNDERLINE % cls.COLOR['white']"?

Seriously? The "+" operator does the exact same thing in this case and is
easier to read. I can forgive the typos in the article, but teaching others
with poor code? That is less easy to forgive. If the original author reads
this, please take the time to revise the article, the code, and resubmit.

~~~
jessedhillon
Give him a break, he's still in high school.

~~~
derleth
> Give him a break, he's still in high school.

That _was_ a break. Constructive criticism is useful, as opposed to lowered
expectations based on ageism.

~~~
jessedhillon
Ageism!? Supposing I take this response seriously, instead of rolling my eyes
and thinking STFU (as I am wont to receive such inanity) --

Asking if this is a serious submission, calling a beginner's effort "crummy"
and declaring the whole thing nearly unforgivable is not constructive
criticism. The one opportunity the OP had to be constructive, he blew: he
could have written an improved code sample so that cwoebker could learn
something directly.

You can talk to children and beginners however you want. My experience has
shown me that encouraging such people by _gently_ correcting mistakes and
offering feedback along with praise, is a very likely to encourage people to
keep trying. Especially when nothing important is at stake.

------
cwoebker
Hey guys, I am the one who wrote this little post. My friend suggested to
submit it to HN. I didn't expect to land on the front page. I'll make sure to
fix all the bugs and spelling mistakes and improve the examples etc. Thanks a
lot for the feedback.

------
habosa
This Haskell package [http://hackage.haskell.org/packages/archive/ansi-
terminal/0....](http://hackage.haskell.org/packages/archive/ansi-
terminal/0.5.0/doc/html/System-Console-ANSI.html) does all of the hard work
for Haskell command line tools looking to use colors, etc. I used it in my
task manager, shpm, and it was awesome.

~~~
batterseapower
I'm glad you liked my package. Let me just plug my related package ansi-wl-
pprint (<http://hackage.haskell.org/package/ansi-wl-pprint-0.6.4>) which (I
think) is an even more convenient interface to ANSI text colouring for most
programs.

------
cwoebker
I made a bunch of fixes. Thanks for the feedback again. Let me know if you
find any other issues with it.

------
46Bit
If any Ruby people want to use some sections of this a bit more cleanly -
<https://github.com/defunkt/colored>

~~~
foz
I think Term::ANSIColor has become the standard for Ruby, it's included by
Capistrano and other popular gems now: <https://github.com/flori/term-
ansicolor> \- defunkt's gem has not been touched in 3 years.

~~~
46Bit
Ah, thanks.

------
pjscott
Why make a class with class methods? Using a plain old module would do the
same thing, easier.

~~~
ekimekim
Indeed, I made my own one of these a few years ago, it was really just a
module defining a bunch of constants. Well, not entirely constants. Rather
than defining, say, FG_RED, BG_RED, FG_GREEN, BG_GREEN, etc. I had functions
FG and BG that took color codes, like FG(RED) + BG(GREEN) + BOLD + "Garish
horrible colors!!" + RESET.

Actually I prefer working in inline escapes like that to curses overly complex
method of defining color pairs and only ever specifying which to use at write-
time. I wish curses had some way to inline attributes in a piece of text, that
I can then use as normal data, pass around, etc.

