]> gitweb.pimeys.fr Git - scripts-20-100.git/blob - create_color.py
typo
[scripts-20-100.git] / create_color.py
1 #!/usr/bin/python
2 # -*- coding:utf8 -*-
3
4 """ Petit script pour générer une chaîne de caractère magique
5 qui permet de changer la couleur d'affichage du texte dans le shell
6 """
7
8 # Les paramètres sont les suivants :
9 # 1 : le type de sortie prompt ou pas
10 # si c'est prompt, la sortie est enadrée de \[ et \], sinon non
11 # 2 : fg ou bg ou both
12 # 3 : la couleur (black, red, green, yellow, blue, purple, cyan, white)
13 # 4 et suivants : des paramètres
14 # liste exhaustive des combinaisons possibles :
15 # []
16 # bold
17 # underline
18 # high
19 # bold underline
20 # bold underline high #(a prori ça garde que bold)
21 # underline high
22 # avec ground = both
23 # [fg_params] , bg_color [bg_params]
24
25
26 import sys
27 args=sys.argv
28
29
30
31 dicocolor={"black":"0","Black":"0","noir":"0","Noir":"0","BLACK":"0","NOIR":"0",
32 "red":"1","Red":"1","rouge":"1","Rouge":"1","RED":"1","ROUGE":"1",
33 "green":"2","Green":"2","vert":"2","Vert":"2","GREEN":"2","VERT":"2",
34 "yellow":"3","Yellow":"3","jaune":"3","Jaune":"3","YELLOW":"3","JAUNE":"3",
35 "blue":"4","Blue":"4","bleu":"4","Bleu":"4","BLUE":"4","BLEU":"4",
36 "purple":"5","Purple":"5","violet":"5","Violet":"5","PURPLE":"5","VIOLET":"5",
37 "cyan":"6","Cyan":"6","CYAN":"6",
38 "white":"7","White":"7","blanc":"7","Blanc":"7","WHITE":"7","BLANC":"7"}
39
40 def isBold(liste):
41 liste_bold=set(["bold","Bold","BOLD","gras","Gras","GRAS"])
42 return not(liste_bold.isdisjoint(set(liste)))
43
44 def isUnderline(liste):
45 liste_underline=set(["underline","Underline","UNDERLINE","souligne","Souligne","SOULIGNE"])
46 return not(liste_underline.isdisjoint(set(liste)))
47
48 def isHigh(liste):
49 liste_high=set(["high","High","HIGH","high_intensity","High_Intensity","HIGH_INTENSITY"])
50 return not(liste_high.isdisjoint(set(liste)))
51
52 def get_fg_color(color,bold,underline,high):
53 prefix=("4;"*underline
54 +"1;"*bold)
55 highoupas="9"*high+"3"*(not(high))
56 return prefix+highoupas+dicocolor[color]
57
58 def get_bg_color(color,high):
59 highoupas="4"*high+"10"*(not(high))
60 return highoupas+dicocolor[color]
61
62 # Let's go
63 out,ground,color=args[1],args[2],args[3]
64 params=args[4:] # éventuellement vide
65
66 if out=="prompt":
67 deb,fin="\\[\\e[","m\\]"
68 else:
69 deb,fin="\\e[","m"
70
71
72
73 if ground=="fg":
74 bold = isBold(params)
75 underline = isUnderline(params)
76 high = isHigh(params)
77 print deb+get_fg_color(color,bold,underline,high)+fin
78 elif ground=="bg":
79 high = isHigh(params)
80 print deb+get_bg_color(color,high)+fin
81 elif ground=="both":
82 # On cherche le séparateur
83 virgule=params.index(",")
84 fg_params=params[:virgule]
85 bg_color=params[virgule+1]
86 bg_params=params[virgule+2:]
87 out = deb
88 out += get_fg_color(color,isBold(fg_params),isUnderline(fg_params),isHigh(fg_params))
89 out += fin+deb
90 out += get_bg_color(bg_color,isHigh(bg_params))
91 out += fin
92 print out
93 else:
94 print ""