OSDN Git Service

[update] : Changed out_dir -> bootfiles_dir
[alterlinux/LUBS.git] / echo_color
1 #!/usr/bin/env python3
2 #== Import ==#
3 import sys
4
5 #== Function ==#
6 def usage():
7     print(
8         "usage: echo_color -b <backcolor> -t <textcolor> -d <decoration> [Text]\n"
9         "Text Color\n"
10         "30 => Black\n"
11         "31 => Red\n"
12         "32 => Green\n"
13         "33 => Yellow\n"
14         "34 => Blue\n"
15         "35 => Magenta\n"
16         "36 => Cyan\n"
17         "37 => White\n"
18         "\n"
19         "Background color\n"
20         "40 => Black\n"
21         "41 => Red\n"
22         "42 => Green\n"
23         "43 => Yellow\n"
24         "44 => Blue\n"
25         "45 => Magenta\n"
26         "46 => Cyan\n"
27         "47 => White\n"
28         "\n"
29         "Text decoration\n"
30         "0 => All attributs off (ノーマル)\n"
31         "1 => Bold on (太字)\n"
32         "4 => Underscore (下線)\n"
33         "5 => Blink on (点滅)\n"
34         "7 => Reverse video on (色反転)\n"
35         "8 => Concealed on\n"
36         "\n"
37         "Special Word\n"
38         "/# = Speace\n"
39         "/! = Speace*2\n"
40         "/^ = Tab Speace"
41     )
42
43     sys.exit()
44
45 #== Define ==#
46 blk = False
47 t = False
48 b = False
49 d = False
50 dics = {}
51 lists = ""
52
53 #== Code ==#
54 for i in sys.argv[1:]:
55     def set_types(name):
56         try:
57             dic[name] = str(int(i))
58             dic["args"] += 1
59             mode = False
60             return mode
61         except:
62             print("Error: The argument of " + name +" is a number.")
63             sys.exit()
64
65     if "-" in i or blk is True:
66         if blk is False:
67             blk = True
68             dic = {}
69             dic["args"] = 0
70
71         if i == "-t":
72             t = True
73         elif i == "-b":
74             b = True
75         elif i == "-d":
76             d = True
77         elif i == "-h":
78             usage()
79         else:
80             if t is True:
81                 t = set_types("t")
82             elif b is True:
83                 b = set_types("b")
84             elif d is True:
85                 d = set_types("d")
86             else:
87                 dic["txt"] = i
88                 
89                 try:
90                     dics[list(dics.keys())[-1]+1] = dic
91                 except:
92                     dics[0] = dic
93                 
94                 blk = False
95     else:
96         dic = {}
97         dic["txt"] = i
98                 
99         try:
100             dics[list(dics.keys())[-1]+1] = dic
101         except:
102             dics[0] = dic
103
104 for i in dics:
105     dic = dics[i]
106     try:
107         if dic["args"] > 0:
108             option = "\033["
109             for name in "t", "b", "d":
110                 if name in dic:
111                     if dic["args"] > 1:
112                         option += dic[name] + ";"
113                         dic["args"] -= 1
114                     else:
115                         option += dic[name] + "m"
116             lists += option + dic["txt"] + "\033[00m"
117     except:
118         lists += "\033[00m" + dic["txt"].replace("/^", "/!/!").replace("/!", "/#/#").replace("/#", " ")
119
120 print(lists)