Skip to content

Commit

Permalink
Allow run recent command for REPL using shell integration (microsoft#…
Browse files Browse the repository at this point in the history
…22720)

Resolves: microsoft#22647

Allow access to recent command "Terminal: Run recent command:" for
Python REPL users.
Due to GNU readline, Mac and Linux Users will now be able to see their
REPL command history.

Blocked on recent history support for Windows:
- Would have to go through VS Code to pick up the command or via native
VS Code REPL.
  • Loading branch information
anthonykim1 authored Jan 22, 2024
1 parent 1eaf5a3 commit d10b2f7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@
"@types/xml2js": "^0.4.2",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"@vscode/test-electron": "^2.3.4",
"@vscode/test-electron": "^2.3.8",
"@vscode/vsce": "^2.18.0",
"bent": "^7.3.12",
"chai": "^4.1.2",
Expand Down Expand Up @@ -1643,13 +1643,13 @@
"typescript": "4.5.5",
"uuid": "^8.3.2",
"webpack": "^5.76.0",
"worker-loader": "^3.0.8",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-cli": "^4.9.2",
"webpack-fix-default-import-plugin": "^1.0.3",
"webpack-merge": "^5.8.0",
"webpack-node-externals": "^3.0.0",
"webpack-require-from": "^1.8.6",
"worker-loader": "^3.0.8",
"yargs": "^15.3.1"
}
}
41 changes: 33 additions & 8 deletions pythonFiles/pythonrc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import sys

if sys.platform != "win32":
import readline

original_ps1 = ">>> "


Expand All @@ -25,6 +28,15 @@ def my_excepthook(self, type, value, traceback):
self.original_excepthook(type, value, traceback)


def get_last_command():
# Get the last history item
last_command = ""
if sys.platform != "win32":
last_command = readline.get_history_item(readline.get_current_history_length())

return last_command


class ps1:
hooks = repl_hooks()
sys.excepthook = hooks.my_excepthook
Expand All @@ -39,14 +51,27 @@ 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 = ""
# For non-windows allow recent_command history.
if sys.platform != "win32":
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",
)
else:
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)}"

return result

Expand Down
19 changes: 15 additions & 4 deletions pythonFiles/tests/test_shell_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib
import sys
from unittest.mock import Mock

import pythonrc


Expand All @@ -10,7 +10,13 @@ def test_decoration_success():

ps1.hooks.failure_flag = False
result = str(ps1)
assert result == "\x1b]633;D;0\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07"
if sys.platform != "win32":
assert (
result
== "\x1b]633;D;0\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07\x1b]633;E;None\x07"
)
else:
pass


def test_decoration_failure():
Expand All @@ -19,8 +25,13 @@ def test_decoration_failure():

ps1.hooks.failure_flag = True
result = str(ps1)

assert result == "\x1b]633;D;1\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07"
if sys.platform != "win32":
assert (
result
== "\x1b]633;D;1\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07\x1b]633;E;None\x07"
)
else:
pass


def test_displayhook_call():
Expand Down

0 comments on commit d10b2f7

Please sign in to comment.