Handle SIGINT more gracefully
This commit is contained in:
parent
2a8c8c0cbb
commit
6c8a4f8d9f
@ -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,24 +34,28 @@ def main() -> None:
|
|||||||
in_result: bytes = b''
|
in_result: bytes = b''
|
||||||
hostname = socket.gethostname().encode('utf-8')
|
hostname = socket.gethostname().encode('utf-8')
|
||||||
|
|
||||||
for line in p.stdout:
|
try:
|
||||||
line = osc_pat.sub(b'', line) # remove any existing hyperlinks
|
for line in p.stdout:
|
||||||
clean_line = sgr_pat.sub(b'', line).rstrip() # remove SGR formatting
|
line = osc_pat.sub(b'', line) # remove any existing hyperlinks
|
||||||
if not clean_line:
|
clean_line = sgr_pat.sub(b'', line).rstrip() # remove SGR formatting
|
||||||
in_result = b''
|
if not clean_line:
|
||||||
write(b'\n')
|
in_result = b''
|
||||||
continue
|
write(b'\n')
|
||||||
if in_result:
|
continue
|
||||||
m = num_pat.match(clean_line)
|
if in_result:
|
||||||
if m is not None:
|
m = num_pat.match(clean_line)
|
||||||
write_hyperlink(write, in_result, line, frag=m.group(1))
|
if m is not None:
|
||||||
else:
|
write_hyperlink(write, in_result, line, frag=m.group(1))
|
||||||
if line.strip():
|
|
||||||
path = quote_from_bytes(os.path.abspath(clean_line)).encode('utf-8')
|
|
||||||
in_result = b'file://' + hostname + path
|
|
||||||
write_hyperlink(write, in_result, line)
|
|
||||||
else:
|
else:
|
||||||
write(line)
|
if line.strip():
|
||||||
|
path = quote_from_bytes(os.path.abspath(clean_line)).encode('utf-8')
|
||||||
|
in_result = b'file://' + hostname + path
|
||||||
|
write_hyperlink(write, in_result, line)
|
||||||
|
else:
|
||||||
|
write(line)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
p.send_signal(signal.SIGINT)
|
||||||
|
p.stdout.close()
|
||||||
raise SystemExit(p.wait())
|
raise SystemExit(p.wait())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user