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 | class _BrowserCachedItem(object): |
|
185 | class _BrowserCachedItem(object): | |
149 | # This is used internally by ``ibrowse`` to store a item together with its |
|
186 | # This is used internally by ``ibrowse`` to store a item together with its | |
150 | # marked status. |
|
187 | # marked status. | |
@@ -464,29 +501,21 b' class _BrowserLevel(object):' | |||||
464 |
|
501 | |||
465 |
|
502 | |||
466 | class _CommandInput(object): |
|
503 | class _CommandInput(object): | |
467 |
keymap = |
|
504 | keymap = Keymap() | |
468 | curses.KEY_LEFT: "left", |
|
505 | keymap.register("left", curses.KEY_LEFT) | |
469 | curses.KEY_RIGHT: "right", |
|
506 | keymap.register("right", curses.KEY_RIGHT) | |
470 | curses.KEY_HOME: "home", |
|
507 | keymap.register("home", curses.KEY_HOME, "\x01") # Ctrl-A | |
471 | 1: "home", |
|
508 | keymap.register("end", curses.KEY_END, "\x05") # Ctrl-E | |
472 | curses.KEY_END: "end", |
|
509 | # FIXME: What's happening here? | |
473 | 5: "end", |
|
510 | keymap.register("backspace", curses.KEY_BACKSPACE, "x\x08\x7f") | |
474 | # FIXME: What's happening here? |
|
511 | keymap.register("delete", curses.KEY_DC) | |
475 | 8: "backspace", |
|
512 | keymap.register("delend", 0x0b) # Ctrl-K | |
476 | 127: "backspace", |
|
513 | keymap.register("execute", "\r\n") | |
477 | curses.KEY_BACKSPACE: "backspace", |
|
514 | keymap.register("up", curses.KEY_UP) | |
478 | curses.KEY_DC: "delete", |
|
515 | keymap.register("down", curses.KEY_DOWN) | |
479 | # CTRL-K |
|
516 | keymap.register("incsearchup", curses.KEY_PPAGE) | |
480 | 0x0B: "delend", |
|
517 | keymap.register("incsearchdown", curses.KEY_NPAGE) | |
481 | ord("\n"): "execute", |
|
518 | keymap.register("exit", "\x18"), # Ctrl-X | |
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 | } |
|
|||
490 |
|
519 | |||
491 | def __init__(self, prompt): |
|
520 | def __init__(self, prompt): | |
492 | self.prompt = prompt |
|
521 | self.prompt = prompt | |
@@ -775,49 +804,40 b' class ibrowse(ipipe.Display):' | |||||
775 | } |
|
804 | } | |
776 |
|
805 | |||
777 | # Maps curses key codes to "function" names |
|
806 | # Maps curses key codes to "function" names | |
778 |
keymap = |
|
807 | keymap = Keymap() | |
779 | ord("q"): "quit", |
|
808 | keymap.register("quit", "q") | |
780 | curses.KEY_UP: "up", |
|
809 | keymap.register("up", curses.KEY_UP) | |
781 | curses.KEY_DOWN: "down", |
|
810 | keymap.register("down", curses.KEY_DOWN) | |
782 | curses.KEY_PPAGE: "pageup", |
|
811 | keymap.register("pageup", curses.KEY_PPAGE) | |
783 | curses.KEY_NPAGE: "pagedown", |
|
812 | keymap.register("pagedown", curses.KEY_NPAGE) | |
784 | curses.KEY_LEFT: "left", |
|
813 | keymap.register("left", curses.KEY_LEFT) | |
785 | curses.KEY_RIGHT: "right", |
|
814 | keymap.register("right", curses.KEY_RIGHT) | |
786 | curses.KEY_HOME: "home", |
|
815 | keymap.register("home", curses.KEY_HOME, "\x01") | |
787 | 1: "home", |
|
816 | keymap.register("end", curses.KEY_END, "\x05") | |
788 | curses.KEY_END: "end", |
|
817 | keymap.register("prevattr", "<\x1b") | |
789 | 5: "end", |
|
818 | keymap.register("nextattr", ">\t") | |
790 | ord("<"): "prevattr", |
|
819 | keymap.register("pick", "p") | |
791 | 0x1b: "prevattr", # SHIFT-TAB |
|
820 | keymap.register("pickattr", "P") | |
792 | ord(">"): "nextattr", |
|
821 | keymap.register("pickallattrs", "C") | |
793 | ord("\t"):"nextattr", # TAB |
|
822 | keymap.register("pickmarked", "m") | |
794 | ord("p"): "pick", |
|
823 | keymap.register("pickmarkedattr", "M") | |
795 | ord("P"): "pickattr", |
|
824 | keymap.register("enterdefault", "\r\n") | |
796 | ord("C"): "pickallattrs", |
|
825 | # FIXME: What's happening here? | |
797 | ord("m"): "pickmarked", |
|
826 | keymap.register("leave", curses.KEY_BACKSPACE, "x\x08\x7f") | |
798 | ord("M"): "pickmarkedattr", |
|
827 | keymap.register("hideattr", "h") | |
799 | ord("\n"): "enterdefault", |
|
828 | keymap.register("unhideattrs", "H") | |
800 | ord("\r"): "enterdefault", |
|
829 | keymap.register("help", "?") | |
801 | # FIXME: What's happening here? |
|
830 | keymap.register("enter", "e") | |
802 | 8: "leave", |
|
831 | keymap.register("enterattr", "E") | |
803 | 127: "leave", |
|
832 | keymap.register("detail", "d") | |
804 | curses.KEY_BACKSPACE: "leave", |
|
833 | keymap.register("detailattr", "D") | |
805 | ord("x"): "leave", |
|
834 | keymap.register("tooglemark", " ") | |
806 | ord("h"): "hideattr", |
|
835 | keymap.register("markrange", "r") | |
807 | ord("H"): "unhideattrs", |
|
836 | keymap.register("sortattrasc", "v") | |
808 | ord("?"): "help", |
|
837 | keymap.register("sortattrdesc", "V") | |
809 | ord("e"): "enter", |
|
838 | keymap.register("goto", "g") | |
810 | ord("E"): "enterattr", |
|
839 | keymap.register("find", "f") | |
811 | ord("d"): "detail", |
|
840 | keymap.register("findbackwards", "b") | |
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 | } |
|
|||
821 |
|
841 | |||
822 | def __init__(self, *attrs): |
|
842 | def __init__(self, *attrs): | |
823 | """ |
|
843 | """ | |
@@ -1331,12 +1351,12 b' class ibrowse(ipipe.Display):' | |||||
1331 | footery = 2 |
|
1351 | footery = 2 | |
1332 |
|
1352 | |||
1333 | keys = [] |
|
1353 | keys = [] | |
1334 | for (key, cmd) in self.keymap.iteritems(): |
|
1354 | quitkey = self.keymap.findkey("quit", None) | |
1335 | if cmd == "quit": |
|
1355 | if quitkey is not None: | |
1336 |
|
|
1356 | keys.append("%s=quit" % self.keylabel(quitkey)) | |
1337 | for (key, cmd) in self.keymap.iteritems(): |
|
1357 | helpkey = self.keymap.findkey("help", None) | |
1338 | if cmd == "help": |
|
1358 | if helpkey is not None: | |
1339 |
|
|
1359 | keys.append("%s=help" % self.keylabel(helpkey)) | |
1340 | helpmsg = " | %s" % " ".join(keys) |
|
1360 | helpmsg = " | %s" % " ".join(keys) | |
1341 |
|
1361 | |||
1342 | scr.clear() |
|
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 | 2006-06-19 Walter Doerwald <walter@livinglogic.de> |
|
6 | 2006-06-19 Walter Doerwald <walter@livinglogic.de> | |
2 |
|
7 | |||
3 | * IPython/Extensions/ibrowse.py: ibrowse now properly handles terminal |
|
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 | 2006-06-16 Walter Doerwald <walter@livinglogic.de> |
|
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