Skip to content

Commit 4d19eeb

Browse files
Merge branch 'keys-help'
Conflicts: bpython/curtsiesfrontend/repl.py
2 parents cefcb25 + 5a19864 commit 4d19eeb

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

bpython/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def loadini(struct, configfile):
6969
'down_one_line': 'C-n',
7070
'exit': '',
7171
'external_editor': 'F7',
72+
'edit_current_block': 'C-x',
73+
'help': 'F1',
7274
'last_output': 'F9',
7375
'pastebin': 'F8',
7476
'save': 'C-s',
@@ -127,7 +129,9 @@ def loadini(struct, configfile):
127129
struct.delete_key = config.get('keyboard', 'delete')
128130
struct.exit_key = config.get('keyboard', 'exit')
129131
struct.last_output_key = config.get('keyboard', 'last_output')
132+
struct.edit_current_block_key = config.get('keyboard', 'edit_current_block')
130133
struct.external_editor_key = config.get('keyboard', 'external_editor')
134+
struct.help_key = config.get('keyboard', 'help')
131135

132136
struct.pastebin_confirm = config.getboolean('general', 'pastebin_confirm')
133137
struct.pastebin_private = config.getboolean('general', 'pastebin_private')

bpython/curtsiesfrontend/repl.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
translations.init()
2525
from bpython.translations import _
2626
from bpython._py3compat import py3
27+
import bpython
2728

2829
from curtsies import FSArray, fmtstr, FmtStr, Termmode
2930
from curtsies.bpythonparse import parse as bpythonparse
3031
from curtsies.bpythonparse import func_for_letter, color_for_letter
3132
from curtsies import fmtfuncs
3233
from curtsies import events
34+
import curtsies
3335

3436
from bpython.curtsiesfrontend.manual_readline import char_sequences as rl_char_sequences
3537
from bpython.curtsiesfrontend.manual_readline import get_updated_char_sequences
@@ -44,6 +46,24 @@
4446

4547
logger = logging.getLogger(__name__)
4648

