Skip to content

Commit 0afe60c

Browse files
committed
Read me fix
1 parent d462cc4 commit 0afe60c

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

README.md

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ from processes running on the same host as VSCode.
3838
### Python example
3939

4040
```py
41+
import json
4142
import requests
4243
from pathlib import Path
4344
from tempfile import gettempdir
@@ -67,39 +68,45 @@ that you have a function `actions.key` that presses the given key (eg
6768
[talon](https://talonvoice.com/)):
6869

6970
```py
70-
port_file_path = Path(gettempdir()) / "vscode-port"
71-
original_contents = port_file_path.read_text()
72-
73-
# Issue command to VSCode telling it to update the port file. Because only
74-
# the active VSCode instance will accept keypresses, we can be sure that
75-
# the active VSCode instance will be the one to write the port.
76-
if is_mac:
77-
actions.key("cmd-shift-alt-p")
78-
else:
79-
actions.key("ctrl-shift-alt-p")
80-
81-
# Wait for the VSCode instance to update the port file. This generally
82-
# happens within the first millisecond, but we give it 3 seconds just in
83-
# case.
84-
start_time = time.monotonic()
71+
import json
72+
import requests
73+
import time
74+
from pathlib import Path
75+
from tempfile import gettempdir
76+
77+
port_file_path = Path(gettempdir()) / "vscode-port"
78+
original_contents = port_file_path.read_text()
79+
80+
# Issue command to VSCode telling it to update the port file. Because only
81+
# the active VSCode instance will accept keypresses, we can be sure that
82+
# the active VSCode instance will be the one to write the port.
83+
if is_mac:
84+
actions.key("cmd-shift-alt-p")
85+
else:
86+
actions.key("ctrl-shift-alt-p")
87+
88+
# Wait for the VSCode instance to update the port file. This generally
89+
# happens within the first millisecond, but we give it 3 seconds just in
90+
# case.
91+
start_time = time.monotonic()
92+
new_contents = port_file_path.read_text()
93+
while original_contents == new_contents:
94+
time.sleep(0.001)
95+
if time.monotonic() - start_time > 3.0:
96+
raise Exception("Timed out waiting for VSCode to update port file")
8597
new_contents = port_file_path.read_text()
86-
while original_contents == new_contents:
87-
time.sleep(0.001)
88-
if time.monotonic() - start_time > 3.0:
89-
raise Exception("Timed out waiting for VSCode to update port file")
90-
new_contents = port_file_path.read_text()
91-
92-
port = json.loads(new_contents)["port"]
93-
94-
response = requests.post(
95-
f"http://localhost:{port}/execute-command",
96-
json={
97-
"commandId": "some-command-id",
98-
"args": ["some-argument"],
99-
},
100-
timeout=(0.05, 3.05),
101-
)
102-
response.raise_for_status()
98+
99+
port = json.loads(new_contents)["port"]
100+
101+
response = requests.post(
102+
f"http://localhost:{port}/execute-command",
103+
json={
104+
"commandId": "some-command-id",
105+
"args": ["some-argument"],
106+
},
107+
timeout=(0.05, 3.05),
108+
)
109+
response.raise_for_status()
103110
```
104111

105112
## Known issues

0 commit comments

Comments
 (0)