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
|
#!/usr/bin/env python3
import json
import sys
import subprocess
from urllib.parse import urlencode, quote
import socket
import argparse
from argparse import RawDescriptionHelpFormatter
EXTENSION = "irth/dmenu"
COMMAND = "index"
parser = argparse.ArgumentParser(
prog="raycast_dmenu",
description="""dmenu-like raycast extension
Provide option list as stdin, the stdout will contain the chosen option.
If no option was chosen, the program will exit with the return code set to 1.""",
formatter_class=RawDescriptionHelpFormatter,
)
parser.add_argument("-p", "--prompt", help="search bar placeholder text")
args = parser.parse_args()
elements = list(sys.stdin)
server = socket.socket()
server.bind(("127.0.0.1", 0))
server.listen(0)
host, port = server.getsockname()
arguments = {
"host": host,
"port": str(port),
}
if args.prompt is not None:
arguments["prompt"] = args.prompt
query = urlencode({"arguments": json.dumps(arguments)}, quote_via=quote)
url = f"raycast://extensions/{EXTENSION}/{COMMAND}?{query}"
subprocess.run(["open", url], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
(conn, _) = server.accept()
conn.send(f"{len(elements)}\n".encode("utf-8"))
for el in elements:
conn.send(f"{el.strip()}\n".encode("utf-8"))
result = b""
while len(data := conn.recv(1024)) != 0:
result += data
final_result = result.decode("utf-8").strip()
if final_result == "":
exit(1)
print(final_result)
|