| 1 |
# Copyright 1998-2004 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
# $Header: /var/cvsroot/gentoo-src/portage/pym/output.py,v 1.24 2004/10/04 14:07:40 vapier Exp $
|
| 4 |
cvs_id_string="$Id: output.py,v 1.24.2.3 2005/02/06 02:48:44 jstubbs Exp $"[5:-2]
|
| 5 |
|
| 6 |
import os,sys,re
|
| 7 |
|
| 8 |
havecolor=1
|
| 9 |
dotitles=1
|
| 10 |
|
| 11 |
esc_seq = "\x1b["
|
| 12 |
|
| 13 |
g_attr = {}
|
| 14 |
g_attr["normal"] = 0
|
| 15 |
|
| 16 |
g_attr["bold"] = 1
|
| 17 |
g_attr["faint"] = 2
|
| 18 |
g_attr["standout"] = 3
|
| 19 |
g_attr["underline"] = 4
|
| 20 |
g_attr["blink"] = 5
|
| 21 |
g_attr["overline"] = 6 # Why is overline actually useful?
|
| 22 |
g_attr["reverse"] = 7
|
| 23 |
g_attr["invisible"] = 8
|
| 24 |
|
| 25 |
g_attr["no-attr"] = 22
|
| 26 |
g_attr["no-standout"] = 23
|
| 27 |
g_attr["no-underline"] = 24
|
| 28 |
g_attr["no-blink"] = 25
|
| 29 |
g_attr["no-overline"] = 26
|
| 30 |
g_attr["no-reverse"] = 27
|
| 31 |
# 28 isn't defined?
|
| 32 |
# 29 isn't defined?
|
| 33 |
g_attr["black"] = 30
|
| 34 |
g_attr["red"] = 31
|
| 35 |
g_attr["green"] = 32
|
| 36 |
g_attr["yellow"] = 33
|
| 37 |
g_attr["blue"] = 34
|
| 38 |
g_attr["magenta"] = 35
|
| 39 |
g_attr["cyan"] = 36
|
| 40 |
g_attr["white"] = 37
|
| 41 |
# 38 isn't defined?
|
| 42 |
g_attr["default"] = 39
|
| 43 |
g_attr["bg_black"] = 40
|
| 44 |
g_attr["bg_red"] = 41
|
| 45 |
g_attr["bg_green"] = 42
|
| 46 |
g_attr["bg_yellow"] = 43
|
| 47 |
g_attr["bg_blue"] = 44
|
| 48 |
g_attr["bg_magenta"] = 45
|
| 49 |
g_attr["bg_cyan"] = 46
|
| 50 |
g_attr["bg_white"] = 47
|
| 51 |
g_attr["bg_default"] = 49
|
| 52 |
|
| 53 |
|
| 54 |
# make_seq("blue", "black", "normal")
|
| 55 |
def color(fg, bg="default", attr=["normal"]):
|
| 56 |
mystr = esc_seq[:] + "%02d" % g_attr[fg]
|
| 57 |
for x in [bg]+attr:
|
| 58 |
mystr += ";%02d" % g_attr[x]
|
| 59 |
return mystr+"m"
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
codes={}
|
| 64 |
codes["reset"] = esc_seq + "39;49;00m"
|
| 65 |
|
| 66 |
codes["bold"] = esc_seq + "01m"
|
| 67 |
codes["faint"] = esc_seq + "02m"
|
| 68 |
codes["standout"] = esc_seq + "03m"
|
| 69 |
codes["underline"] = esc_seq + "04m"
|
| 70 |
codes["blink"] = esc_seq + "05m"
|
| 71 |
codes["overline"] = esc_seq + "06m" # Who made this up? Seriously.
|
| 72 |
|
| 73 |
codes["teal"] = esc_seq + "36m"
|
| 74 |
codes["turquoise"] = esc_seq + "36;01m"
|
| 75 |
|
| 76 |
codes["fuchsia"] = esc_seq + "35;01m"
|
| 77 |
codes["purple"] = esc_seq + "35m"
|
| 78 |
|
| 79 |
codes["blue"] = esc_seq + "34;01m"
|
| 80 |
codes["darkblue"] = esc_seq + "34m"
|
| 81 |
|
| 82 |
codes["green"] = esc_seq + "32;01m"
|
| 83 |
codes["darkgreen"] = esc_seq + "32m"
|
| 84 |
|
| 85 |
codes["yellow"] = esc_seq + "33;01m"
|
| 86 |
codes["brown"] = esc_seq + "33m"
|
| 87 |
|
| 88 |
codes["red"] = esc_seq + "31;01m"
|
| 89 |
codes["darkred"] = esc_seq + "31m"
|
| 90 |
|
| 91 |
def nc_len(mystr):
|
| 92 |
tmp = re.sub(esc_seq + "^m]+m", "", mystr);
|
| 93 |
return len(tmp)
|
| 94 |
|
| 95 |
def xtermTitle(mystr):
|
| 96 |
if havecolor and dotitles and os.environ.has_key("TERM") and sys.stderr.isatty():
|
| 97 |
myt=os.environ["TERM"]
|
| 98 |
legal_terms = ["xterm","Eterm","aterm","rxvt","screen","kterm","rxvt-unicode"]
|
| 99 |
if (myt in legal_terms) or myt.startswith("xterm") or myt.startswith("screen"):
|
| 100 |
sys.stderr.write("\x1b]2;"+str(mystr)+"\x07")
|
| 101 |
sys.stderr.flush()
|
| 102 |
if (myt.startswith("screen")):
|
| 103 |
sys.stderr.write("\x1bk"+str(mystr)+"\x1b\\")
|
| 104 |
sys.stderr.flush()
|
| 105 |
|
| 106 |
def xtermTitleReset():
|
| 107 |
if havecolor and dotitles and os.environ.has_key("TERM"):
|
| 108 |
myt=os.environ["TERM"]
|
| 109 |
xtermTitle(os.environ["TERM"])
|
| 110 |
|
| 111 |
|
| 112 |
def notitles():
|
| 113 |
"turn off title setting"
|
| 114 |
dotitles=0
|
| 115 |
|
| 116 |
def nocolor():
|
| 117 |
"turn off colorization"
|
| 118 |
havecolor=0
|
| 119 |
for x in codes.keys():
|
| 120 |
codes[x]=""
|
| 121 |
|
| 122 |
def resetColor():
|
| 123 |
return codes["reset"]
|
| 124 |
|
| 125 |
def ctext(color,text):
|
| 126 |
return codes[ctext]+text+codes["reset"]
|
| 127 |
|
| 128 |
def bold(text):
|
| 129 |
return codes["bold"]+text+codes["reset"]
|
| 130 |
def white(text):
|
| 131 |
return bold(text)
|
| 132 |
|
| 133 |
def teal(text):
|
| 134 |
return codes["teal"]+text+codes["reset"]
|
| 135 |
def turquoise(text):
|
| 136 |
return codes["turquoise"]+text+codes["reset"]
|
| 137 |
def darkteal(text):
|
| 138 |
return turquoise(text)
|
| 139 |
|
| 140 |
def fuscia(text): # Don't use this one. It's spelled wrong!
|
| 141 |
return codes["fuchsia"]+text+codes["reset"]
|
| 142 |
def fuchsia(text):
|
| 143 |
return codes["fuchsia"]+text+codes["reset"]
|
| 144 |
def purple(text):
|
| 145 |
return codes["purple"]+text+codes["reset"]
|
| 146 |
|
| 147 |
def blue(text):
|
| 148 |
return codes["blue"]+text+codes["reset"]
|
| 149 |
def darkblue(text):
|
| 150 |
return codes["darkblue"]+text+codes["reset"]
|
| 151 |
|
| 152 |
def green(text):
|
| 153 |
return codes["green"]+text+codes["reset"]
|
| 154 |
def darkgreen(text):
|
| 155 |
return codes["darkgreen"]+text+codes["reset"]
|
| 156 |
|
| 157 |
def yellow(text):
|
| 158 |
return codes["yellow"]+text+codes["reset"]
|
| 159 |
def brown(text):
|
| 160 |
return codes["brown"]+text+codes["reset"]
|
| 161 |
def darkyellow(text):
|
| 162 |
return brown(text)
|
| 163 |
|
| 164 |
def red(text):
|
| 165 |
return codes["red"]+text+codes["reset"]
|
| 166 |
def darkred(text):
|
| 167 |
return codes["darkred"]+text+codes["reset"]
|