From dad848053bc78b295882dfdb54a52743e5188cc6 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Fri, 5 Jan 2024 14:07:19 -0500 Subject: [PATCH] allow run recent command --- pythonFiles/pythonrc.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pythonFiles/pythonrc.py b/pythonFiles/pythonrc.py index 616a59e21203..40a8d3854c55 100644 --- a/pythonFiles/pythonrc.py +++ b/pythonFiles/pythonrc.py @@ -1,4 +1,5 @@ import sys +import readline original_ps1 = ">>> " @@ -25,6 +26,12 @@ def my_excepthook(self, type, value, traceback): self.original_excepthook(type, value, traceback) +def get_last_command(): + # Get the last history item + last_command = readline.get_history_item(readline.get_current_history_length()) + return last_command + + class ps1: hooks = repl_hooks() sys.excepthook = hooks.my_excepthook @@ -39,14 +46,15 @@ def __str__(self): exit_code = 0 # Guide following official VS Code doc for shell integration sequence: - # result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format( - # command_finished="\x1b]633;D;" + str(exit_code) + "\x07", - # prompt_started="\x1b]633;A\x07", - # prompt=original_ps1, - # command_start="\x1b]633;B\x07", - # command_executed="\x1b]633;C\x07", - # ) - result = f"{chr(27)}]633;D;{exit_code}{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}" + result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}{command_line}".format( + command_finished="\x1b]633;D;" + str(exit_code) + "\x07", + prompt_started="\x1b]633;A\x07", + prompt=original_ps1, + command_start="\x1b]633;B\x07", + command_executed="\x1b]633;C\x07", + command_line="\x1b]633;E;" + str(get_last_command()) + "\x07", + ) + # result = f"{chr(27)}]633;D;{exit_code}{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}" return result