indent, dedent and bold

This commit is contained in:
Gabriel Falcão 2010-04-18 21:52:46 -03:00
parent d3eb114018
commit 3b311e90ef
3 changed files with 114 additions and 43 deletions

View File

@ -17,20 +17,20 @@ couleur has a syntax sugar that is semantically nice:
import couleur
sh = couleur.Shell(indent=4)
sh.bold_black_on_white('Nice highlight')
# prints '\033[1m\033[47m\033[30mNice highlight\033[0m'
sh.print_bold_black_on_white('Nice highlight')
# prints '\033[47m\033[1m\033[30mNice highlight\033[0m'
sh.indent()
# will increase a internal indentation factor in couleur.Shell instance
sh.green('Just green')
sh.print_green('Just green')
# prints indented as well ' \033[32mJust Green\033[0m'
sh.dedent()
# will decrease that indentation factor (above)
# syntax sugar
sh.green_and_nornal_and_blue('this will be printed in green| and |this in blue')
sh.print_green_and_nornal_and_blue('this will be printed in green| and |this in blue')
# see: '\033[32mthis will be printed in green\033[0m and \033[34mthis in blue\033[0m'
## free software

View File

@ -18,6 +18,8 @@
import sys
import uuid
__version__ = '0.1'
def ansify(number):
"""Wraps the given ansi code to a proper escaped python output
@ -51,7 +53,7 @@ class forecolors:
magenta = ansify(35)
cyan = ansify(36)
white = ansify(37)
default = ansify(39)
normal = ansify(39)
class backcolors:
black = ansify(40)
@ -62,10 +64,18 @@ class backcolors:
magenta = ansify(45)
cyan = ansify(46)
white = ansify(47)
default = ansify(49)
normal = ansify(49)
def fore(color):
return getattr(forecolors, color)
def get(what):
try:
r = getattr(modifiers, what)
except AttributeError:
r = getattr(forecolors, what)
return r
args = map(get, color.split("_"))
return "".join(args)
def back(color):
return getattr(backcolors, color)
@ -73,35 +83,59 @@ def back(color):
_sep1 = '_on_'
_sep2 = '_and_'
def _printer_for(color):
colors = color.split(_sep1)
parts = [
fore(colors.pop(0)),
"%s",
modifiers.reset
]
if colors:
parts.insert(0, back(colors[0]))
string = "".join(parts)
return lambda z: sys.stdout.write(string % z)
class Shell(object):
def __init__(self, indent=2, breakline=False, bold=False):
self._indentation_factor = indent
self._indent = 0
self._breakline = breakline
self._bold = bold
def indent(self):
self._indent += self._indentation_factor
def dedent(self):
self._indent -= self._indentation_factor
def _printer_for(self, color):
color = color.replace('print_', '')
colors = color.split(_sep1)
parts = [
fore(colors.pop(0)),
"%s",
modifiers.reset
]
if colors:
parts.insert(0, back(colors[0]))
if self._bold:
parts.insert(0, ansify(1))
if self._indent:
parts.insert(0, ' ' * self._indent)
if self._breakline:
parts.append("\n")
string = "".join(parts)
return lambda z: sys.stdout.write(string % z)
def __getattr__(self, attr):
if _sep2 in attr:
printers = map(_printer_for, attr.split(_sep2))
def dec(string):
unique = str(uuid.uuid4())
string = string.replace(r'\|', unique)
parts = string.split("|")
if attr.startswith("print_"):
if _sep2 in attr:
printers = map(self._printer_for, attr.split(_sep2))
def dec(string):
unique = str(uuid.uuid4())
string = string.replace(r'\|', unique)
parts = string.split("|")
for part, output in zip(parts, printers):
output(part.replace(unique, "|"))
return dec
for part, output in zip(parts, printers):
output(part.replace(unique, "|"))
try:
return _printer_for(attr)
except AttributeError:
return super(Shell, self).__getattribute__(attr)
return dec
return self._printer_for(attr)
return super(Shell, self).__getattribute__(attr)

View File

@ -40,44 +40,81 @@ def test_ansify():
def test_output_black_foreground():
"Test output: black foreground"
sh = Shell()
sh.black("Hello Black!")
sh.print_black("Hello Black!")
assert_stdout('\033[30mHello Black!\033[0m')
@with_setup(prepare_stdout)
def test_output_black_foreground_on_white_background():
"Test output: black foreground on white background"
sh = Shell()
sh.black_on_white("Hello Black!")
sh.print_black_on_white("Hello Black!")
assert_stdout('\033[47m\033[30mHello Black!\033[0m')
@with_setup(prepare_stdout)
def test_output_green_foreground():
"Test output: green foreground"
sh = Shell()
sh.green("Hello World!")
sh.print_green("Hello World!")
assert_stdout('\033[32mHello World!\033[0m')
@with_setup(prepare_stdout)
def test_mixed_output():
"Test mixed output"
"print_green_and_red_and_white is a valid call"
sh = Shell()
sh.green_and_red_and_white("Hello |World |for you!")
sh.print_green_and_red_and_white("Hello |World |for you!")
assert_stdout(
'\033[32mHello \033[0m\033[31mWorld \033[0m\033[37mfor you!\033[0m'
)
@with_setup(prepare_stdout)
def test_mixed_output_with_escaped_separator():
"print_green_and_red_on_yellow works is a valid call"
sh = Shell()
sh.green_and_red_on_yellow("Hello |World \|for you!")
sh.print_green_and_red_on_yellow("Hello |World \|for you!")
assert_stdout(
'\033[32mHello \033[0m\033[43m\033[31mWorld |for you!\033[0m'
)
@with_setup(prepare_stdout)
def test_mixed_output_with_backgrounds():
"test mixed output with nice background"
"print_green_on_magenta_and_red_and_white_on_blue is a valid call"
sh = Shell()
sh.green_on_magenta_and_red_and_white_on_blue("Hello |World |for you!")
sh.print_green_on_magenta_and_red_and_white_on_blue("Hello |World |for you!")
assert_stdout('\033[45m\033[32mHello \033[0m\033[31mWorld \033[0m\033[44m\033[37mfor you!\033[0m'
)
@with_setup(prepare_stdout)
def test_indent():
"indentation"
sh = Shell(indent=4, breakline=True)
sh.print_normal_on_blue("Hello")
sh.indent()
sh.print_normal_on_red("World")
assert_stdout('\033[44m\033[39mHello\033[0m\n \033[41m\033[39mWorld\033[0m\n')
@with_setup(prepare_stdout)
def test_dedent():
"de-indentation"
sh = Shell(indent=4, breakline=True)
sh.indent()
sh.print_normal_on_blue("Hello")
sh.dedent()
sh.print_normal_on_red("World")
assert_stdout(' \033[44m\033[39mHello\033[0m\n\033[41m\033[39mWorld\033[0m\n')
@with_setup(prepare_stdout)
def test_bold():
"bold text"
sh = Shell(bold=True)
sh.print_normal_on_blue("Hello")
sh.print_normal_on_red("World")
assert_stdout('\033[1m\033[44m\033[39mHello\033[0m\033[1m\033[41m\033[39mWorld\033[0m')
@with_setup(prepare_stdout)
def test_bold_inline():
"bold text with inline call"
sh = Shell()
sh.print_bold_normal_on_blue("Hello")
sh.print_bold_normal_on_red("World")
assert_stdout('\033[44m\033[1m\033[39mHello\033[0m\033[41m\033[1m\033[39mWorld\033[0m')