Skip to content

Commit

Permalink
DOC: more detailed section about ANSI colors
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Feb 2, 2016
1 parent 14fbcc4 commit a2cc323
Showing 1 changed file with 111 additions and 26 deletions.
137 changes: 111 additions & 26 deletions doc/code-cells.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,6 @@
"\"I'm the 'normal' output\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ANSI Colors\n",
"\n",
"The standard output and standard error streams may contain [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code) to change the text and background colors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('BEWARE: \\x1b[1;33;41mugly colors\\x1b[0m!', file=sys.stderr, flush=True)\n",
"for bg in range(40, 48):\n",
" for fg in range(30, 38):\n",
" print('\\x1b[{fg};{bg}mX\\x1b[0m'.format(**locals()), end='')\n",
" print('\\x1b[1;{fg};{bg}mX\\x1b[0m'.format(**locals()), end='')\n",
" print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -316,6 +291,13 @@
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### YouTube Videos"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -326,10 +308,113 @@
"source": [
"YouTubeVideo('iV2ViNJFZC8')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ANSI Colors\n",
"\n",
"The standard output and standard error streams may contain [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code) to change the text and background colors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('BEWARE: \\x1b[1;33;41mugly colors\\x1b[m!', file=sys.stderr, flush=True)\n",
"print('ABC\\x1b[43mDEF\\x1b[35mGHI\\x1b[1mJKL\\x1b[49mMNO\\x1b[39mPQR\\x1b[22mSTU')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following code showing the 8 basic ANSI colors is based on http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html.\n",
"Note that Jupyter does not switch to a brighter color for bold text."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"text = ' XYZ '\n",
"formatstring = '\\x1b[{}m' + text + '\\x1b[m'\n",
"\n",
"print(' ' * 6 + ' ' * len(text) +\n",
" ''.join('{:^{}}'.format(bg, len(text)) for bg in range(40, 48)))\n",
"for fg in range(30, 38):\n",
" for bold in False, True:\n",
" fg_code = ('1;' if bold else '') + str(fg)\n",
" print(' {:>4} '.format(fg_code) + formatstring.format(fg_code) +\n",
" ''.join(formatstring.format(fg_code + ';' + str(bg))\n",
" for bg in range(40, 48)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ANSI also supports a set of 256 indexed colors.\n",
"The following code showing all of them is based on http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"formatstring = '\\x1b[38;5;{0};48;5;{0}mX\\x1b[1mX\\x1b[m'\n",
"\n",
"print(' + ' + ''.join('{:2}'.format(i) for i in range(36)))\n",
"print(' 0 ' + ''.join(formatstring.format(i) for i in range(16)))\n",
"for i in range(7):\n",
" i = i * 36 + 16\n",
" print('{:3} '.format(i) + ''.join(formatstring.format(i + j)\n",
" for j in range(36) if i + j < 256))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can even use 24-bit RGB colors:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"start = 255, 0, 0\n",
"end = 0, 0, 255\n",
"length = 79\n",
"out = []\n",
"\n",
"for i in range(length):\n",
" rgb = [start[c] + int(i * (end[c] - start[c]) / length) for c in range(3)]\n",
" out.append('\\x1b['\n",
" '38;2;{rgb[2]};{rgb[1]};{rgb[0]};'\n",
" '48;2;{rgb[0]};{rgb[1]};{rgb[2]}mX\\x1b[m'.format(rgb=rgb))\n",
"print(''.join(out))"
]
}
],
"metadata": {
"celltoolbar": "Raw Cell Format",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
Expand Down

0 comments on commit a2cc323

Please sign in to comment.