forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathpythonrc.py
More file actions
107 lines (85 loc) · 3.58 KB
/
Copy pathpythonrc.py
File metadata and controls
107 lines (85 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import platform
import sys
from enum import Enum
if sys.platform != "win32":
import readline
original_ps1 = ">>> "
is_wsl = "microsoft-standard-WSL" in platform.release()
class ShellIntegrationSequence(str, Enum):
SOH = "\001"
STX = "\002"
COMMAND_EXECUTED = "\x1b]633;C\x07"
COMMAND_LINE = "\x1b]633;E;"
COMMAND_FINISHED = "\x1b]633;D;"
PROMPT_STARTED = "\x1b]633;A\x07"
COMMAND_START = "\x1b]633;B\x07"
TERMINATOR = "\x07"
def __str__(self):
return self.value
class REPLHooks:
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)
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 = REPLHooks()
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 = int(bool(self.hooks.failure_flag))
self.hooks.failure_flag = False
# Guide following official VS Code doc for shell integration sequence:
result = ""
# For non-windows allow recent_command history.
if sys.platform != "win32":
result = "{soh}{command_executed}{command_line}{command_finished}{prompt_started}{stx}{prompt}{soh}{command_start}{stx}".format(
soh=ShellIntegrationSequence.SOH,
stx=ShellIntegrationSequence.STX,
command_executed=ShellIntegrationSequence.COMMAND_EXECUTED,
command_line=ShellIntegrationSequence.COMMAND_LINE
+ str(get_last_command())
+ ShellIntegrationSequence.TERMINATOR,
command_finished=ShellIntegrationSequence.COMMAND_FINISHED
+ str(exit_code)
+ ShellIntegrationSequence.TERMINATOR,
prompt_started=ShellIntegrationSequence.PROMPT_STARTED,
prompt=original_ps1,
command_start=ShellIntegrationSequence.COMMAND_START,
)
else:
result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
command_finished=ShellIntegrationSequence.COMMAND_FINISHED
+ str(exit_code)
+ ShellIntegrationSequence.TERMINATOR,
prompt_started=ShellIntegrationSequence.PROMPT_STARTED,
prompt=original_ps1,
command_start=ShellIntegrationSequence.COMMAND_START,
command_executed=ShellIntegrationSequence.COMMAND_EXECUTED,
)
return result
def __repr__(self):
return "<Custom PS1 for VS Code Python Shell Integration>"
if sys.platform != "win32" and (not is_wsl):
sys.ps1 = PS1()
if sys.platform == "darwin":
print("Cmd click to launch VS Code Native REPL (https://aka.ms/python-native-repl)")
else:
print("Ctrl click to launch VS Code Native REPL (https://aka.ms/python-native-repl)")