Terminals are so boring these days. They used to look like this:
Now they're just bland and dull and black-and-white. I want you to write me a program that will make my terminal all colorful again!
Description
Take this example Ruby code:
Most Linux terminals support these escape sequences (\e
stands for the escape character), and Windows can support them with ANSICON. Here is the syntax of the specific escape sequence that can change the text or background color of a string:
\e[{{COLOR}}m
where \e
stands for the escape character (0x1B
in ASCII) and {{COLOR}}
is replaced by the number of the color that you want to use (more details on that later). Text that comes after this escape sequence will be formatted as directed, and a value of 0
will reset all formatting.
Your challenge is to take a string specifying some text that may contain color, and output a colorful version of it.
Input / Output
Normal text works just like normal, and is printed literally. For example, the input waffles
yields the same output, with no special color.
The syntax for specifying a color is similar to Wikipedia's syntax. For example, to color the words "the color red" in red in the sentence This is the color red!
, the input would be:
This is {{red|the color red}}!
Background colors work too. If you wanted black letters on a white background, you would use this:
{{black|white|This text is black on white}}
To get only a background color, omit the foreground:
{{|red|This text has a red background}}
Specification
Two open curly brackets always specify the beginning of a color directive. Two closing curly brackets specify the end. Brackets will always match; there will never be a {{
without a corresponding }}
, and a }}
will never come before its corresponding {{
. These color directives will not be nested, and a {{
will never appear within a color directive.
Within a color directive, there will always be either one or two |
symbols. If there is one, the text before it is the foreground color and the text after is the string to show in that color. If there are two, the text before the first one is the foreground color, the text after the first but before the second is the background color, and the text after the second is the string to display. These vertical bars may exist outside of a color directive, and should be printed literally.
The foreground color or background color (but not both) may be empty, in which case you should leave them as the default. The final string (the one to output) will never be empty.
Here are the directions to output text of a certain color:
A color sequence is defined in the "Description" section. For example, a color sequence of 42 would be
"\e[42m"
.To set a color, print the color sequence of the number determined below:
Color name | Color sequence number (foreground / background) --------------+---------- black | 30 / 40 red | 31 / 41 green | 32 / 42 yellow | 33 / 43 blue | 34 / 44 magenta | 35 / 45 cyan | 36 / 46 lightgray | 37 / 47 darkgray | 90 / 100 lightred | 91 / 101 lightgreen | 92 / 102 lightyellow | 93 / 103 lightblue | 94 / 104 lightmagenta | 95 / 105 lightcyan | 96 / 106 white | 97 / 107
Color names are case sensitive, and an invalid color name will never be provided. You don't have to handle stuff like
RED
orlightgrey
(spelled with ane
).After you print a color sequence, it will apply for all text following it. To end a color sequence (reset to the default color), output a color sequence of
0
("\e[0m"
).
Test case
{{|yellow| }}
{{|yellow| }} {{|yellow| }}
{{|yellow| }} {{red|'}} {{red|'}} {{|yellow| }}
{{|yellow| }} \_/ {{|yellow| }}
{{|yellow| }} {{|yellow| }}
{{|yellow| }}
This should output a smiley face... with evil red eyes.
Rules
You may not use any libraries or functions of your programming language to automatically parse a color. This means that you must be the one to determine what
"red"
means; you can't have a library automatically do that for you.This is code-golf, so the shortest code in bytes will win!
\n\[\e[32m\]\w\n\[\e[0m\]>
(green directory name, plain prompt on next line), but I can't get it to work from a program (tried python and Java so far). Any ideas? \$\endgroup\$echo -e "\e[31mtest\e[0m"
. \$\endgroup\$lolcat
. \$\endgroup\$you
he figuratively meansyour program
(as opposed to a call to a library function), and that he takesdetermine
in the sense offigure out
, not as inchoose
. Ie, it is your program that should handle the mapping: String("red") |-> Integer(31).red
is only31
because he says so, that information needs to be integrated into the program. Although it may be argued exactly what would count asyour program
- can we use general-purpose String manipulation functions? - don't blatantly cheat/abuse. \$\endgroup\$