tableau_ng, gre tout seul le formatage pour les ko, Mo, Go d'upload,
l'allignement des colonnes, la largeur automatique et les titres la syntaxe est un peu diffrente de celle d'avant mais on retrouve le meme principe darcs-hash:20051103144059-4ec08-0665ab0f8a7e35462942dc4a9826837f570f2485.gz
This commit is contained in:
parent
8c5389cd59
commit
ba62565f57
1 changed files with 92 additions and 0 deletions
|
@ -129,6 +129,98 @@ def tableau(largeurs,data) :
|
|||
f = f.replace(u'\n',s,1) # Insertion du séparateur entète - corps
|
||||
return f[:-1] # Supression du \n final
|
||||
|
||||
def tableau_ng(data,titres=None,largeurs=None,allignements=None,formats=None) :
|
||||
"""
|
||||
retourne une chaine formatée repésentant un tableau
|
||||
|
||||
data : liste de listes, chacune contenant les valeurs d'une ligne
|
||||
|
||||
titres : liste des titres
|
||||
Si none, n'affiche pas de ligne de titre
|
||||
|
||||
largeurs : liste des largeurs des colonnes, '*' met la plus grande
|
||||
largeur possible.
|
||||
Si None, réduit aux max chaque colonne
|
||||
|
||||
allignements: liste des alignements : c = centrer
|
||||
g = gauche
|
||||
d = droit
|
||||
Si None, met c pour chaque colonne
|
||||
|
||||
formats : liste des formats : s = string
|
||||
o = octet
|
||||
Si None, s pour chaque colonne
|
||||
"""
|
||||
sep_col = u'|'
|
||||
nbcols = len(data[0])
|
||||
|
||||
# Formats
|
||||
#########
|
||||
if not formats :
|
||||
formats = ['s'] * nbcols
|
||||
|
||||
def reformate (data, format) :
|
||||
if format == 's' :
|
||||
return str(data)
|
||||
|
||||
elif format == 'o' :
|
||||
if data > 0.8 * 1024**3 :
|
||||
return str(round(data/1024**3,1))+'Go'
|
||||
elif data > 0.8 * 1024**2 :
|
||||
return str(round(data/1024**2,1))+'Mo'
|
||||
elif data > 0.8 * 1024 :
|
||||
return str(round(data/1024,1))+'ko'
|
||||
else :
|
||||
return str(round(data,1))+'o'
|
||||
|
||||
data = [ [ reformate(ligne[i],formats[i]) for i in range(nbcols) ] for ligne in data ]
|
||||
|
||||
# Largeurs
|
||||
##########
|
||||
if not largeurs :
|
||||
largeurs = [ max([len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',ligne[i])) for ligne in data]) for i in range(nbcols) ]
|
||||
elif '*' in largeurs:
|
||||
rows, cols = get_screen_size()
|
||||
for i in range(nbcols) :
|
||||
if largeurs[i] in ['*',-1] :
|
||||
largeurs[i] = max(cols - sum([l for l in largeurs if l != '*']) - nbcols - 1, 3)
|
||||
break
|
||||
print largeurs
|
||||
|
||||
# Allignement
|
||||
#############
|
||||
if not allignements :
|
||||
allignements = ['c'] * nbcols
|
||||
|
||||
def alligne (data, allignement, largeur) :
|
||||
# Longeur sans les chaines de formatage
|
||||
l = largeur - len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',data))
|
||||
# Allignement
|
||||
if allignement == 'g' :
|
||||
return data + u' '*l
|
||||
elif allignement == 'd' :
|
||||
return u' '*l + data
|
||||
else :
|
||||
return u' '*(l/2) + data + u' '*((l+1)/2)
|
||||
|
||||
data = [ [ alligne(ligne[i],allignements[i],largeurs[i]) for i in range(nbcols) ] for ligne in data ]
|
||||
|
||||
# Le titre
|
||||
##########
|
||||
if titres :
|
||||
# ligne de titre
|
||||
chaine = sep_col + sep_col.join([alligne(titres[i],'c',largeurs[i]) for i in range(nbcols)]) + sep_col + u'\n'
|
||||
# ligne de séparation
|
||||
chaine += sep_col + u'+'.join([u'-'*largeurs[i] for i in range(nbcols)]) + sep_col + u'\n'
|
||||
else :
|
||||
chaine = u''
|
||||
|
||||
# Les données
|
||||
#############
|
||||
chaine += u'\n'.join([sep_col + sep_col.join([ligne[i] for i in range(nbcols)]) + sep_col for ligne in data])
|
||||
|
||||
return chaine
|
||||
|
||||
def get_screen_size():
|
||||
"""Retourne la taille de l'écran.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue