diff --git a/pisi/cli/__init__.py b/pisi/cli/__init__.py index f2b69f5f..9b35e399 100644 --- a/pisi/cli/__init__.py +++ b/pisi/cli/__init__.py @@ -22,7 +22,6 @@ import pisi import pisi.context as ctx import pisi.ui import pisi.util -import pisi.cli.colors as colors class Error(pisi.Error): pass @@ -76,7 +75,7 @@ class CLI(pisi.ui.UI): if ctx.get_option('no_color'): self.output(_('Warning: ') + msg + '\n', err=True, verbose=verbose) else: - self.output(colors.colorize(msg + '\n', 'brightred'), err=True, verbose=verbose) + self.output(pisi.util.colorize(msg + '\n', 'brightred'), err=True, verbose=verbose) def error(self, msg): msg = unicode(msg) @@ -85,21 +84,21 @@ class CLI(pisi.ui.UI): if ctx.get_option('no_color'): self.output(_('Error: ') + msg + '\n', err=True) else: - self.output(colors.colorize(msg + '\n', 'red'), err=True) + self.output(pisi.util.colorize(msg + '\n', 'red'), err=True) def action(self, msg, verbose = False): #TODO: this seems quite redundant? msg = unicode(msg) if ctx.log: ctx.log.info(msg) - self.output(colors.colorize(msg + '\n', 'green')) + self.output(pisi.util.colorize(msg + '\n', 'green')) def choose(self, msg, opts): print msg for i in range(0,len(opts)): print i + 1, opts(i) while True: - s = raw_input(msg + colors.colorize('1-%d' % len(opts), 'red')) + s = raw_input(msg + pisi.util.colorize('1-%d' % len(opts), 'red')) try: opt = int(s) if 1 <= opt and opt <= len(opts): @@ -115,7 +114,7 @@ class CLI(pisi.ui.UI): import re yesexpr = re.compile(locale.nl_langinfo(locale.YESEXPR)) - prompt = msg + colors.colorize(_(' (yes/no)'), 'red') + prompt = msg + pisi.util.colorize(_(' (yes/no)'), 'red') s = raw_input(prompt.encode('utf-8')) if yesexpr.search(s): return True @@ -136,12 +135,12 @@ class CLI(pisi.ui.UI): self.output("\r%s (%d%%)" % (info, percent)) if percent == 100: - self.output(colors.colorize(_(' [complete]\n'), 'gray')) + self.output(pisi.util.colorize(_(' [complete]\n'), 'gray')) def status(self, msg = None): if msg: msg = unicode(msg) - self.output(colors.colorize(msg + '\n', 'brightgreen')) + self.output(pisi.util.colorize(msg + '\n', 'brightgreen')) pisi.util.xterm_title(msg) def notify(self, event, **keywords): @@ -158,6 +157,6 @@ class CLI(pisi.ui.UI): else: msg = None if msg: - self.output(colors.colorize(msg + '\n', 'cyan')) + self.output(pisi.util.colorize(msg + '\n', 'cyan')) if ctx.log: ctx.log.info(msg) diff --git a/pisi/cli/colors.py b/pisi/cli/colors.py deleted file mode 100644 index e7d37171..00000000 --- a/pisi/cli/colors.py +++ /dev/null @@ -1,64 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2005 - 2007, TUBITAK/UEKAE -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free -# Software Foundation; either version 2 of the License, or (at your option) -# any later version. -# -# Please read the COPYING file. -# - -"""Colors module provides some color codes for console output.""" - -import pisi.context as ctx - -colors = {'black' : "\033[30m", - 'red' : "\033[31m", - 'green' : "\033[32m", - 'yellow' : "\033[33m", - 'blue' : "\033[34m", - 'purple' : "\033[35m", - 'cyan' : "\033[36m", - 'white' : "\033[37m", - 'brightblack' : "\033[01;30m", - 'brightred' : "\033[01;31m", - 'brightgreen' : "\033[01;32m", - 'brightyellow' : "\033[01;33m", - 'brightblue' : "\033[01;34m", - 'brightmagenta' : "\033[01;35m", - 'brightcyan' : "\033[01;36m", - 'brightwhite' : "\033[01;37m", - 'underlineblack' : "\033[04;30m", - 'underlinered' : "\033[04;31m", - 'underlinegreen' : "\033[04;32m", - 'underlineyellow' : "\033[04;33m", - 'underlineblue' : "\033[04;34m", - 'underlinemagenta' : "\033[04;35m", - 'underlinecyan' : "\033[04;36m", - 'underlinewhite' : "\033[04;37m", - 'blinkingblack' : "\033[05;30m", - 'blinkingred' : "\033[05;31m", - 'blinkinggreen' : "\033[05;32m", - 'blinkingyellow' : "\033[05;33m", - 'blinkingblue' : "\033[05;34m", - 'blinkingmagenta' : "\033[05;35m", - 'blinkingcyan' : "\033[05;36m", - 'blinkingwhite' : "\033[05;37m", - 'backgroundblack' : "\033[07;30m", - 'backgroundred' : "\033[07;31m", - 'backgroundgreen' : "\033[07;32m", - 'backgroundyellow' : "\033[07;33m", - 'backgroundblue' : "\033[07;34m", - 'backgroundmagenta' : "\033[07;35m", - 'backgroundcyan' : "\033[07;36m", - 'backgroundwhite' : "\033[07;37m", - 'default' : "\033[0m" } - -def colorize(msg, color): - """Colorize the given message for console output""" - if colors.has_key(color) and not ctx.get_option('no_color'): - return colors[color] + msg + colors['default'] - else: - return msg diff --git a/pisi/cli/commands.py b/pisi/cli/commands.py index e2254d79..96944d5b 100644 --- a/pisi/cli/commands.py +++ b/pisi/cli/commands.py @@ -22,7 +22,6 @@ import pisi.cli import pisi.context as ctx from pisi.uri import URI import pisi.util as util -from colors import colorize class Error(pisi.Error): pass @@ -1368,7 +1367,7 @@ all repositories. self.init(database = True, write = False) if not (ctx.get_option('no_color') or ctx.config.get_option('uninstalled')): - ctx.ui.info(colorize(_('Installed packages are shown in this color'), 'green')) + ctx.ui.info(util.colorize(_('Installed packages are shown in this color'), 'green')) if self.args: for arg in self.args: @@ -1398,7 +1397,7 @@ all repositories. if p in installed_list: if ctx.config.get_option('uninstalled'): continue - p = colorize(p, 'green') + p = util.colorize(p, 'green') p = p + ' ' * max(0, 15 - lenp) ctx.ui.info('%s - %s ' % (p, unicode(package.summary))) @@ -1436,7 +1435,7 @@ repositories. else: lenp = len(p) #if p in installed_list: - # p = colorize(p, 'cyan') + # p = util.colorize(p, 'cyan') p = p + ' ' * max(0, 15 - lenp) ctx.ui.info('%s - %s ' % (component.name, unicode(component.summary))) self.finalize() @@ -1476,7 +1475,7 @@ Gives a brief list of sources published in the repositories. else: lenp = len(p) #if p in installed_list: - # p = colorize(p, 'cyan') + # p = util.colorize(p, 'cyan') p = p + ' ' * max(0, 15 - lenp) ctx.ui.info('%s - %s' % (sf.source.name, unicode(sf.source.summary))) self.finalize() diff --git a/pisi/constants.py b/pisi/constants.py index d2be3af3..bea67123 100644 --- a/pisi/constants.py +++ b/pisi/constants.py @@ -102,6 +102,47 @@ class Constants: self.__c.executable = "executable" self.__c.data = "data" self.__c.localedata = "localedata" + self.__c.colors = {'black' : "\033[30m", + 'red' : "\033[31m", + 'green' : "\033[32m", + 'yellow' : "\033[33m", + 'blue' : "\033[34m", + 'purple' : "\033[35m", + 'cyan' : "\033[36m", + 'white' : "\033[37m", + 'brightblack' : "\033[01;30m", + 'brightred' : "\033[01;31m", + 'brightgreen' : "\033[01;32m", + 'brightyellow' : "\033[01;33m", + 'brightblue' : "\033[01;34m", + 'brightmagenta' : "\033[01;35m", + 'brightcyan' : "\033[01;36m", + 'brightwhite' : "\033[01;37m", + 'underlineblack' : "\033[04;30m", + 'underlinered' : "\033[04;31m", + 'underlinegreen' : "\033[04;32m", + 'underlineyellow' : "\033[04;33m", + 'underlineblue' : "\033[04;34m", + 'underlinemagenta' : "\033[04;35m", + 'underlinecyan' : "\033[04;36m", + 'underlinewhite' : "\033[04;37m", + 'blinkingblack' : "\033[05;30m", + 'blinkingred' : "\033[05;31m", + 'blinkinggreen' : "\033[05;32m", + 'blinkingyellow' : "\033[05;33m", + 'blinkingblue' : "\033[05;34m", + 'blinkingmagenta' : "\033[05;35m", + 'blinkingcyan' : "\033[05;36m", + 'blinkingwhite' : "\033[05;37m", + 'backgroundblack' : "\033[07;30m", + 'backgroundred' : "\033[07;31m", + 'backgroundgreen' : "\033[07;32m", + 'backgroundyellow' : "\033[07;33m", + 'backgroundblue' : "\033[07;34m", + 'backgroundmagenta' : "\033[07;35m", + 'backgroundcyan' : "\033[07;36m", + 'backgroundwhite' : "\033[07;37m", + 'default' : "\033[0m" } def __getattr__(self, attr): return getattr(self.__c, attr) diff --git a/pisi/util.py b/pisi/util.py index f06ef3d3..7702cc6c 100644 --- a/pisi/util.py +++ b/pisi/util.py @@ -682,3 +682,11 @@ def filter_latest_packages(package_paths): latest[name] = (root, name, version) return map(lambda x:"%s/%s-%s.pisi" % x, latest.values()) + +def colorize(msg, color): + """Colorize the given message for console output""" + if ctx.const.colors.has_key(color) and not ctx.get_option('no_color'): + return ctx.const.colors[color] + msg + ctx.const.colors['default'] + else: + return msg +