Skip to content

Commit 261cf20

Browse files
committed
Initial work on urwid shortcuts working, todo: cleanup
1 parent bb2190c commit 261cf20

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

bpython/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from bpython.config import Struct
5555

5656
# This for keys
57-
from bpython.keys import key_dispatch
57+
from bpython.keys import cli_key_dispatch as key_dispatch
5858

5959
# This for i18n
6060
from bpython import translations

bpython/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from ConfigParser import ConfigParser
55
from itertools import chain
6-
from bpython.keys import key_dispatch
6+
from bpython.keys import cli_key_dispatch as key_dispatch
77

88

99
class Struct(object):

bpython/keys.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,27 @@ def __delitem__(self, key):
4747
def __setitem__(self, key, value):
4848
self.map[key] = value
4949

50-
key_dispatch = KeyMap()
50+
cli_key_dispatch = KeyMap()
51+
urwid_key_dispatch = KeyMap()
5152

5253
# fill dispatch with letters
5354
for c in string.ascii_lowercase:
54-
key_dispatch['C-%s' % c] = (chr(string.ascii_lowercase.index(c) + 1),
55+
cli_key_dispatch['C-%s' % c] = (chr(string.ascii_lowercase.index(c) + 1),
5556
'^%s' % c.upper())
5657

58+
for c in string.ascii_lowercase:
59+
urwid_key_dispatch['C-%s' % c] = 'ctrl %s' % c
60+
5761
# fill dispatch with cool characters
58-
key_dispatch['C-['] = (chr(27), '^[')
59-
key_dispatch['C-\\'] = (chr(28), '^\\')
60-
key_dispatch['C-]'] = (chr(29), '^]')
61-
key_dispatch['C-^'] = (chr(30), '^^')
62-
key_dispatch['C-_'] = (chr(31), '^_')
62+
cli_key_dispatch['C-['] = (chr(27), '^[')
63+
cli_key_dispatch['C-\\'] = (chr(28), '^\\')
64+
cli_key_dispatch['C-]'] = (chr(29), '^]')
65+
cli_key_dispatch['C-^'] = (chr(30), '^^')
66+
cli_key_dispatch['C-_'] = (chr(31), '^_')
6367

6468
# fill dispatch with function keys
6569
for x in xrange(1, 13):
66-
key_dispatch['F%s' % str(x)] = ('KEY_F(%s)' % str(x),)
70+
cli_key_dispatch['F%s' % str(x)] = ('KEY_F(%s)' % str(x),)
71+
72+
for x in xrange(1, 13):
73+
urwid_key_dispatch['F%s' % str(x)] = 'F%s' % str(x)

bpython/urwid.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
from bpython.importcompletion import find_coroutine
5050
from bpython.translations import _
5151

52+
from bpython.keys import urwid_key_dispatch as key_dispatch
53+
5254
import urwid
5355

5456
py3 = sys.version_info[0] == 3
@@ -77,6 +79,8 @@
7779
'd': 'default',
7880
}
7981

82+
# Add our keys to the urwid command_map
83+
8084

8185
try:
8286
from twisted.internet import protocol
@@ -215,12 +219,13 @@ class BPythonEdit(urwid.Edit):
215219

216220
signals = ['edit-pos-changed']
217221

218-
def __init__(self, tab_length, *args, **kwargs):
222+
def __init__(self, config, *args, **kwargs):
219223
self._bpy_text = ''
220224
self._bpy_attr = []
221225
self._bpy_selectable = True
222226
self._bpy_may_move_cursor = False
223-
self.tab_length = tab_length
227+
self.config = config
228+
self.tab_length = config.tab_length
224229
urwid.Edit.__init__(self, *args, **kwargs)
225230

226231
def set_edit_pos(self, pos):
@@ -294,6 +299,9 @@ def keypress(self, size, key):
294299
if not (cpos or len(line) % self.tab_length or line.strip()):
295300
self.set_edit_text(line[:-self.tab_length])
296301
return None
302+
elif key == 'pastebin':
303+
# do pastebin
304+
pass
297305
# TODO: Add in specific keypress fetching code here
298306
return urwid.Edit.keypress(self, size, key)
299307
finally:
@@ -451,6 +459,8 @@ def __init__(self, event_loop, palette, interpreter, config):
451459
self.edit = None
452460
self._completion_update_suppressed = False
453461

462+
load_urwid_command_map(config)
463+
454464
# Subclasses of Repl need to implement echo, current_line, cw
455465
def echo(self, s):
456466
s = s.rstrip('\n')
@@ -722,11 +732,11 @@ def prompt(self, more):
722732
self.rl_history.reset()
723733
# XXX what is s_hist?
724734
if not more:
725-
self.edit = BPythonEdit(self.config.tab_length,
735+
self.edit = BPythonEdit(self.config,
726736
caption=('prompt', '>>> '))
727737
self.stdout_hist += '>>> '
728738
else:
729-
self.edit = BPythonEdit(self.config.tab_length,
739+
self.edit = BPythonEdit(self.config,
730740
caption=('prompt_more', '... '))
731741
self.stdout_hist += '... '
732742

@@ -1036,6 +1046,25 @@ def run_find_coroutine():
10361046
sys.stdout.write(myrepl.getstdout())
10371047
sys.stdout.flush()
10381048

1039-
1049+
def load_urwid_command_map(config):
1050+
urwid.command_map[key_dispatch[config.up_one_line_key]] = 'cursor up'
1051+
urwid.command_map[key_dispatch[config.down_one_line_key]] = 'cursor down'
1052+
"""
1053+
'clear_line': 'C-u',
1054+
'clear_screen': 'C-l',
1055+
'clear_word': 'C-w',
1056+
'cut_to_buffer': 'C-k',
1057+
'delete': 'C-d',
1058+
'down_one_line': 'C-n',
1059+
'exit': '',
1060+
'last_output': 'F9',
1061+
'pastebin': 'F8',
1062+
'save': 'C-s',
1063+
'show_source': 'F2',
1064+
'suspend': 'C-z',
1065+
'undo': 'C-r',
1066+
'up_one_line': 'C-p',
1067+
'yank_from_buffer': 'C-y'},
1068+
"""
10401069
if __name__ == '__main__':
10411070
main()

0 commit comments

Comments
 (0)