Skip to content

Commit f03f671

Browse files
committed
Encapsulate pythonrc script logic into a setup function and execute it
1 parent 7e11d06 commit f03f671

File tree

1 file changed

+78
-80
lines changed

1 file changed

+78
-80
lines changed

python_files/pythonrc.py

Lines changed: 78 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,83 @@
1-
import platform
2-
import sys
1+
def setup():
2+
import platform
3+
import sys
34

4-
if sys.platform != "win32":
5-
import readline
6-
7-
original_ps1 = ">>> "
8-
is_wsl = "microsoft-standard-WSL" in platform.release()
9-
10-
11-
class REPLHooks:
12-
def __init__(self):
13-
self.global_exit = None
14-
self.failure_flag = False
15-
self.original_excepthook = sys.excepthook
16-
self.original_displayhook = sys.displayhook
17-
sys.excepthook = self.my_excepthook
18-
sys.displayhook = self.my_displayhook
19-
20-
def my_displayhook(self, value):
21-
if value is None:
22-
self.failure_flag = False
23-
24-
self.original_displayhook(value)
25-
26-
def my_excepthook(self, type_, value, traceback):
27-
self.global_exit = value
28-
self.failure_flag = True
29-
30-
self.original_excepthook(type_, value, traceback)
31-
32-
33-
def get_last_command():
34-
# Get the last history item
35-
last_command = ""
365
if sys.platform != "win32":
37-
last_command = readline.get_history_item(readline.get_current_history_length())
38-
39-
return last_command
40-
6+
import readline
417

42-
class PS1:
43-
hooks = REPLHooks()
44-
sys.excepthook = hooks.my_excepthook
45-
sys.displayhook = hooks.my_displayhook
8+
original_ps1 = ">>> "
9+
is_wsl = "microsoft-standard-WSL" in platform.release()
4610

47-
# str will get called for every prompt with exit code to show success/failure
48-
def __str__(self):
49-
exit_code = int(bool(self.hooks.failure_flag))
50-
self.hooks.failure_flag = False
51-
# Guide following official VS Code doc for shell integration sequence:
52-
result = ""
53-
# For non-windows allow recent_command history.
11+
class REPLHooks:
12+
def __init__(self):
13+
self.global_exit = None
14+
self.failure_flag = False
15+
self.original_excepthook = sys.excepthook
16+
self.original_displayhook = sys.displayhook
17+
sys.excepthook = self.my_excepthook
18+
sys.displayhook = self.my_displayhook
19+
20+
def my_displayhook(self, value):
21+
if value is None:
22+
self.failure_flag = False
23+
self.original_displayhook(value)
24+
25+
def my_excepthook(self, type_, value, traceback):
26+
self.global_exit = value
27+
self.failure_flag = True
28+
self.original_excepthook(type_, value, traceback)
29+
30+
def get_last_command():
31+
# Get the last history item
32+
last_command = ""
5433
if sys.platform != "win32":
55-
result = "{soh}{command_executed}{command_line}{command_finished}{prompt_started}{stx}{prompt}{soh}{command_start}{stx}".format(
56-
soh="\001",
57-
stx="\002",
58-
command_executed="\x1b]633;C\x07",
59-
command_line="\x1b]633;E;" + str(get_last_command()) + "\x07",
60-
command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
61-
prompt_started="\x1b]633;A\x07",
62-
prompt=original_ps1,
63-
command_start="\x1b]633;B\x07",
64-
)
65-
else:
66-
result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
67-
command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
68-
prompt_started="\x1b]633;A\x07",
69-
prompt=original_ps1,
70-
command_start="\x1b]633;B\x07",
71-
command_executed="\x1b]633;C\x07",
72-
)
73-
74-
# 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)}"
75-
76-
return result
77-
78-
79-
if sys.platform != "win32" and (not is_wsl):
80-
sys.ps1 = PS1()
81-
82-
if sys.platform == "darwin":
83-
print("Cmd click to launch VS Code Native REPL")
84-
else:
85-
print("Ctrl click to launch VS Code Native REPL")
34+
last_command = readline.get_history_item(readline.get_current_history_length())
35+
return last_command
36+
37+
class PS1:
38+
hooks = REPLHooks()
39+
sys.excepthook = hooks.my_excepthook
40+
sys.displayhook = hooks.my_displayhook
41+
42+
# str will get called for every prompt with exit code to show success/failure
43+
def __str__(self):
44+
exit_code = int(bool(self.hooks.failure_flag))
45+
self.hooks.failure_flag = False
46+
# Guide following official VS Code doc for shell integration sequence:
47+
result = ""
48+
# For non-windows allow recent_command history.
49+
if sys.platform != "win32":
50+
result = "{soh}{command_executed}{command_line}{command_finished}{prompt_started}{stx}{prompt}{soh}{command_start}{stx}".format(
51+
soh="\001",
52+
stx="\002",
53+
command_executed="\x1b]633;C\x07",
54+
command_line="\x1b]633;E;" + str(get_last_command()) + "\x07",
55+
command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
56+
prompt_started="\x1b]633;A\x07",
57+
prompt=original_ps1,
58+
command_start="\x1b]633;B\x07",
59+
)
60+
else:
61+
result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
62+
command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
63+
prompt_started="\x1b]633;A\x07",
64+
prompt=original_ps1,
65+
command_start="\x1b]633;B\x07",
66+
command_executed="\x1b]633;C\x07",
67+
)
68+
69+
# 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)}"
70+
71+
return result
72+
73+
if sys.platform != "win32" and (not is_wsl):
74+
sys.ps1 = PS1()
75+
76+
if sys.platform == "darwin":
77+
print("Cmd click to launch VS Code Native REPL")
78+
else:
79+
print("Ctrl click to launch VS Code Native REPL")
80+
81+
82+
setup()
83+
del setup

0 commit comments

Comments
 (0)