#!/usr/bin/env python
### COPYRIGHT NOTICE:
### This file was taken from Gentoo's portage package
### Original copyright notice:
#------------------------------------------------------------------------------
# Copyright 1998-2003 Daniel Robbins, Gentoo Technologies, Inc.
# Distributed under the GNU Public License v2
# $Header: /home/cvsroot/gentoo-src/portage/pym/output.py,v 1.17 2003/12/08 15:11:28 nakano Exp $
#------------------------------------------------------------------------------
import os
import sys
import re
from config_locale import toprint
havecolor=1
dotitles=1
codes={}
codes["reset"]="\x1b[0m"
codes["bold"]="\x1b[01m"
codes["teal"]="\x1b[36;06m"
codes["turquoise"]="\x1b[36;01m"
codes["fuscia"]="\x1b[35;01m"
codes["purple"]="\x1b[35;06m"
codes["blue"]="\x1b[34;01m"
codes["darkblue"]="\x1b[34;06m"
codes["green"]="\x1b[32;01m"
codes["darkgreen"]="\x1b[32;06m"
codes["yellow"]="\x1b[33;01m"
codes["brown"]="\x1b[33;06m"
codes["red"]="\x1b[31;01m"
codes["darkred"]="\x1b[31;06m"
def xtermTitle(mystr):
if havecolor and dotitles and os.environ.has_key("TERM"):
myt=os.environ["TERM"]
if myt in ["xterm","Eterm","aterm","rxvt","screen"]:
sys.stderr.write("\x1b]1;\x07\x1b]2;"+
toprint( mystr ) +"\x07")
sys.stderr.flush()
def xtermTitleReset():
if havecolor and dotitles and os.environ.has_key("TERM"):
myt=os.environ["TERM"]
xtermTitle(os.environ["TERM"])
def notitles():
"turn off title setting"
dotitles=0
def nocolor():
"turn off colorization"
havecolor=0
for x in codes.keys():
codes[x]=""
def resetColor():
return codes["reset"]
def ctext(color,text):
return codes[ctext]+text+codes["reset"]
def bold(text):
return codes["bold"]+text+codes["reset"]
def white(text):
return bold(text)
def teal(text):
return codes["teal"]+text+codes["reset"]
def turquoise(text):
return codes["turquoise"]+text+codes["reset"]
def darkteal(text):
return turquoise(text)
def fuscia(text):
return codes["fuscia"]+text+codes["reset"]
def purple(text):
return codes["purple"]+text+codes["reset"]
def blue(text):
return codes["blue"]+text+codes["reset"]
def darkblue(text):
return codes["darkblue"]+text+codes["reset"]
def green(text):
return codes["green"]+text+codes["reset"]
def darkgreen(text):
return codes["darkgreen"]+text+codes["reset"]
def yellow(text):
return codes["yellow"]+text+codes["reset"]
def brown(text):
return codes["brown"]+text+codes["reset"]
def darkyellow(text):
return brown(text)
def red(text):
return codes["red"]+text+codes["reset"]
def darkred(text):
return codes["darkred"]+text+codes["reset"]
def strip_escape(s): return re.sub('\x1b.*?m', '', s)
def tabulate(heading, table):
"""
>>> heading=[('a','A'), ('b', 'BB'), ('c','CCCCCCCCCCCCCCCC')]
>>> table=[
{'b':'there', 'a':'hello', 'c':'cat' },
{'b':'are', 'a':'howwwww', 'c':'dog' },
{'b':'this evening','a':'you', 'c':'fish' },
{'b':'chris', 'a':'mr', 'c':'giraffe'},
]
>>> print tabulate(heading, table)
A BB CCCCCCCCCCCCCCCC
-------------------------------------
hello there cat
howwwww are dog
you this evening fish
mr chris giraffe
"""
def get_max(x,y):
result={}
for k in x.keys():
s1=strip_escape(x[k])
s2=strip_escape(y[k])
if len(s1) > len(s2):
result[k]=s1
else:
result[k]=s2
return result
# get_max()
max_lengths=dict(map(lambda x: (x[0], len(x[1])), reduce(get_max, table, dict(heading)).items()))
result=''
for k,v in heading:
v=bold(v)
n=max_lengths[k]+len(v)-len(strip_escape(v))
fmt='%%-%ss ' % n
result+=fmt % v
result=result[:-1]+'\n'
result+='%s\n' % ('-'*reduce(lambda x,y: x+y, max_lengths.values(), len(max_lengths.values())-1))
for tuple in table:
for k,v in heading:
n=max_lengths[k]+len(tuple[k])-len(strip_escape(tuple[k]))
fmt='%%-%ss ' % n
result+=fmt % tuple[k]
result=result[:-1]+'\n'
return toprint(result)
# tabulate()
#heading=[('a','A'), ('b', '\x1b[32;01mBB\x1b[0m'), ('c','CCCCCCCCCCCCCCCC')]
#table=[
# {'b':'there', 'a':'hello', 'c':'cat' },
# {'b':'\x1b[34;01mare\x1b[0m', 'a':'howwwww','c':'dog' },
# {'b':'this evening' , 'a':'you', 'c':'fish' },
# {'b':'chris', 'a':'mr', 'c':'giraffe'},
# ]
#print tabulate(heading, table)
|