X Tutup
Skip to content

Commit 3150f82

Browse files
committed
First start at configurable keys
1 parent dc8f93d commit 3150f82

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

bpython/cli.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
# This for config
6666
from bpython.config import Struct, loadini, migrate_rc
6767

68+
# This for keys
69+
from bpython.keys import key_dispatch
70+
6871
from bpython import __version__
6972

7073
def log(x):
@@ -1323,11 +1326,11 @@ def p_key(self):
13231326
else:
13241327
return ''
13251328

1326-
elif self.c == 'KEY_F(2)':
1329+
elif self.c in key_dispatch[OPTS.save_key]:
13271330
self.write2file()
13281331
return ''
13291332

1330-
elif self.c == 'KEY_F(8)':
1333+
elif self.c == key_dispatch[OPTS.pastebin_key]:
13311334
self.pastebin()
13321335
return ''
13331336

@@ -1618,7 +1621,6 @@ def get_key(self):
16181621
if self.idle:
16191622
self.idle(self)
16201623

1621-
16221624
class Statusbar(object):
16231625
"""This class provides the status bar at the bottom of the screen.
16241626
It has message() and prompt() methods for user interactivity, as
@@ -1720,7 +1722,7 @@ def bs(s):
17201722
o = bs(o)
17211723
continue
17221724

1723-
if not c or c > 127:
1725+
if not c or c < 0 or c > 127:
17241726
continue
17251727
c = chr(c)
17261728

@@ -1783,7 +1785,7 @@ def init_wins(scr, cols):
17831785
# This should show to be configured keys from bpython.ini
17841786
#
17851787
statusbar = Statusbar(scr, main_win,
1786-
".:: <C-d> Exit <C-r> Rewind <F2> Save <F8> Pastebin ::.",
1788+
".:: <C-d> Exit <C-r> Rewind <%s> Save <%s> Pastebin ::." % (OPTS.save_key, OPTS.pastebin_key),
17871789
get_colpair('main'))
17881790

17891791
return main_win, statusbar
@@ -1903,6 +1905,8 @@ def main_curses(scr):
19031905

19041906
main_win, statusbar = init_wins(scr, cols)
19051907

1908+
curses.raw(True)
1909+
19061910
interpreter = Interpreter()
19071911

19081912
repl = Repl(main_win, interpreter, statusbar, idle)

bpython/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def loadini(struct, configfile):
4141
struct.hist_file = config.safeget('general', 'hist_file', '~/.pythonhist')
4242
struct.hist_length = config.safeget('general', 'hist_length', 100)
4343
struct.flush_output = config.safeget('general', 'flush_output', True)
44+
struct.pastebin_key = config.safeget('keyboard', 'pastebin', 'C-p')
45+
struct.save_key = config.safeget('keyboard', 'save', 'C-s')
4446
color_scheme_name = config.safeget('general', 'color_scheme', 'default')
4547

4648
if color_scheme_name == 'default':

bpython/keys.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
#
3+
# The MIT License
4+
#
5+
# Copyright (c) 2008 Simon de Vlieger
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
26+
import string
27+
28+
key_dispatch = {}
29+
30+
# fill dispatch with letters
31+
for c in string.lowercase:
32+
key_dispatch['C-%s' % c] = (chr(string.lowercase.index(c)+1), '^%s' % c.upper())
33+
34+
# fill dispatch with cool characters
35+
key_dispatch['C-['] = (chr(27), '^[')
36+
key_dispatch['C-\\'] = (chr(28), '^\\')
37+
key_dispatch['C-]'] = (chr(29), '^]')
38+
key_dispatch['C-^'] = (chr(30), '^^')
39+
key_dispatch['C-_'] = (chr(31), '^_')
40+
41+
# fill dispatch with function keys
42+
for x in xrange(1,13):
43+
key_dispatch['F%s' % str(x)] = 'KEY_F(%s)' % str(x)

sample.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ tab_length = 4
3030
# Color schemes should be put in ~/.bpython/
3131
# e.g. to use the theme ~/.bpython/foo.theme set color_scheme = foo
3232
# Leave blank or set to "default" to use the default theme
33-
color_scheme = dark
34-
33+
color_scheme = default
3534

35+
[keyboard]
36+
pastebin = F8
37+
save = C-s

0 commit comments

Comments
 (0)
X Tutup