Ir para o conteúdo
ou

Software livre Brasil

 Voltar a Blog
Tela cheia

A visual cheat sheet for ANSI color codes

28 de Janeiro de 2012, 0:00 , por Software Livre Brasil - | Ninguém está seguindo este artigo ainda.
Visualizado 12975 vezes

Now and then I want to output some ANSI color escape codes from software I write, and I always end up doing some trial-and-error to figure out the exact codes I want. Sometimes it’s overkill to add a dependency on an existing library that already deals with it, or the language I am using does not have one.

There are a lot of listings of the ANSI color codes out there, but I couldn’t find one that matches the actual codes with the resulting effect in a visual way. Even the Wikipedia article has a colored table with the actual colors, but I have to lookup manually which code combination produces which color.

So I spent a few minutes to write a shell script that prints all useful combinations, formatted with themselves. This way I can quickly figure out which exact code I want to achieve the desired effect.

The code for now is very simple:


#!/bin/sh -e

for attr in $(seq 0 1); do
  for fg in $(seq 30 37); do
    for bg in $(seq 40 47); do
      printf "\033[$attr;${bg};${fg}m$attr;$fg;$bg\033[m "
    done
    echo
  done
done

Is there a package in Debian that already does that? Would people find it useful to have this packaged?

update: it turns out you can find some similar stuff on google images. It was a quick and fun hack, though.

update 2: Replacing echo -n with printf makes the script work independently if /bin/sh is bash or dash. Thanks to cocci for pointing that out.


Tags deste artigo: english ansi shellscript

66 comentários

  • 01cf38858a3a705d200e722c77b4f8a5?only path=false&size=50&d=404John Eikenberry(usuário não autenticado)
    28 de Janeiro de 2012, 18:49

     

    > Is there a package in Debian that already does that?
    Yep.

    $ apt-cache search colortest
    colortest - utilities to test color capabilities of terminal
    colortest-python - utility to test color capabilities of terminal


  • 2d838804f63f868fa58e9bf69a1caacd?only path=false&size=50&d=404Chris K(usuário não autenticado)
    28 de Janeiro de 2012, 19:41

    This hack is better

    I hadn't previously known about the 'colortest' package, but I like Antonnio's small hack even better because it tells the user the ANSI color code to use for the desired color. Telling me that something is "black foreground with black background" isn't as useful as showing me black letters on red background with "0;30;41" to tell me that that's the CODE for black-foreground with red background. And unfortunately the colortest-[8|16|16b|256] binaries don't have command line options to show the codes.

    So regardless, I appreciate this little script and have put it in my home directory in ~/bin.

    Thanks!!


  • 01cf38858a3a705d200e722c77b4f8a5?only path=false&size=50&d=404John Eikenberry(usuário não autenticado)
    29 de Janeiro de 2012, 4:51

     

    The colortest-python package supports showing the numeric and rgb codes which are most useful for 88 and 256 color terminals.


  • Db738e655dfa219689e3d27548da0edc?only path=false&size=50&d=404deansu(usuário não autenticado)
    29 de Janeiro de 2012, 5:56

     

    As some one who in the past has messed around with MUD server code and more recently played with creating fancy zshell prompts I appreciate this little ANSI code snippet. :)


  • 4c8471adb96db0bb7980a865a8f15b7a?only path=false&size=50&d=404cocci(usuário não autenticado)
    1 de Fevereiro de 2012, 17:56

    not working with bash

    Hi, nice script. It runs fine with Dash. If you want to run it with Bash, need to add the `-e' option to the first `echo'.
    But doing so it'll run bad with Dash. :(
    (don't know if I'm worldwide-right, thiis is my debian box behavior...)


  • C648804652ff8f8cb724f7f3e0d02312?only path=false&size=50&d=404Charles(usuário não autenticado)
    9 de Março de 2012, 20:32

     

    Simple and useful... as always! Você broca!