Show More
@@ -145,6 +145,43 b' class CommandError(Exception):' | |||
|
145 | 145 | """ |
|
146 | 146 | |
|
147 | 147 | |
|
148 | class Keymap(dict): | |
|
149 | """ | |
|
150 | Stores mapping of keys to commands. | |
|
151 | """ | |
|
152 | def __init__(self): | |
|
153 | self._keymap = {} | |
|
154 | ||
|
155 | def __setitem__(self, key, command): | |
|
156 | if isinstance(key, str): | |
|
157 | for c in key: | |
|
158 | dict.__setitem__(self, ord(c), command) | |
|
159 | else: | |
|
160 | dict.__setitem__(self, key, command) | |
|
161 | ||
|
162 | def register(self, command, *keys): | |
|
163 | for key in keys: | |
|
164 | self[key] = command | |
|
165 | ||
|
166 | def __getitem__(self, key): | |
|
167 | if isinstance(key, str): | |
|
168 | key = ord(key) | |
|
169 | return dict.__getitem__(self, key) | |
|
170 | ||
|
171 | def get(self, key, default=None): | |
|
172 | if isinstance(key, str): | |
|
173 | key = ord(key) | |
|
174 | return dict.get(self, key, default) | |
|
175 | ||
|
176 | def findkey(self, command, default=ipipe.noitem): | |
|
177 | for (key, commandcandidate) in self.iteritems(): | |
|
178 | if commandcandidate == command: | |
|
179 | return key | |
|
180 | if default is ipipe.noitem: | |
|
181 | raise KeyError(command) | |
|
182 | return default | |
|
183 | ||
|
184 | ||
|
148 | 185 | class _BrowserCachedItem(object): |
|
149 | 186 | # This is used internally by ``ibrowse`` to store a item together with its |
|
150 | 187 | # marked status. |
@@ -464,29 +501,21 b' class _BrowserLevel(object):' | |||
|
464 | 501 | |
|
465 | 502 | |
|
466 | 503 | class _CommandInput(object): |
|
467 |
keymap = |
|
|
468 | curses.KEY_LEFT: "left", | |
|
469 | curses.KEY_RIGHT: "right", | |
|
470 | curses.KEY_HOME: "home", | |
|
471 | 1: "home", | |
|
472 | curses.KEY_END: "end", | |
|
473 | 5: "end", | |
|
504 | keymap = Keymap() | |
|
505 | keymap.register("left", curses.KEY_LEFT) | |
|
506 | keymap.register("right", curses.KEY_RIGHT) | |
|
507 | keymap.register("home", curses.KEY_HOME, "\x01") # Ctrl-A | |
|
508 | keymap.register("end", curses.KEY_END, "\x05") # Ctrl-E | |
|
474 | 509 |
|
|
475 | 8: "backspace", | |
|
476 | 127: "backspace", | |
|
477 | curses.KEY_BACKSPACE: "backspace", | |
|
478 | curses.KEY_DC: "delete", | |
|
479 | # CTRL-K | |
|
480 | 0x0B: "delend", | |
|
481 | ord("\n"): "execute", | |
|
482 | ord("\r"): "execute", | |
|
483 | curses.KEY_UP: "up", | |
|
484 | curses.KEY_DOWN: "down", | |
|
485 | curses.KEY_PPAGE: "incsearchup", | |
|
486 | curses.KEY_NPAGE: "incsearchdown", | |
|
487 | # CTRL-X | |
|
488 | 0x18: "exit", | |
|
489 | } | |
|
510 | keymap.register("backspace", curses.KEY_BACKSPACE, "x\x08\x7f") | |
|
511 | keymap.register("delete", curses.KEY_DC) | |
|
512 | keymap.register("delend", 0x0b) # Ctrl-K | |
|
513 | keymap.register("execute", "\r\n") | |
|
514 | keymap.register("up", curses.KEY_UP) | |
|
515 | keymap.register("down", curses.KEY_DOWN) | |
|
516 | keymap.register("incsearchup", curses.KEY_PPAGE) | |
|
517 | keymap.register("incsearchdown", curses.KEY_NPAGE) | |
|
518 | keymap.register("exit", "\x18"), # Ctrl-X | |
|
490 | 519 | |
|
491 | 520 | def __init__(self, prompt): |
|
492 | 521 | self.prompt = prompt |
@@ -775,49 +804,40 b' class ibrowse(ipipe.Display):' | |||
|
775 | 804 | } |
|
776 | 805 | |
|
777 | 806 | # Maps curses key codes to "function" names |
|
778 |
keymap = |
|
|
779 | ord("q"): "quit", | |
|
780 | curses.KEY_UP: "up", | |
|
781 | curses.KEY_DOWN: "down", | |
|
782 | curses.KEY_PPAGE: "pageup", | |
|
783 | curses.KEY_NPAGE: "pagedown", | |
|
784 | curses.KEY_LEFT: "left", | |
|
785 | curses.KEY_RIGHT: "right", | |
|
786 | curses.KEY_HOME: "home", | |
|
787 | 1: "home", | |
|
788 | curses.KEY_END: "end", | |
|
789 | 5: "end", | |
|
790 | ord("<"): "prevattr", | |
|
791 | 0x1b: "prevattr", # SHIFT-TAB | |
|
792 | ord(">"): "nextattr", | |
|
793 | ord("\t"):"nextattr", # TAB | |
|
794 | ord("p"): "pick", | |
|
795 | ord("P"): "pickattr", | |
|
796 | ord("C"): "pickallattrs", | |
|
797 | ord("m"): "pickmarked", | |
|
798 | ord("M"): "pickmarkedattr", | |
|
799 | ord("\n"): "enterdefault", | |
|
800 | ord("\r"): "enterdefault", | |
|
807 | keymap = Keymap() | |
|
808 | keymap.register("quit", "q") | |
|
809 | keymap.register("up", curses.KEY_UP) | |
|
810 | keymap.register("down", curses.KEY_DOWN) | |
|
811 | keymap.register("pageup", curses.KEY_PPAGE) | |
|
812 | keymap.register("pagedown", curses.KEY_NPAGE) | |
|
813 | keymap.register("left", curses.KEY_LEFT) | |
|
814 | keymap.register("right", curses.KEY_RIGHT) | |
|
815 | keymap.register("home", curses.KEY_HOME, "\x01") | |
|
816 | keymap.register("end", curses.KEY_END, "\x05") | |
|
817 | keymap.register("prevattr", "<\x1b") | |
|
818 | keymap.register("nextattr", ">\t") | |
|
819 | keymap.register("pick", "p") | |
|
820 | keymap.register("pickattr", "P") | |
|
821 | keymap.register("pickallattrs", "C") | |
|
822 | keymap.register("pickmarked", "m") | |
|
823 | keymap.register("pickmarkedattr", "M") | |
|
824 | keymap.register("enterdefault", "\r\n") | |
|
801 | 825 |
|
|
802 | 8: "leave", | |
|
803 | 127: "leave", | |
|
804 | curses.KEY_BACKSPACE: "leave", | |
|
805 | ord("x"): "leave", | |
|
806 | ord("h"): "hideattr", | |
|
807 | ord("H"): "unhideattrs", | |
|
808 | ord("?"): "help", | |
|
809 | ord("e"): "enter", | |
|
810 | ord("E"): "enterattr", | |
|
811 | ord("d"): "detail", | |
|
812 | ord("D"): "detailattr", | |
|
813 | ord(" "): "tooglemark", | |
|
814 | ord("r"): "markrange", | |
|
815 | ord("v"): "sortattrasc", | |
|
816 | ord("V"): "sortattrdesc", | |
|
817 | ord("g"): "goto", | |
|
818 | ord("f"): "find", | |
|
819 | ord("b"): "findbackwards", | |
|
820 | } | |
|
826 | keymap.register("leave", curses.KEY_BACKSPACE, "x\x08\x7f") | |
|
827 | keymap.register("hideattr", "h") | |
|
828 | keymap.register("unhideattrs", "H") | |
|
829 | keymap.register("help", "?") | |
|
830 | keymap.register("enter", "e") | |
|
831 | keymap.register("enterattr", "E") | |
|
832 | keymap.register("detail", "d") | |
|
833 | keymap.register("detailattr", "D") | |
|
834 | keymap.register("tooglemark", " ") | |
|
835 | keymap.register("markrange", "r") | |
|
836 | keymap.register("sortattrasc", "v") | |
|
837 | keymap.register("sortattrdesc", "V") | |
|
838 | keymap.register("goto", "g") | |
|
839 | keymap.register("find", "f") | |
|
840 | keymap.register("findbackwards", "b") | |
|
821 | 841 | |
|
822 | 842 | def __init__(self, *attrs): |
|
823 | 843 | """ |
@@ -1331,12 +1351,12 b' class ibrowse(ipipe.Display):' | |||
|
1331 | 1351 | footery = 2 |
|
1332 | 1352 | |
|
1333 | 1353 | keys = [] |
|
1334 | for (key, cmd) in self.keymap.iteritems(): | |
|
1335 | if cmd == "quit": | |
|
1336 |
|
|
|
1337 | for (key, cmd) in self.keymap.iteritems(): | |
|
1338 | if cmd == "help": | |
|
1339 |
|
|
|
1354 | quitkey = self.keymap.findkey("quit", None) | |
|
1355 | if quitkey is not None: | |
|
1356 | keys.append("%s=quit" % self.keylabel(quitkey)) | |
|
1357 | helpkey = self.keymap.findkey("help", None) | |
|
1358 | if helpkey is not None: | |
|
1359 | keys.append("%s=help" % self.keylabel(helpkey)) | |
|
1340 | 1360 | helpmsg = " | %s" % " ".join(keys) |
|
1341 | 1361 | |
|
1342 | 1362 | scr.clear() |
@@ -1,7 +1,12 b'' | |||
|
1 | 2006-06-22 Walter Doerwald <walter@livinglogic.de> | |
|
2 | ||
|
3 | * IPython/Extensions/ibrowse.py: Replace the plain dictionaries used | |
|
4 | for keymaps with a custom class that simplifies handling. | |
|
5 | ||
|
1 | 6 | 2006-06-19 Walter Doerwald <walter@livinglogic.de> |
|
2 | 7 | |
|
3 | 8 | * IPython/Extensions/ibrowse.py: ibrowse now properly handles terminal |
|
4 |
resizing. This requires Python 2.5 to wor |
|
|
9 | resizing. This requires Python 2.5 to work. | |
|
5 | 10 | |
|
6 | 11 | 2006-06-16 Walter Doerwald <walter@livinglogic.de> |
|
7 | 12 |
General Comments 0
You need to be logged in to leave comments.
Login now