[affichage] Ajout d'une fonction de style dédiée pour dialog
This commit is contained in:
parent
9085bc35e8
commit
b26e329e83
1 changed files with 70 additions and 7 deletions
|
@ -1,5 +1,36 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Vrac d'outils pour avoir un affichage sympathique.
|
||||||
|
# Auteur : Pierre-Elliott Bécue <becue@crans.org>
|
||||||
|
# Licence : GPLv3
|
||||||
|
#
|
||||||
|
# Contenu :
|
||||||
|
# ---------
|
||||||
|
#
|
||||||
|
# Décorateur :
|
||||||
|
# static_var(name, val), un décorateur pour créer une variable
|
||||||
|
# statique dans une fonction
|
||||||
|
#
|
||||||
|
# Fonctions :
|
||||||
|
# getTerminalSize(), une fonction qui récupère le couple
|
||||||
|
# largeur, hauteur du terminal courant.
|
||||||
|
# cap_text(text, length), un raccourci pour couper un texte
|
||||||
|
# trop long en mettant …
|
||||||
|
# format_percent(percent), un raccourci qui fait en sorte
|
||||||
|
# qu'un pourcentage prenne toujours autant de caractères
|
||||||
|
# en ajoutant des espaces (c'est pour les prints dynamiques)
|
||||||
|
# rojv(percent, seuils, couls), une méthode qui affiche une couleur
|
||||||
|
# pour un pourcentage en fonction de la valeur de celui-ci, des
|
||||||
|
# seuils définis, et des couleurs disponibles.
|
||||||
|
# style(texte, what), une méthode qui applique un ensemble de styles
|
||||||
|
# contenus dans what au texte texte.
|
||||||
|
# dialogStyle(texte, what), pareil, mais dans dialog
|
||||||
|
# prettyDoin(what, status), une méthode qui affiche des messages "service"-
|
||||||
|
# like, en mettant le status en fonction de l'accomplissement de la tâche.
|
||||||
|
#
|
||||||
|
# Classes :
|
||||||
|
# Animation, une classe générant les animations type "progress bar".
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
@ -7,6 +38,7 @@ import fcntl
|
||||||
import termios
|
import termios
|
||||||
import struct
|
import struct
|
||||||
import functools
|
import functools
|
||||||
|
import time
|
||||||
|
|
||||||
def static_var(name, val):
|
def static_var(name, val):
|
||||||
"""Decorator setting static variable
|
"""Decorator setting static variable
|
||||||
|
@ -53,10 +85,10 @@ def getTerminalSize():
|
||||||
return int(cr[1]), int(cr[0])
|
return int(cr[1]), int(cr[0])
|
||||||
|
|
||||||
def cap_text(text, length):
|
def cap_text(text, length):
|
||||||
"""Cap text length to length
|
"""Cap text length to length-1
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if len(text) > length:
|
if len(text) >= length:
|
||||||
return text[0:length-1] + "…"
|
return text[0:length-1] + "…"
|
||||||
else:
|
else:
|
||||||
return text
|
return text
|
||||||
|
@ -105,7 +137,7 @@ class Animation(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, texte, nb_cycles=0, couleur=True, kikoo=True):
|
def __init__(self, texte, nb_cycles=0, couleur=True, kikoo=True, timer=True):
|
||||||
"""__init__
|
"""__init__
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -114,6 +146,8 @@ class Animation(object):
|
||||||
self.kikoo = kikoo
|
self.kikoo = kikoo
|
||||||
self.step = 0
|
self.step = 0
|
||||||
self.couleur = couleur
|
self.couleur = couleur
|
||||||
|
self.timer = timer
|
||||||
|
self.beginTime = 0
|
||||||
|
|
||||||
def kikoo(self, kikoo):
|
def kikoo(self, kikoo):
|
||||||
"""Switch pour la valeur kikoo"""
|
"""Switch pour la valeur kikoo"""
|
||||||
|
@ -123,6 +157,8 @@ class Animation(object):
|
||||||
"""Effectue une étape dans l'affichage
|
"""Effectue une étape dans l'affichage
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if self.step == 0 and self.timer:
|
||||||
|
self.beginTime = time.time()
|
||||||
cols, _ = getTerminalSize()
|
cols, _ = getTerminalSize()
|
||||||
|
|
||||||
if not self.nb_cycles > 0:
|
if not self.nb_cycles > 0:
|
||||||
|
@ -136,8 +172,8 @@ class Animation(object):
|
||||||
percent = "%s %%" % (format_percent(percent),)
|
percent = "%s %%" % (format_percent(percent),)
|
||||||
sys.stdout.write("\r%s : %s" % (cap_text(self.texte, cols - 10), percent))
|
sys.stdout.write("\r%s : %s" % (cap_text(self.texte, cols - 10), percent))
|
||||||
else:
|
else:
|
||||||
# La kikoo bar est une barre de la forme [###### ]
|
# La kikoo bar est une barre de la forme [======> ]
|
||||||
# Nombre de #
|
# Nombre de =
|
||||||
amount = percent/4
|
amount = percent/4
|
||||||
# On remplit le reste avec des espaces.
|
# On remplit le reste avec des espaces.
|
||||||
if self.couleur:
|
if self.couleur:
|
||||||
|
@ -155,6 +191,10 @@ class Animation(object):
|
||||||
format_percent(percent))
|
format_percent(percent))
|
||||||
sys.stdout.write("\r%s %s" % (cap_text(self.texte, cols - 45),
|
sys.stdout.write("\r%s %s" % (cap_text(self.texte, cols - 45),
|
||||||
kikoo_print))
|
kikoo_print))
|
||||||
|
|
||||||
|
if self.timer:
|
||||||
|
self.elapsedTime = time.time() - self.beginTime
|
||||||
|
sys.stdout.write(" (temps écoulé : %ds)" % (self.elapsedTime))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
self.step += 1
|
self.step += 1
|
||||||
|
|
||||||
|
@ -190,8 +230,6 @@ def style(texte, what=[]):
|
||||||
"""
|
"""
|
||||||
if isinstance(what, str):
|
if isinstance(what, str):
|
||||||
what = [what]
|
what = [what]
|
||||||
# Pour l'éventuelle coloration du fond.
|
|
||||||
add = 0
|
|
||||||
|
|
||||||
if not what:
|
if not what:
|
||||||
return texte
|
return texte
|
||||||
|
@ -235,6 +273,31 @@ OK = style('Ok', 'vert')
|
||||||
WARNING = style('Warning', 'jaune')
|
WARNING = style('Warning', 'jaune')
|
||||||
ERREUR = style('Erreur', 'rouge')
|
ERREUR = style('Erreur', 'rouge')
|
||||||
|
|
||||||
|
def dialogStyle(texte, what):
|
||||||
|
"""Fournit du texte coloré pour dialog.
|
||||||
|
|
||||||
|
Séparé volontairement de style pour une meilleure
|
||||||
|
utilisation.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
if isinstance(what, str):
|
||||||
|
what = [what]
|
||||||
|
|
||||||
|
dialog_styles = {
|
||||||
|
'rouge': 1,
|
||||||
|
'vert': 2,
|
||||||
|
'jaune': 3,
|
||||||
|
'bleu': 4,
|
||||||
|
'violet': 5,
|
||||||
|
'cyan': 6,
|
||||||
|
'gris': 0,
|
||||||
|
'gras': 'b' }
|
||||||
|
|
||||||
|
for elem in what:
|
||||||
|
texte = "\Z%s%s\Zn" % (dialog_styles[elem], texte)
|
||||||
|
return texte
|
||||||
|
|
||||||
def prettyDoin(what, status):
|
def prettyDoin(what, status):
|
||||||
"""Affiche une opération en cours et met son statut à jour
|
"""Affiche une opération en cours et met son statut à jour
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue