Better formatting for headers, help and other key-value displays.

We now use proper Columns, rather than laying it out manually.
This commit is contained in:
Aldo Cortesi 2012-02-11 18:23:07 +13:00
parent 3e70fa8d58
commit f25156a637
4 changed files with 50 additions and 54 deletions

View File

@ -27,7 +27,8 @@ def highlight_key(s, k):
return l
def format_keyvals(lst, key="key", val="text", space=5, indent=0):
KEY_MAX = 30
def format_keyvals(lst, key="key", val="text", indent=0):
"""
Format a list of (key, value) tuples.
@ -37,24 +38,25 @@ def format_keyvals(lst, key="key", val="text", space=5, indent=0):
"""
ret = []
if lst:
pad = max(len(i[0]) for i in lst if i and i[0]) + space
maxk = min(max(len(i[0]) for i in lst if i and i[0]), KEY_MAX)
for i, kv in enumerate(lst):
if kv is None:
ret.extend("")
elif kv[0] is None:
ret.append(" "*(pad + indent*2))
ret.extend(kv[1])
ret.append(Text(""))
else:
ret.extend(
[
" "*indent,
(key, kv[0]),
" "*(pad-len(kv[0])),
(val, kv[1]),
]
ret.append(
urwid.Columns(
[
("fixed", indent, urwid.Text("")),
(
"fixed",
maxk,
urwid.Text([(key, kv[0] or "")])
),
urwid.Text([(val, kv[1])])
],
dividechars = 2
)
)
if i < len(lst) - 1:
ret.append("\n")
return ret
@ -124,8 +126,6 @@ def format_flow(f, focus, extended=False, padding=2):
return txt
def int_version(v):
SIG = 3
v = urwid.__version__.split("-")[0].split(".")

View File

@ -149,26 +149,23 @@ class ConnectionView(common.WWrap):
vals.append(utils.cleanBin(
"\n".join(parts[3+parts[2:].index(""):])
))
kv = common.format_keyvals(
r = [
urwid.Text(("highlight", "Form data:\n")),
]
r.extend(common.format_keyvals(
zip(keys, vals),
key = "header",
val = "text"
)
return [
urwid.Text(("highlight", "Form data:\n")),
urwid.Text(kv)
]
))
return r
def _view_conn_urlencoded(self, lines):
return [
urwid.Text(
common.format_keyvals(
[(k+":", v) for (k, v) in lines],
key = "header",
val = "text"
)
)
]
return common.format_keyvals(
[(k+":", v) for (k, v) in lines],
key = "header",
val = "text"
)
def _find_pretty_view(self, content, hdrItems):
ctype = None
@ -189,19 +186,15 @@ class ConnectionView(common.WWrap):
elif ctype and "multipart/form-data" in ctype:
boundary = ctype.split('boundary=')
if len(boundary) > 1:
return "FOrm data", self._view_conn_formdata(content, boundary[1].split(';')[0])
return "Form data", self._view_conn_formdata(content, boundary[1].split(';')[0])
return "", self._view_conn_raw(content)
def _cached_conn_text(self, e, content, hdrItems, viewmode):
hdr = []
hdr.extend(
common.format_keyvals(
txt = common.format_keyvals(
[(h+":", v) for (h, v) in hdrItems],
key = "header",
val = "text"
)
)
txt = [urwid.Text(hdr)]
if content:
msg = ""
if viewmode == common.VIEW_BODY_HEX:

View File

@ -23,10 +23,10 @@ class HelpView(urwid.ListBox):
def helptext(self):
text = []
text.append(("head", "Keys for this view:\n"))
text.append(urwid.Text([("head", "Keys for this view:\n")]))
text.extend(self.help_context)
text.append(("head", "\n\nMovement:\n"))
text.append(urwid.Text([("head", "\n\nMovement:\n")]))
keys = [
("j, k", "up, down"),
("h, l", "left, right (in some contexts)"),
@ -36,7 +36,7 @@ class HelpView(urwid.ListBox):
]
text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
text.append(("head", "\n\nGlobal keys:\n"))
text.append(urwid.Text([("head", "\n\nGlobal keys:\n")]))
keys = [
("c", "client replay"),
("i", "set interception pattern"),
@ -68,7 +68,7 @@ class HelpView(urwid.ListBox):
]
text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
text.append(("head", "\n\nFilter expressions:\n"))
text.append(urwid.Text([("head", "\n\nFilter expressions:\n")]))
f = []
for i in filt.filt_unary:
f.append(
@ -93,16 +93,18 @@ class HelpView(urwid.ListBox):
)
text.extend(common.format_keyvals(f, key="key", val="text", indent=4))
text.extend(
[
"\n",
("text", " Regexes are Python-style.\n"),
("text", " Regexes can be specified as quoted strings.\n"),
("text", " Header matching (~h, ~hq, ~hs) is against a string of the form \"name: value\".\n"),
("text", " Expressions with no operators are regex matches against URL.\n"),
("text", " Default binary operator is &.\n"),
("head", "\n Examples:\n"),
]
text.append(
urwid.Text(
[
"\n",
("text", " Regexes are Python-style.\n"),
("text", " Regexes can be specified as quoted strings.\n"),
("text", " Header matching (~h, ~hq, ~hs) is against a string of the form \"name: value\".\n"),
("text", " Expressions with no operators are regex matches against URL.\n"),
("text", " Default binary operator is &.\n"),
("head", "\n Examples:\n"),
]
)
)
examples = [
("google\.com", "Url containing \"google.com"),
@ -110,5 +112,5 @@ class HelpView(urwid.ListBox):
("!(~q & ~t \"text/html\")", "Anything but requests with a text/html content type."),
]
text.extend(common.format_keyvals(examples, key="key", val="text", indent=4))
return [urwid.Text(text)]
return text

View File

@ -87,10 +87,11 @@ class KVItem(common.WWrap):
return True
KEY_MAX = 30
class KVWalker(urwid.ListWalker):
def __init__(self, lst, editor):
self.lst, self.editor = lst, editor
self.maxk = max(len(v[0]) for v in lst) if lst else 20
self.maxk = min(max(len(v[0]) for v in lst), KEY_MAX) if lst else 20
if self.maxk < 20:
self.maxk = 20
self.focus = 0