aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/console/pathedit.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2015-03-20 10:54:57 +1300
committerAldo Cortesi <aldo@nullcube.com>2015-03-20 10:54:57 +1300
commit560e44c637e4f1fcbeba1305fc1eb39e3d796013 (patch)
treec7ca1308ce79ed9195ffffa7cfb9b5abdce50008 /libmproxy/console/pathedit.py
parenta3f4296bf1ba0ac1a72d5a44a504d375707fdc39 (diff)
downloadmitmproxy-560e44c637e4f1fcbeba1305fc1eb39e3d796013.tar.gz
mitmproxy-560e44c637e4f1fcbeba1305fc1eb39e3d796013.tar.bz2
mitmproxy-560e44c637e4f1fcbeba1305fc1eb39e3d796013.zip
Pull PathEdit out into its own file.
Diffstat (limited to 'libmproxy/console/pathedit.py')
-rw-r--r--libmproxy/console/pathedit.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/libmproxy/console/pathedit.py b/libmproxy/console/pathedit.py
new file mode 100644
index 00000000..53cda3be
--- /dev/null
+++ b/libmproxy/console/pathedit.py
@@ -0,0 +1,69 @@
+import glob
+import os.path
+
+import urwid
+
+
+class _PathCompleter:
+ def __init__(self, _testing=False):
+ """
+ _testing: disables reloading of the lookup table to make testing
+ possible.
+ """
+ self.lookup, self.offset = None, None
+ self.final = None
+ self._testing = _testing
+
+ def reset(self):
+ self.lookup = None
+ self.offset = -1
+
+ def complete(self, txt):
+ """
+ Returns the next completion for txt, or None if there is no
+ completion.
+ """
+ path = os.path.expanduser(txt)
+ if not self.lookup:
+ if not self._testing:
+ # Lookup is a set of (display value, actual value) tuples.
+ self.lookup = []
+ if os.path.isdir(path):
+ files = glob.glob(os.path.join(path, "*"))
+ prefix = txt
+ else:
+ files = glob.glob(path+"*")
+ prefix = os.path.dirname(txt)
+ prefix = prefix or "./"
+ for f in files:
+ display = os.path.join(prefix, os.path.basename(f))
+ if os.path.isdir(f):
+ display += "/"
+ self.lookup.append((display, f))
+ if not self.lookup:
+ self.final = path
+ return path
+ self.lookup.sort()
+ self.offset = -1
+ self.lookup.append((txt, txt))
+ self.offset += 1
+ if self.offset >= len(self.lookup):
+ self.offset = 0
+ ret = self.lookup[self.offset]
+ self.final = ret[1]
+ return ret[0]
+
+
+class PathEdit(urwid.Edit, _PathCompleter):
+ def __init__(self, *args, **kwargs):
+ urwid.Edit.__init__(self, *args, **kwargs)
+ _PathCompleter.__init__(self)
+
+ def keypress(self, size, key):
+ if key == "tab":
+ comp = self.complete(self.get_edit_text())
+ self.set_edit_text(comp)
+ self.set_edit_pos(len(comp))
+ else:
+ self.reset()
+ return urwid.Edit.keypress(self, size, key)