Skip to content

Commit 183661d

Browse files
authored
Add trepan3k --edit-mode option (#67)
1 parent aaefc23 commit 183661d

3 files changed

Lines changed: 42 additions & 13 deletions

File tree

trepan/inout/input.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,34 @@
2323
from trepan.inout import base as Mbase
2424

2525
try:
26-
from prompt_toolkit import PromptSession, HTML
27-
from prompt_toolkit.styles import Style
26+
from prompt_toolkit import HTML, PromptSession
27+
from prompt_toolkit.enums import EditingMode
2828
from prompt_toolkit.history import FileHistory
29+
from prompt_toolkit.styles import Style
2930
except:
3031
PromptSession = lambda history: None
3132
FileHistory = lambda history: None
3233
HTML = lambda string: string
3334

35+
3436
class DebuggerUserInput(Mbase.DebuggerInputBase):
3537
"""Debugger input connected to what we think of as a end-user input
3638
as opposed to a relay mechanism to another process. Input could be
3739
interactive terminal, but it might be file input."""
3840

39-
def __init__(self, inp=None, opts=None):
40-
41-
if opts and opts.get("readline") == "prompt_toolkit":
42-
self.session = PromptSession(history=FileHistory(opts.get("histfile")))
41+
def __init__(self, inp=None, opts=dict()):
42+
43+
self.edit_mode = opts.get("edit_mode", "emacs")
44+
if opts.get("readline") == "prompt_toolkit":
45+
prompt_editing_mode = (
46+
EditingMode.EMACS if self.edit_mode == "emacs" else EditingMode.VI
47+
)
48+
self.session = PromptSession(
49+
editing_mode=prompt_editing_mode,
50+
enable_history_search=True,
51+
history=FileHistory(opts.get("histfile")),
52+
)
4353
self.input = self.session.input
44-
self.session.enable_history_search = True
4554
self.line_edit = True
4655
self.closed = False
4756
self.use_raw = False

trepan/options.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
173173
# action="store", type='string',
174174
# help="Write debugger's error output "
175175
# + "(stderr) to FILE")
176+
optparser.add_option(
177+
"--edit-mode",
178+
default="emacs",
179+
dest="edit_mode",
180+
type="string",
181+
help='input edit mode. This should be either "emacs" or "vi"',
182+
)
183+
176184
optparser.add_option(
177185
"-e",
178186
"--exec",
@@ -347,7 +355,7 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
347355
dest="use_prompt_toolkit",
348356
action="store_true",
349357
default=True,
350-
help="Try using prompt_toolkit",
358+
help="Try using prompt_toolkit. This take precedence over the --gnu-readline option",
351359
)
352360
optparser.add_option(
353361
"--no-prompt-toolkit",
@@ -367,7 +375,18 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
367375
optparser.disable_interspersed_args()
368376

369377
sys.argv = list(sys_argv)
378+
379+
# Here is where we *parse* arguments
370380
(opts, sys.argv) = optparser.parse_args(sys_argv[1:])
381+
382+
if opts.edit_mode not in ("vi", "emacs"):
383+
sys.stderr.write(
384+
'Option --editmode should be either "emacs" or "vi"; assuming "emacs".\n'
385+
f'Got: "{opts.edit_mode}".\n'
386+
)
387+
opts.edit_mode = "emacs"
388+
389+
371390
if hasattr(opts, "use_prompt_toolkit") and opts.use_prompt_toolkit:
372391
readline = "prompt_toolkit"
373392
elif hasattr(opts, "use_gnu_readline") and opts.use_gnu_readline:
@@ -380,6 +399,7 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
380399
"interface_opts": {
381400
"readline": readline,
382401
"debugger_name": "trepan3k",
402+
"edit_mode": opts.edit_mode,
383403
}
384404
}
385405

trepan/processor/command/skip.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2009, 2013, 2015, 2020 Rocky Bernstein
2+
# Copyright (C) 2009, 2013, 2015, 2020, 2024 Rocky Bernstein
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
1717
# Our local modules
1818
from trepan.processor.command.base_cmd import DebuggerCommand
1919
from trepan.processor.cmdproc import print_location
20-
from trepan.lib import bytecode as Mbytecode
20+
from trepan.lib.bytecode import next_linestart
2121

2222

2323
class SkipCommand(DebuggerCommand):
@@ -53,14 +53,14 @@ def run(self, args):
5353
if len(args) == 1:
5454
count = 1
5555
else:
56-
msg = "skip: expecting a number, got %s." % args[1]
56+
msg = f"skip: expecting a number, got {args[1]}."
5757
count = self.proc.get_an_int(args[1], msg)
5858
pass
5959
co = self.proc.curframe.f_code
6060
offset = self.proc.curframe.f_lasti
6161
if count is None:
6262
return False
63-
lineno = Mbytecode.next_linestart(co, offset, count)
63+
lineno = next_linestart(co, offset, count)
6464

6565
if lineno < 0:
6666
self.errmsg("No next line found")
@@ -76,7 +76,7 @@ def run(self, args):
7676
)
7777
print_location(self.proc)
7878
except ValueError as e:
79-
self.errmsg("skip failed: %s" % e)
79+
self.errmsg(f"skip failed: {e}")
8080
return False
8181

8282
pass

0 commit comments

Comments
 (0)