forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use shell integration to denote success/failure (microsoft#22487)
Resolves: microsoft#22486 Use shell integration to denote success/failure in Python REPL launched from VS Code. This would mean having the blue or red decorators based on whether or not user's command succeeded.
- Loading branch information
1 parent
f6e1338
commit eb96141
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import sys | ||
|
||
original_ps1 = ">>>" | ||
|
||
|
||
class repl_hooks: | ||
def __init__(self): | ||
self.global_exit = None | ||
self.failure_flag = False | ||
self.original_excepthook = sys.excepthook | ||
self.original_displayhook = sys.displayhook | ||
sys.excepthook = self.my_excepthook | ||
sys.displayhook = self.my_displayhook | ||
|
||
def my_displayhook(self, value): | ||
if value is None: | ||
self.failure_flag = False | ||
|
||
self.original_displayhook(value) | ||
|
||
def my_excepthook(self, type, value, traceback): | ||
self.global_exit = value | ||
self.failure_flag = True | ||
|
||
self.original_excepthook(type, value, traceback) | ||
|
||
|
||
class ps1: | ||
hooks = repl_hooks() | ||
sys.excepthook = hooks.my_excepthook | ||
sys.displayhook = hooks.my_displayhook | ||
|
||
# str will get called for every prompt with exit code to show success/failure | ||
def __str__(self): | ||
exit_code = 0 | ||
if self.hooks.failure_flag: | ||
exit_code = 1 | ||
else: | ||
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) + "0\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}0{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}" | ||
|
||
return result | ||
|
||
|
||
sys.ps1 = ps1() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import importlib | ||
from unittest.mock import Mock | ||
|
||
import pythonrc | ||
|
||
|
||
def test_decoration_success(): | ||
importlib.reload(pythonrc) | ||
ps1 = pythonrc.ps1() | ||
|
||
ps1.hooks.failure_flag = False | ||
result = str(ps1) | ||
assert result == "\x1b]633;D;00\x07\x1b]633;A\x07>>>\x1b]633;B\x07\x1b]633;C\x07" | ||
|
||
|
||
def test_decoration_failure(): | ||
importlib.reload(pythonrc) | ||
ps1 = pythonrc.ps1() | ||
|
||
ps1.hooks.failure_flag = True | ||
result = str(ps1) | ||
|
||
assert result == "\x1b]633;D;10\x07\x1b]633;A\x07>>>\x1b]633;B\x07\x1b]633;C\x07" | ||
|
||
|
||
def test_displayhook_call(): | ||
importlib.reload(pythonrc) | ||
pythonrc.ps1() | ||
mock_displayhook = Mock() | ||
|
||
hooks = pythonrc.repl_hooks() | ||
hooks.original_displayhook = mock_displayhook | ||
|
||
hooks.my_displayhook("mock_value") | ||
|
||
mock_displayhook.assert_called_once_with("mock_value") | ||
|
||
|
||
def test_excepthook_call(): | ||
importlib.reload(pythonrc) | ||
pythonrc.ps1() | ||
mock_excepthook = Mock() | ||
|
||
hooks = pythonrc.repl_hooks() | ||
hooks.original_excepthook = mock_excepthook | ||
|
||
hooks.my_excepthook("mock_type", "mock_value", "mock_traceback") | ||
mock_excepthook.assert_called_once_with("mock_type", "mock_value", "mock_traceback") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters