root/scapy/themes.py

Revision 950:800294eaa8c0, 10.0 kB (checked in by Phil <phil@secdev.org>, 2 weeks ago)

Fixed NoTheme? and factorized some code (ticket #143)

Line 
1 ## This file is part of Scapy
2 ## See http://www.secdev.org/projects/scapy for more informations
3 ## Copyright (C) Philippe Biondi <phil@secdev.org>
4 ## This program is published under a GPLv2 license
5
6 import config
7
8 ##################
9 ## Color themes ##
10 ##################
11
12 class Color:
13     normal = "\033[0m"
14     black = "\033[30m"
15     red = "\033[31m"
16     green = "\033[32m"
17     yellow = "\033[33m"
18     blue = "\033[34m"
19     purple = "\033[35m"
20     cyan = "\033[36m"
21     grey = "\033[37m"
22
23     bold = "\033[1m"
24     uline = "\033[4m"
25     blink = "\033[5m"
26     invert = "\033[7m"
27        
28
29 def create_styler(fmt=None, before="", after="", fmt2="%s"):
30     def do_style(val, fmt=fmt, before=before, after=after, fmt2=fmt2):
31         if fmt is None:
32             if type(val) is not str:
33                 val = str(val)
34         else:
35             val = fmt % val
36         return fmt2 % (before+val+after)
37     return do_style
38
39 class ColorTheme:
40     def __repr__(self):
41         return "<%s>" % self.__class__.__name__
42     def __getattr__(self, attr):
43         return create_styler()
44        
45
46 class NoTheme(ColorTheme):
47     pass
48
49
50 class AnsiColorTheme(ColorTheme):
51     def __getattr__(self, attr):
52         if attr.startswith("__"):
53             raise AttributeError(attr)
54         s = "style_%s" % attr
55         if s in self.__class__.__dict__:
56             before = getattr(self, s)
57             after = self.style_normal
58         else:
59             before = after = ""
60
61         return create_styler(before=before, after=after)
62        
63        
64     style_normal = ""
65     style_prompt = ""
66     style_punct = ""
67     style_id = ""
68     style_not_printable = ""
69     style_layer_name = ""
70     style_field_name = ""
71     style_field_value = ""
72     style_emph_field_name = ""
73     style_emph_field_value = ""
74     style_packetlist_name = ""
75     style_packetlist_proto = ""
76     style_packetlist_value = ""
77     style_fail = ""
78     style_success = ""
79     style_odd = ""
80     style_even = ""
81     style_opening = ""
82     style_active = ""
83     style_closed = ""
84     style_left = ""
85     style_right = ""
86
87 class BlackAndWhite(AnsiColorTheme):
88     pass
89
90 class DefaultTheme(AnsiColorTheme):
91     style_normal = Color.normal
92     style_prompt = Color.blue+Color.bold
93     style_punct = Color.normal
94     style_id = Color.blue+Color.bold
95     style_not_printable = Color.grey
96     style_layer_name = Color.red+Color.bold
97     style_field_name = Color.blue
98     style_field_value = Color.purple
99     style_emph_field_name = Color.blue+Color.uline+Color.bold
100     style_emph_field_value = Color.purple+Color.uline+Color.bold
101     style_packetlist_name = Color.red+Color.bold
102     style_packetlist_proto = Color.blue
103     style_packetlist_value = Color.purple
104     style_fail = Color.red+Color.bold
105     style_success = Color.blue+Color.bold
106     style_even = Color.black+Color.bold
107     style_odd = Color.black
108     style_opening = Color.yellow
109     style_active = Color.black
110     style_closed = Color.grey
111     style_left = Color.blue+Color.invert
112     style_right = Color.red+Color.invert
113    
114 class BrightTheme(AnsiColorTheme):
115     style_normal = Color.normal
116     style_punct = Color.normal
117     style_id = Color.yellow+Color.bold
118     style_layer_name = Color.red+Color.bold
119     style_field_name = Color.yellow+Color.bold
120     style_field_value = Color.purple+Color.bold
121     style_emph_field_name = Color.yellow+Color.bold
122     style_emph_field_value = Color.green+Color.bold
123     style_packetlist_name = Color.red+Color.bold
124     style_packetlist_proto = Color.yellow+Color.bold
125     style_packetlist_value = Color.purple+Color.bold
126     style_fail = Color.red+Color.bold
127     style_success = Color.blue+Color.bold
128     style_even = Color.black+Color.bold
129     style_odd = Color.black
130     style_left = Color.cyan+Color.invert
131     style_right = Color.purple+Color.invert
132
133
134 class RastaTheme(AnsiColorTheme):
135     style_normal = Color.normal+Color.green+Color.bold
136     style_prompt = Color.yellow+Color.bold
137     style_punct = Color.red
138     style_id = Color.green+Color.bold
139     style_not_printable = Color.green
140     style_layer_name = Color.red+Color.bold
141     style_field_name = Color.yellow+Color.bold
142     style_field_value = Color.green+Color.bold
143     style_emph_field_name = Color.green
144     style_emph_field_value = Color.green
145     style_packetlist_name = Color.red+Color.bold
146     style_packetlist_proto = Color.yellow+Color.bold
147     style_packetlist_value = Color.green+Color.bold
148     style_fail = Color.red
149     style_success = Color.red+Color.bold
150     style_even = Color.yellow
151     style_odd = Color.green
152     style_left = Color.yellow+Color.invert
153     style_right = Color.red+Color.invert
154
155 class ColorOnBlackTheme(AnsiColorTheme):
156     """Color theme for black backgrounds"""
157     style_normal = Color.normal
158     style_prompt = Color.green+Color.bold
159     style_punct = Color.normal
160     style_id = Color.green
161     style_not_printable = Color.black+Color.bold
162     style_layer_name = Color.yellow+Color.bold
163     style_field_name = Color.cyan
164     style_field_value = Color.purple+Color.bold
165     style_emph_field_name = Color.cyan+Color.bold
166     style_emph_field_value = Color.red+Color.bold
167     style_packetlist_name = Color.black+Color.bold
168     style_packetlist_proto = Color.yellow+Color.bold
169     style_packetlist_value = Color.purple+Color.bold
170     style_fail = Color.red+Color.bold
171     style_success = Color.green
172     style_even = Color.black+Color.bold
173     style_odd = Color.grey
174     style_opening = Color.yellow
175     style_active = Color.grey+Color.bold
176     style_closed = Color.black+Color.bold
177     style_left = Color.cyan+Color.bold
178     style_right = Color.red+Color.bold
179
180
181 class FormatTheme(ColorTheme):
182     def __getattr__(self, attr):
183         if attr.startswith("__"):
184             raise AttributeError(attr)
185         colfmt = self.__class__.__dict__.get("style_%s" % attr, "%s")
186         return create_styler(fmt2 = colfmt)       
187
188 class LatexTheme(FormatTheme):
189     style_prompt = r"\textcolor{blue}{%s}"
190     style_not_printable = r"\textcolor{gray}{%s}"
191     style_layer_name = r"\textcolor{red}{\bf %s}"
192     style_field_name = r"\textcolor{blue}{%s}"
193     style_field_value = r"\textcolor{purple}{%s}"
194     style_emph_field_name = r"\textcolor{blue}{\underline{%s}}" #ul
195     style_emph_field_value = r"\textcolor{purple}{\underline{%s}}" #ul
196     style_packetlist_name = r"\textcolor{red}{\bf %s}"
197     style_packetlist_proto = r"\textcolor{blue}{%s}"
198     style_packetlist_value = r"\textcolor{purple}{%s}"
199     style_fail = r"\textcolor{red}{\bf %s}"
200     style_success = r"\textcolor{blue}{\bf %s}"
201     style_left = r"\textcolor{blue}{%s}"
202     style_right = r"\textcolor{red}{%s}"
203 #    style_even = r"}{\bf "
204 #    style_odd = ""
205
206 class LatexTheme2(FormatTheme):
207     style_prompt = r"@`@textcolor@[@blue@]@@[@%s@]@"
208     style_not_printable = r"@`@textcolor@[@gray@]@@[@%s@]@"
209     style_layer_name = r"@`@textcolor@[@red@]@@[@@`@bfseries@[@@]@%s@]@"
210     style_field_name = r"@`@textcolor@[@blue@]@@[@%s@]@"
211     style_field_value = r"@`@textcolor@[@purple@]@@[@%s@]@"
212     style_emph_field_name = r"@`@textcolor@[@blue@]@@[@@`@underline@[@%s@]@@]@"
213     style_emph_field_value = r"@`@textcolor@[@purple@]@@[@@`@underline@[@%s@]@@]@"
214     style_packetlist_name = r"@`@textcolor@[@red@]@@[@@`@bfseries@[@@]@%s@]@"
215     style_packetlist_proto = r"@`@textcolor@[@blue@]@@[@%s@]@"
216     style_packetlist_value = r"@`@textcolor@[@purple@]@@[@%s@]@"
217     style_fail = r"@`@textcolor@[@red@]@@[@@`@bfseries@[@@]@%s@]@"
218     style_success = r"@`@textcolor@[@blue@]@@[@@`@bfserices@[@@]@%s@]@"
219     style_even = r"@`@textcolor@[@gray@]@@[@@`@bfseries@[@@]@%s@]@"
220 #    style_odd = r"@`@textcolor@[@black@]@@[@@`@bfseries@[@@]@%s@]@"
221     style_left = r"@`@textcolor@[@blue@]@@[@%s@]@"
222     style_right = r"@`@textcolor@[@red@]@@[@%s@]@"
223
224 class HTMLTheme(FormatTheme):
225     style_prompt = "<span class=prompt>%s</span>"
226     style_not_printable = "<span class=not_printable>%s</span>"
227     style_layer_name = "<span class=layer_name>%s</span>"
228     style_field_name = "<span class=field_name>%s</span>"
229     style_field_value = "<span class=field_value>%s</span>"
230     style_emph_field_name = "<span class=emph_field_name>%s</span>"
231     style_emph_field_value = "<span class=emph_field_value>%s</span>"
232     style_packetlist_name = "<span class=packetlist_name>%s</span>"
233     style_packetlist_proto = "<span class=packetlist_proto>%s</span>"
234     style_packetlist_value = "<span class=packetlist_value>%s</span>"
235     style_fail = "<span class=fail>%s</span>"
236     style_success = "<span class=success>%s</span>"
237     style_even = "<span class=even>%s</span>"
238     style_odd = "<span class=odd>%s</span>"
239     style_left = "<span class=left>%s</span>"
240     style_right = "<span class=right>%s</span>"
241
242 class HTMLTheme2(HTMLTheme):
243     style_prompt = "#[#span class=prompt#]#%s#[#/span#]#"
244     style_not_printable = "#[#span class=not_printable#]#%s#[#/span#]#"
245     style_layer_name = "#[#span class=layer_name#]#%s#[#/span#]#"
246     style_field_name = "#[#span class=field_name#]#%s#[#/span#]#"
247     style_field_value = "#[#span class=field_value#]#%s#[#/span#]#"
248     style_emph_field_name = "#[#span class=emph_field_name#]#%s#[#/span#]#"
249     style_emph_field_value = "#[#span class=emph_field_value#]#%s#[#/span#]#"
250     style_packetlist_name = "#[#span class=packetlist_name#]#%s#[#/span#]#"
251     style_packetlist_proto = "#[#span class=packetlist_proto#]#%s#[#/span#]#"
252     style_packetlist_value = "#[#span class=packetlist_value#]#%s#[#/span#]#"
253     style_fail = "#[#span class=fail#]#%s#[#/span#]#"
254     style_success = "#[#span class=success#]#%s#[#/span#]#"
255     style_even = "#[#span class=even#]#%s#[#/span#]#"
256     style_odd = "#[#span class=odd#]#%s#[#/span#]#"
257     style_left = "#[#span class=left#]#%s#[#/span#]#"
258     style_right = "#[#span class=right#]#%s#[#/span#]#"
259
260
261 class ColorPrompt:
262     __prompt = ">>> "
263     def __str__(self):
264         try:
265             ct = config.conf.color_theme
266             if isinstance(ct, AnsiColorTheme):
267                 ## ^A and ^B delimit invisible caracters for readline to count right
268                 return "\001%s\002" % ct.prompt("\002"+config.conf.prompt+"\001")
269             else:
270                 return ct.prompt(config.conf.prompt)
271         except:
272             return self.__prompt
273
Note: See TracBrowser for help on using the browser.