Changeset View
Changeset View
Standalone View
Standalone View
scap/ansi.py
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
""" | """ | ||||
scap.ansi | scap.ansi | ||||
~~~~~~~~~ | ~~~~~~~~~ | ||||
ANSI escape codes | ANSI escape codes | ||||
.. seealso: http://en.wikipedia.org/wiki/ANSI_escape_code | ..seealso:: `https://en.wikipedia.org/wiki/ANSI_escape_code` | ||||
mobrovac: We're in TLS-only mode - `https` :P | |||||
""" | """ | ||||
FG_BLACK = 30 | FG_BLACK = 30 | ||||
FG_RED = 31 | FG_RED = 31 | ||||
FG_GREEN = 32 | FG_GREEN = 32 | ||||
FG_YELLOW = 33 | FG_YELLOW = 33 | ||||
FG_BLUE = 34 | FG_BLUE = 34 | ||||
FG_MAGENTA = 35 | FG_MAGENTA = 35 | ||||
Show All 23 Lines | |||||
def esc(*args): | def esc(*args): | ||||
""" | """ | ||||
Get an ANSI escape code. | Get an ANSI escape code. | ||||
>>> esc(BG_WHITE, FG_RED, BLINK) == r'\x1b[5;31;47m' | >>> esc(BG_WHITE, FG_RED, BLINK) == r'\x1b[5;31;47m' | ||||
True | True | ||||
:param *args: ANSI attributes | :param args: ANSI attributes | ||||
:returns: str | :returns: str | ||||
""" | """ | ||||
return '\x1b[%sm' % ';'.join(str(arg) for arg in sorted(args)) | return '\x1b[%sm' % ';'.join(str(arg) for arg in sorted(args)) | ||||
def format(*args): | def format(*args): | ||||
""" | """ | ||||
create an ansi color string from a list of color codes and plain strings. | create an ansi color string from a list of color codes and plain strings. | ||||
>>> format((FG_BLUE,BG_WHITE),'blue on white') \ | >>> format((FG_BLUE,BG_WHITE),'blue on white') \ | ||||
== '\x1b[34;47mblue on white\x1b[0m' | == '\x1b[34;47mblue on white\x1b[0m' | ||||
True | True | ||||
:param *args: ANSI color codes and strings of text | :param args: ANSI color codes and strings of text | ||||
:returns: str | :returns: str | ||||
""" | """ | ||||
result = "" | result = "" | ||||
for arg in args: | for arg in args: | ||||
argtype = type(arg) | argtype = type(arg) | ||||
if argtype is int: | if argtype is int: | ||||
result += esc(arg) | result += esc(arg) | ||||
elif argtype is tuple: | elif argtype is tuple: | ||||
Show All 16 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 3.0 (CC-BY-SA) unless otherwise noted; code licensed under GNU General Public License (GPL) or other open source licenses. By using this site, you agree to the Terms of Use, Privacy Policy, and Code of Conduct. · Wikimedia Foundation · Privacy Policy · Code of Conduct · Terms of Use · Disclaimer · CC-BY-SA · GPL
We're in TLS-only mode - https :P