49+
HELP_MESSAGE = """Thanks for using bpython!
50+
51+
See http://bpython-interpreter.org/ for info, http://docs.bpython-interpreter.org/ for docs, and https://github.com/bpython/bpython for source.
52+
Please report issues at https://github.com/bpython/bpython/issues
53+
54+
Try using undo ({config.undo_key})!
55+
Edit the current line ({config.edit_current_block_key}) or the entire session ({config.external_editor_key}) in an external editor! (currently {config.editor})
56+
Save sessions ({config.save_key}) or post them to pastebins ({config.pastebin_key})! Current pastebin helper: {config.pastebin_helper}
57+
Re-execute the current session and reload all modules to test out changes to a module!
58+
Toggle auto-reload mode to re-execute the current session when a module you've imported is modified!
59+
60+
Use bpython-curtsies -i your_script.py to run a file in interactive mode (interpreter in namespace of script).
61+
Use bpython-curtsies -t your_script.py to paste in the contents of a file, as though you typed them.
62+
63+
Use a config file at {config_file_location} to customize keys and behavior of bpython.
64+
See {example_config_url} for an example config file.
65+
"""
66+
4767
class FakeStdin(object):
4868
"""Stdin object user code references so sys.stdin.read() asked user for interactive input"""
4969
def __init__(self, coderunner, repl):
@@ -202,7 +222,7 @@ def __init__(self, locals_=None, config=None,
202222
if interp is None:
203223
interp = code.InteractiveInterpreter(locals=locals_)
204224
if banner is None:
205-
banner = _('welcome to bpython')
225+
banner = _('Welcome to bpython! Press <%s> for help.') % config.help_key
206226
config.autocomplete_mode = autocomplete.SIMPLE # only one implemented currently
207227
if config.cli_suggestion_width <= 0 or config.cli_suggestion_width > 1:
208228
config.cli_suggestion_width = 1
@@ -218,7 +238,7 @@ def smarter_request_refresh():
218238
self.get_term_hw = get_term_hw
219239
self.get_cursor_vertical_diff = get_cursor_vertical_diff
220240

221-
self.status_bar = StatusBar(banner if config.curtsies_fill_terminal else '', _(
241+
self.status_bar = StatusBar(banner, _(
222242
" <%s> Rewind <%s> Save <%s> Pastebin <%s> Editor"
223243
) % (config.undo_key, config.save_key, config.pastebin_key, config.external_editor_key),
224244
refresh_request=self.request_refresh
@@ -403,10 +423,9 @@ def process_event(self, e):
403423
else:
404424
if self.config.highlight_show_source:
405425
source = format(PythonLexer().get_tokens(source), TerminalFormatter())
406-
with tempfile.NamedTemporaryFile() as tmp:
407-
tmp.write(source)
408-
tmp.flush()
409-
self.focus_on_subprocess(['less', '-R', tmp.name])
426+
self.pager(source)
427+
elif e in key_dispatch[self.config.help_key]:
428+
self.pager(self.help_text())
410429
elif e in key_dispatch[self.config.suspend_key]:
411430
raise SystemExit()
412431
elif e in ("<Ctrl-d>",):
@@ -437,7 +456,8 @@ def process_event(self, e):
437456
elif e in key_dispatch[self.config.external_editor_key]:
438457
self.send_session_to_external_editor()
439458
self.rl_history.reset()
440-
elif e in ["<Ctrl-x>"]:
459+
#TODO add PAD keys hack as in bpython.cli
460+
elif e in key_dispatch[self.config.edit_current_block_key]:
441461
self.send_current_block_to_external_editor()
442462
elif e in ["<ESC>"]: #ESC
443463
pass
@@ -1025,6 +1045,40 @@ def focus_on_subprocess(self, args):
10251045
finally:
10261046
signal.signal(signal.SIGWINCH, prev_sigwinch_handler)
10271047

1048+
def pager(self, text):
1049+
command = os.environ.get('PAGER', 'less -r').split()
1050+
with tempfile.NamedTemporaryFile() as tmp:
1051+
tmp.write(text)
1052+
tmp.flush()
1053+
self.focus_on_subprocess(command + [tmp.name])
1054+
1055+
def help_text(self):
1056+
return self.version_help_text() + '\n' + self.key_help_text()
1057+
1058+
def version_help_text(self):
1059+
return (('bpython-curtsies version %s' % bpython.__version__) + ' ' +
1060+
('using curtsies version %s' % curtsies.__version__) + '\n' +
1061+
HELP_MESSAGE.format(config_file_location=default_config_path(),
1062+
example_config_url='https://raw.githubusercontent.com/bpython/bpython/master/sample-config',
1063+
config=self.config)
1064+
)
1065+
1066+
def key_help_text(self):
1067+
NOT_IMPLEMENTED = ['suspend', 'cut to buffer', 'search', 'last output', 'yank from buffer', 'cut to buffer']
1068+
pairs = []
1069+
pairs.append(['complete history suggestion', 'right arrow at end of line'])
1070+
pairs.append(['previous match with current line', 'up arrow'])
1071+
for functionality, key in [(attr[:-4].replace('_', ' '), getattr(self.config, attr))
1072+
for attr in self.config.__dict__
1073+
if attr.endswith('key')]:
1074+
if functionality in NOT_IMPLEMENTED: key = "Not Implemented"
1075+
if key == '': key = 'Disabled'
1076+
1077+
pairs.append([functionality, key])
1078+
1079+
max_func = max(len(func) for func, key in pairs)
1080+
return '\n'.join('%s : %s' % (func.rjust(max_func), key) for func, key in pairs)
1081+
10281082
def is_nop(char):
10291083
return unicodedata.category(unicode(char)) == 'Cc'
10301084

0 commit comments

Comments
 (0)