making all features work with mixed formats
This commit is contained in:
parent
5bb4b785c7
commit
d6dbb12f20
|
@ -41,16 +41,16 @@ couleur can overwrite output, so that you can make things like printing progress
|
||||||
import time
|
import time
|
||||||
import couleur
|
import couleur
|
||||||
|
|
||||||
shell = couleur.Shell(breakline=True)
|
shell = couleur.Shell(breakline=True, bold=True)
|
||||||
|
|
||||||
for num in range(101):
|
for num in range(101):
|
||||||
if num == 0:
|
if num == 0:
|
||||||
print
|
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)
|
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
|
## free software
|
||||||
|
|
||||||
|
|
37
couleur.py
37
couleur.py
|
@ -91,6 +91,7 @@ class Shell(object):
|
||||||
self._indent = 0
|
self._indent = 0
|
||||||
self._breakline = breakline
|
self._breakline = breakline
|
||||||
self._bold = bold
|
self._bold = bold
|
||||||
|
self._in_format = False
|
||||||
|
|
||||||
def indent(self):
|
def indent(self):
|
||||||
self._indent += self._indentation_factor
|
self._indent += self._indentation_factor
|
||||||
|
@ -111,31 +112,51 @@ class Shell(object):
|
||||||
if colors:
|
if colors:
|
||||||
parts.insert(0, back(colors[0]))
|
parts.insert(0, back(colors[0]))
|
||||||
|
|
||||||
if self._bold:
|
if not self._in_format:
|
||||||
parts.insert(0, ansify(1))
|
if self._bold:
|
||||||
|
parts.insert(0, ansify(1))
|
||||||
|
|
||||||
if self._indent:
|
if self._indent:
|
||||||
parts.insert(0, ' ' * self._indent)
|
parts.insert(0, ' ' * self._indent)
|
||||||
|
|
||||||
if self._breakline:
|
if self._breakline:
|
||||||
parts.append("\n")
|
parts.append("\n")
|
||||||
|
|
||||||
string = "".join(parts)
|
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):
|
def __getattr__(self, attr):
|
||||||
if attr.startswith("print_"):
|
if attr.startswith("print_"):
|
||||||
if _sep2 in attr:
|
if _sep2 in attr:
|
||||||
|
self._in_format = True
|
||||||
printers = map(self._printer_for, attr.split(_sep2))
|
printers = map(self._printer_for, attr.split(_sep2))
|
||||||
def dec(string):
|
def dec(string, replace=False):
|
||||||
unique = str(uuid.uuid4())
|
unique = str(uuid.uuid4())
|
||||||
string = string.replace(r'\|', unique)
|
string = string.replace(r'\|', unique)
|
||||||
parts = string.split("|")
|
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):
|
for part, output in zip(parts, printers):
|
||||||
output(part.replace(unique, "|"))
|
output(part.replace(unique, "|"))
|
||||||
|
|
||||||
|
if self._breakline:
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
|
self._in_format = False
|
||||||
|
|
||||||
return dec
|
return dec
|
||||||
|
|
||||||
return self._printer_for(attr)
|
return self._printer_for(attr)
|
||||||
|
|
|
@ -126,3 +126,32 @@ def test_update_shell():
|
||||||
sh.indent()
|
sh.indent()
|
||||||
sh.print_red("Red", True)
|
sh.print_red("Red", True)
|
||||||
assert_stdout('\033[33mYellow\033[0m\r\033[A \033[31mRed\033[0m')
|
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')
|
||||||
|
|
Loading…
Reference in New Issue