making all features work with mixed formats

This commit is contained in:
Gabriel Falcão 2010-04-19 00:25:19 -03:00
parent 5bb4b785c7
commit d6dbb12f20
3 changed files with 61 additions and 11 deletions

View File

@ -41,16 +41,16 @@ couleur can overwrite output, so that you can make things like printing progress
import time
import couleur
shell = couleur.Shell(breakline=True)
shell = couleur.Shell(breakline=True, bold=True)
for num in range(101):
if num == 0:
print
shell.print_bold_white_on_red("Downloading file: %d%%" % num, replace=True)
shell.print_yellow_and_red("Downloading file: |%d%%" % num, replace=True)
time.sleep(0.05)
shell.print_bold_white_on_green("Downloading file: DONE!", replace=True)
shell.print_white_and_green("Downloading file: |DONE!", replace=True)
## free software

View File

@ -91,6 +91,7 @@ class Shell(object):
self._indent = 0
self._breakline = breakline
self._bold = bold
self._in_format = False
def indent(self):
self._indent += self._indentation_factor
@ -111,31 +112,51 @@ class Shell(object):
if colors:
parts.insert(0, back(colors[0]))
if self._bold:
parts.insert(0, ansify(1))
if not self._in_format:
if self._bold:
parts.insert(0, ansify(1))
if self._indent:
parts.insert(0, ' ' * self._indent)
if self._indent:
parts.insert(0, ' ' * self._indent)
if self._breakline:
parts.append("\n")
if self._breakline:
parts.append("\n")
string = "".join(parts)
return lambda z, replace=False: sys.stdout.write((replace and modifiers.up or '') + string % z)
def dec(z, replace=False):
pre = (replace and modifiers.up or '')
sys.stdout.write(pre + (string % z))
return dec
def __getattr__(self, attr):
if attr.startswith("print_"):
if _sep2 in attr:
self._in_format = True
printers = map(self._printer_for, attr.split(_sep2))
def dec(string):
def dec(string, replace=False):
unique = str(uuid.uuid4())
string = string.replace(r'\|', unique)
parts = string.split("|")
if replace:
sys.stdout.write(modifiers.up)
if self._indent:
sys.stdout.write(' ' * self._indent)
if self._bold:
sys.stdout.write(ansify(1))
for part, output in zip(parts, printers):
output(part.replace(unique, "|"))
if self._breakline:
sys.stdout.write("\n")
self._in_format = False
return dec
return self._printer_for(attr)

View File

@ -126,3 +126,32 @@ def test_update_shell():
sh.indent()
sh.print_red("Red", True)
assert_stdout('\033[33mYellow\033[0m\r\033[A \033[31mRed\033[0m')
@with_setup(prepare_stdout)
def test_update_shell_mixed_with_linebreak():
"updating the shell with mixed output and breakline enabled"
sh = Shell(breakline=True)
sh.print_yellow("Yellow")
sh.print_yellow_and_normal_and_red("Yellow| and |Red", True)
sh.print_green("Green")
assert_stdout('\033[33mYellow\033[0m\n\r\033[A\033[33mYellow\033[0m\033[39m and \033[0m\033[31mRed\033[0m\n\033[32mGreen\033[0m\n')
@with_setup(prepare_stdout)
def test_update_shell_mixed_with_indentation():
"updating the shell with mixed output and indentation"
sh = Shell(breakline=True)
sh.print_yellow("Yellow")
sh.indent()
sh.print_yellow_and_normal_and_red("Yellow| and |Red", True)
sh.dedent()
sh.print_green("Green")
assert_stdout('\033[33mYellow\033[0m\n\r\033[A \033[33mYellow\033[0m\033[39m and \033[0m\033[31mRed\033[0m\n\033[32mGreen\033[0m\n')
@with_setup(prepare_stdout)
def test_update_shell_mixed_with_bold():
"updating the shell with mixed output and bold enabled"
sh = Shell(bold=True)
sh.print_yellow("Yellow")
sh.print_yellow_and_normal_and_red("Yellow| and |Red", True)
sh.print_green("Green")
assert_stdout('\033[1m\033[33mYellow\033[0m\r\033[A\033[1m\033[33mYellow\033[0m\033[39m and \033[0m\033[31mRed\033[0m\033[1m\033[32mGreen\033[0m')