2020-10-04 13:15:27 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
2020-10-14 18:38:41 +00:00
|
|
|
from pathlib import Path
|
2021-04-18 15:06:47 +00:00
|
|
|
from typing import Iterable, NoReturn
|
2020-10-14 18:38:41 +00:00
|
|
|
|
|
|
|
if os.name == "posix":
|
2021-04-26 07:11:02 +00:00
|
|
|
DOTFILES_CONFIG_DIR: Path = Path.home() / ".config" / "dotfiles"
|
|
|
|
DOTFILES_CACHE_DIR: Path = Path.home() / ".cache" / "dotfiles"
|
2020-10-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
2021-04-18 15:06:47 +00:00
|
|
|
def platform_not_supported_error() -> NoReturn:
|
2021-04-26 07:11:02 +00:00
|
|
|
raise Exception("platform '{}' is not supported!".format(sys.platform))
|
2020-10-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
2021-04-26 07:07:31 +00:00
|
|
|
def run_chooser(choices: Iterable[str], prompt: str = None, async_read: bool = False) -> int:
|
2021-04-26 07:11:02 +00:00
|
|
|
supports_result_index = True
|
|
|
|
if os.isatty(sys.stderr.fileno()):
|
|
|
|
process_args = [
|
|
|
|
"fzf",
|
|
|
|
"--with-nth=2..",
|
|
|
|
"--height=50%",
|
|
|
|
"--reverse",
|
|
|
|
"--tiebreak=index",
|
|
|
|
]
|
|
|
|
supports_result_index = False
|
|
|
|
elif sys.platform == "darwin":
|
|
|
|
process_args = ["choose", "-i"]
|
|
|
|
elif os.name == "posix":
|
|
|
|
process_args = ["rofi", "-dmenu", "-i", "-format", "i"]
|
|
|
|
if prompt is not None:
|
|
|
|
process_args += ["-p", prompt]
|
|
|
|
if async_read:
|
|
|
|
process_args += ["-async-pre-read", "0"]
|
|
|
|
else:
|
|
|
|
platform_not_supported_error()
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:11:02 +00:00
|
|
|
chooser_process = subprocess.Popen(process_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:11:02 +00:00
|
|
|
with chooser_process.stdin as pipe:
|
|
|
|
for index, choice in enumerate(choices):
|
|
|
|
assert "\n" not in choice
|
|
|
|
if not supports_result_index:
|
|
|
|
pipe.write(str(index).encode())
|
|
|
|
pipe.write(b" ")
|
|
|
|
pipe.write(choice.encode())
|
|
|
|
pipe.write(b"\n")
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:11:02 +00:00
|
|
|
exit_code: int = chooser_process.wait()
|
|
|
|
if exit_code != 0:
|
|
|
|
raise Exception("chooser process failed with exit code {}".format(exit_code))
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:11:02 +00:00
|
|
|
chosen_index = int(chooser_process.stdout.read().strip().split()[0])
|
|
|
|
return chosen_index
|
2020-10-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
2021-04-18 15:06:47 +00:00
|
|
|
def send_notification(title: str, message: str, url: str = None) -> None:
|
2021-04-26 07:11:02 +00:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
process_args = [
|
|
|
|
"terminal-notifier",
|
|
|
|
"-title",
|
|
|
|
title,
|
|
|
|
"-message",
|
|
|
|
message,
|
|
|
|
"-open",
|
|
|
|
]
|
|
|
|
if url is not None:
|
|
|
|
process_args += [url]
|
|
|
|
elif os.name == "posix":
|
|
|
|
process_args = [
|
|
|
|
"notify-send",
|
|
|
|
"--icon=utilities-terminal",
|
|
|
|
"--expire-time=3000",
|
|
|
|
title,
|
|
|
|
message,
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
platform_not_supported_error()
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:11:02 +00:00
|
|
|
subprocess.run(process_args, check=True)
|
2020-10-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
2021-04-18 15:06:47 +00:00
|
|
|
def set_clipboard(text: str) -> None:
|
2021-04-26 07:11:02 +00:00
|
|
|
# TODO: somehow merge program selection with the logic in `zsh/functions.zsh`
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
process_args = ["pbcopy"]
|
|
|
|
elif os.name == "posix":
|
|
|
|
process_args = ["xsel", "--clipboard", "--input"]
|
|
|
|
# process_args = ["xclip", "-in", "-selection", "clipboard"]
|
|
|
|
else:
|
|
|
|
platform_not_supported_error()
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:11:02 +00:00
|
|
|
subprocess.run(process_args, input=text.encode(), check=True)
|