Saturday, July 28, 2007

ANSI Screen Escape Characters

I am posting this so I can find it later when I need it. If you find it useful then awesome.

  • \033[ or Ctl-V,Esc,[ Starts a escape sequence

  • m should be the last character in a sequence that sets font and/or background color

  • a two digit number starting with 3 is for setting the font color

  • a two digit number starting with 4 is for setting the background color

  • the second digit of a two digit number represents the color
    0 => black
    1 => red
    2 => green
    3 => yellow
    4 => blue
    5 => purple
    6 => cyan
    7 => white

  • a single digit number represents a text style
    0 => reset
    1 => bold
    3 => italics
    4 => underline
    5 => blink
    7 => reverse
    8 => invisible

  • cursor movement starts with a number which represents the number of characters to move and ends with a capital letter representing the direction
    A => up
    B => down
    C => right
    D => left

  • to move the cursor to a specific location it requires column number then a ; then the row number and ends with the letter H

  • to clear the screen and move the cursor to the home position it requires the number 2 and the capital letter J


Examples:

echo '\033[31mHello World\033[0m'
\033[31m - Sets the text color to red.
Hello World - Prints 'Hello World' to the screen
\033[0m - Resets everything back to normal

echo 'XXXX\033[3DY'

XXXX - Prints XXXX
\033[3D - Moves back 3 characters
Y - Prints a Y
The output would look like 'XYXX'

No comments:

Post a Comment