Handle SIGINT more gracefully

This commit is contained in:
Kovid Goyal 2020-09-25 18:55:43 +05:30
parent 2a8c8c0cbb
commit 6c8a4f8d9f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -4,6 +4,7 @@
import os import os
import re import re
import signal
import socket import socket
import subprocess import subprocess
import sys import sys
@ -12,14 +13,11 @@ from urllib.parse import quote_from_bytes
def write_hyperlink(write: Callable[[bytes], None], url: bytes, line: bytes, frag: bytes = b'') -> None: def write_hyperlink(write: Callable[[bytes], None], url: bytes, line: bytes, frag: bytes = b'') -> None:
write(b'\033]8;;') text = b'\033]8;;' + url
write(url)
if frag: if frag:
write(b'#') text += b'#' + frag
write(frag) text += b'\033\\' + line + b'\033]8;;\033\\'
write(b'\033\\') write(text)
write(line)
write(b'\033]8;;\033\\')
def main() -> None: def main() -> None:
@ -36,6 +34,7 @@ def main() -> None:
in_result: bytes = b'' in_result: bytes = b''
hostname = socket.gethostname().encode('utf-8') hostname = socket.gethostname().encode('utf-8')
try:
for line in p.stdout: for line in p.stdout:
line = osc_pat.sub(b'', line) # remove any existing hyperlinks line = osc_pat.sub(b'', line) # remove any existing hyperlinks
clean_line = sgr_pat.sub(b'', line).rstrip() # remove SGR formatting clean_line = sgr_pat.sub(b'', line).rstrip() # remove SGR formatting
@ -54,6 +53,9 @@ def main() -> None:
write_hyperlink(write, in_result, line) write_hyperlink(write, in_result, line)
else: else:
write(line) write(line)
except KeyboardInterrupt:
p.send_signal(signal.SIGINT)
p.stdout.close()
raise SystemExit(p.wait()) raise SystemExit(p.wait())