Skip to content
Snippets Groups Projects
Commit f1e86ef4 authored by Phil's avatar Phil
Browse files

Fixed NoTheme and factorized some code (ticket #143)

parent d7dd2222
No related branches found
No related tags found
No related merge requests found
...@@ -104,7 +104,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis ...@@ -104,7 +104,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis
if lfilter is not None: if lfilter is not None:
if not lfilter(self.res[i]): if not lfilter(self.res[i]):
continue continue
print conf.color_theme.id(i,"%04i"), print conf.color_theme.id(i,fmt="%04i"),
if prn is None: if prn is None:
print self._elt2sum(self.res[i]) print self._elt2sum(self.res[i])
else: else:
...@@ -187,7 +187,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis ...@@ -187,7 +187,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis
p = self._elt2pkt(self.res[i]) p = self._elt2pkt(self.res[i])
if lfilter is not None and not lfilter(p): if lfilter is not None and not lfilter(p):
continue continue
print "%s %s %s" % (conf.color_theme.id(i,"%04i"), print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"),
p.sprintf("%.time%"), p.sprintf("%.time%"),
self._elt2sum(self.res[i])) self._elt2sum(self.res[i]))
if p.haslayer(conf.raw_layer): if p.haslayer(conf.raw_layer):
...@@ -200,7 +200,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis ...@@ -200,7 +200,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis
p = self._elt2pkt(self.res[i]) p = self._elt2pkt(self.res[i])
if lfilter is not None and not lfilter(p): if lfilter is not None and not lfilter(p):
continue continue
print "%s %s %s" % (conf.color_theme.id(i,"%04i"), print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"),
p.sprintf("%.time%"), p.sprintf("%.time%"),
self._elt2sum(self.res[i])) self._elt2sum(self.res[i]))
hexdump(p) hexdump(p)
...@@ -211,7 +211,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis ...@@ -211,7 +211,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis
p = self._elt2pkt(self.res[i]) p = self._elt2pkt(self.res[i])
if p.haslayer(Padding): if p.haslayer(Padding):
if lfilter is None or lfilter(p): if lfilter is None or lfilter(p):
print "%s %s %s" % (conf.color_theme.id(i,"%04i"), print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"),
p.sprintf("%.time%"), p.sprintf("%.time%"),
self._elt2sum(self.res[i])) self._elt2sum(self.res[i]))
hexdump(p.getlayer(Padding).load) hexdump(p.getlayer(Padding).load)
...@@ -225,7 +225,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis ...@@ -225,7 +225,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis
if pad == pad[0]*len(pad): if pad == pad[0]*len(pad):
continue continue
if lfilter is None or lfilter(p): if lfilter is None or lfilter(p):
print "%s %s %s" % (conf.color_theme.id(i,"%04i"), print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"),
p.sprintf("%.time%"), p.sprintf("%.time%"),
self._elt2sum(self.res[i])) self._elt2sum(self.res[i]))
hexdump(p.getlayer(Padding).load) hexdump(p.getlayer(Padding).load)
......
...@@ -26,11 +26,21 @@ class Color: ...@@ -26,11 +26,21 @@ class Color:
invert = "\033[7m" invert = "\033[7m"
def create_styler(fmt=None, before="", after="", fmt2="%s"):
def do_style(val, fmt=fmt, before=before, after=after, fmt2=fmt2):
if fmt is None:
if type(val) is not str:
val = str(val)
else:
val = fmt % val
return fmt2 % (before+val+after)
return do_style
class ColorTheme: class ColorTheme:
def __repr__(self): def __repr__(self):
return "<%s>" % self.__class__.__name__ return "<%s>" % self.__class__.__name__
def __getattr__(self, attr): def __getattr__(self, attr):
return lambda x:x return create_styler()
class NoTheme(ColorTheme): class NoTheme(ColorTheme):
...@@ -48,14 +58,7 @@ class AnsiColorTheme(ColorTheme): ...@@ -48,14 +58,7 @@ class AnsiColorTheme(ColorTheme):
else: else:
before = after = "" before = after = ""
def do_style(val, fmt=None, before=before, after=after): return create_styler(before=before, after=after)
if fmt is None:
if type(val) is not str:
val = str(val)
else:
val = fmt % val
return before+val+after
return do_style
style_normal = "" style_normal = ""
...@@ -174,20 +177,13 @@ class ColorOnBlackTheme(AnsiColorTheme): ...@@ -174,20 +177,13 @@ class ColorOnBlackTheme(AnsiColorTheme):
style_left = Color.cyan+Color.bold style_left = Color.cyan+Color.bold
style_right = Color.red+Color.bold style_right = Color.red+Color.bold
class FormatTheme(ColorTheme): class FormatTheme(ColorTheme):
def __getattr__(self, attr): def __getattr__(self, attr):
if attr.startswith("__"): if attr.startswith("__"):
raise AttributeError(attr) raise AttributeError(attr)
col = self.__class__.__dict__.get("style_%s" % attr, "%s") colfmt = self.__class__.__dict__.get("style_%s" % attr, "%s")
def do_style(val, fmt=None, col=col): return create_styler(fmt2 = colfmt)
if fmt is None:
if type(val) is not str:
val = str(val)
else:
val = fmt % val
return col % val
return do_style
class LatexTheme(FormatTheme): class LatexTheme(FormatTheme):
style_prompt = r"\textcolor{blue}{%s}" style_prompt = r"\textcolor{blue}{%s}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment