starting the templating support
This commit is contained in:
parent
54ed7df96e
commit
cead6007e9
|
@ -109,5 +109,5 @@ And run:
|
|||
## Licensing
|
||||
|
||||
Copyright (c) 2010 Gabriel Falcão
|
||||
Licensed under LGPL 3+
|
||||
http://www.gnu.org/copyleft/gpl.html
|
||||
Licensed under Apache License 2.0
|
||||
http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
|
38
couleur.py
38
couleur.py
|
@ -17,8 +17,44 @@
|
|||
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
__version__ = '0.2'
|
||||
from StringIO import StringIO
|
||||
|
||||
class ColorWriter(StringIO):
|
||||
def __init__(self, original):
|
||||
self.original = original
|
||||
self.old_write = original.write
|
||||
|
||||
def translate_colors(self, string):
|
||||
for attr in filter(lambda x: not x.startswith("_"), dir(forecolors)):
|
||||
string = string.replace(
|
||||
"#{%s}" % attr,
|
||||
getattr(forecolors, attr)
|
||||
)
|
||||
string = string.replace(
|
||||
"#{on:%s}" % attr,
|
||||
getattr(backcolors, attr)
|
||||
)
|
||||
|
||||
string = string.replace("\n", "%s\n" % modifiers.reset)
|
||||
return string
|
||||
|
||||
def write(self, what):
|
||||
self.old_write(self.translate_colors(what))
|
||||
|
||||
class Proxy(object):
|
||||
def __init__(self, output):
|
||||
self.output = output
|
||||
self.writer = ColorWriter(output)
|
||||
|
||||
def enable(self):
|
||||
self.output.write = self.writer.write
|
||||
|
||||
def disable(self):
|
||||
self.output.write = self.writer.old_write
|
||||
|
||||
def proxy(output):
|
||||
return Proxy(output)
|
||||
|
||||
def ansify(number):
|
||||
"""Wraps the given ansi code to a proper escaped python output
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# <Couleur - fancy shell output for python>
|
||||
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
import sys
|
||||
from StringIO import StringIO
|
||||
from nose.tools import with_setup, assert_equals
|
||||
|
||||
import couleur
|
||||
|
||||
def prepare_stdout():
|
||||
if isinstance(sys.stdout, StringIO):
|
||||
del sys.stdout
|
||||
|
||||
std = StringIO()
|
||||
sys.stdout = std
|
||||
|
||||
def assert_stdout(expected):
|
||||
string = sys.stdout.getvalue()
|
||||
assert_equals(string, expected)
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_black_foreground():
|
||||
"Test stdout filter output: black foreground"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{black}Hello Black!"
|
||||
assert_stdout('\033[30mHello Black!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_black_on_white_foreground():
|
||||
"Test stdout filter output: black foreground on white background"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{black}#{on:white}Hello Black!"
|
||||
assert_stdout('\033[30m\033[47mHello Black!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_green_foreground():
|
||||
"Test stdout filter output: green foreground"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{green}Hello Green!"
|
||||
assert_stdout('\033[32mHello Green!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_green_and_red_on_white_foreground():
|
||||
"Test stdout filter output: green foreground and white on red background"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{green}Hello #{white}#{on:red}Italy!"
|
||||
assert_stdout('\033[32mHello \033[37m\033[41mItaly!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
Loading…
Reference in New Issue