Skip to content

Commit

Permalink
Add trepan3k --edit-mode option (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky authored Nov 10, 2024
1 parent aaefc23 commit 183661d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
23 changes: 16 additions & 7 deletions trepan/inout/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@
from trepan.inout import base as Mbase

try:
from prompt_toolkit import PromptSession, HTML
from prompt_toolkit.styles import Style
from prompt_toolkit import HTML, PromptSession
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.history import FileHistory
from prompt_toolkit.styles import Style
except:
PromptSession = lambda history: None
FileHistory = lambda history: None
HTML = lambda string: string


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

def __init__(self, inp=None, opts=None):

if opts and opts.get("readline") == "prompt_toolkit":
self.session = PromptSession(history=FileHistory(opts.get("histfile")))
def __init__(self, inp=None, opts=dict()):

self.edit_mode = opts.get("edit_mode", "emacs")
if opts.get("readline") == "prompt_toolkit":
prompt_editing_mode = (
EditingMode.EMACS if self.edit_mode == "emacs" else EditingMode.VI
)
self.session = PromptSession(
editing_mode=prompt_editing_mode,
enable_history_search=True,
history=FileHistory(opts.get("histfile")),
)
self.input = self.session.input
self.session.enable_history_search = True
self.line_edit = True
self.closed = False
self.use_raw = False
Expand Down
22 changes: 21 additions & 1 deletion trepan/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
# action="store", type='string',
# help="Write debugger's error output "
# + "(stderr) to FILE")
optparser.add_option(
"--edit-mode",
default="emacs",
dest="edit_mode",
type="string",
help='input edit mode. This should be either "emacs" or "vi"',
)

optparser.add_option(
"-e",
"--exec",
Expand Down Expand Up @@ -347,7 +355,7 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
dest="use_prompt_toolkit",
action="store_true",
default=True,
help="Try using prompt_toolkit",
help="Try using prompt_toolkit. This take precedence over the --gnu-readline option",
)
optparser.add_option(
"--no-prompt-toolkit",
Expand All @@ -367,7 +375,18 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
optparser.disable_interspersed_args()

sys.argv = list(sys_argv)

# Here is where we *parse* arguments
(opts, sys.argv) = optparser.parse_args(sys_argv[1:])

if opts.edit_mode not in ("vi", "emacs"):
sys.stderr.write(
'Option --editmode should be either "emacs" or "vi"; assuming "emacs".\n'
f'Got: "{opts.edit_mode}".\n'
)
opts.edit_mode = "emacs"


if hasattr(opts, "use_prompt_toolkit") and opts.use_prompt_toolkit:
readline = "prompt_toolkit"
elif hasattr(opts, "use_gnu_readline") and opts.use_gnu_readline:
Expand All @@ -380,6 +399,7 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None):
"interface_opts": {
"readline": readline,
"debugger_name": "trepan3k",
"edit_mode": opts.edit_mode,
}
}

Expand Down
10 changes: 5 additions & 5 deletions trepan/processor/command/skip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2009, 2013, 2015, 2020 Rocky Bernstein
# Copyright (C) 2009, 2013, 2015, 2020, 2024 Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -17,7 +17,7 @@
# Our local modules
from trepan.processor.command.base_cmd import DebuggerCommand
from trepan.processor.cmdproc import print_location
from trepan.lib import bytecode as Mbytecode
from trepan.lib.bytecode import next_linestart


class SkipCommand(DebuggerCommand):
Expand Down Expand Up @@ -53,14 +53,14 @@ def run(self, args):
if len(args) == 1:
count = 1
else:
msg = "skip: expecting a number, got %s." % args[1]
msg = f"skip: expecting a number, got {args[1]}."
count = self.proc.get_an_int(args[1], msg)
pass
co = self.proc.curframe.f_code
offset = self.proc.curframe.f_lasti
if count is None:
return False
lineno = Mbytecode.next_linestart(co, offset, count)
lineno = next_linestart(co, offset, count)

if lineno < 0:
self.errmsg("No next line found")
Expand All @@ -76,7 +76,7 @@ def run(self, args):
)
print_location(self.proc)
except ValueError as e:
self.errmsg("skip failed: %s" % e)
self.errmsg(f"skip failed: {e}")
return False

pass
Expand Down

0 comments on commit 183661d

Please sign in to comment.