-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlib.py
55 lines (39 loc) · 1.42 KB
/
lib.py
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
from __future__ import annotations
import logging
import subprocess
import gi
gi.require_versions({"Gdk": "3.0", "Gtk": "3.0"})
try:
gi.require_version("Notify", "0.8")
except ValueError:
gi.require_version("Notify", "0.7")
from gi.repository import Gdk, GObject, Gtk, Notify # noqa: E402
logger = logging.getLogger("ulauncher-clipboard")
Notify.init("ulauncher-clipboard-extension")
def exec_get(*args: str) -> str:
proc = subprocess.run([*args], check=True, capture_output=True, text=True)
return proc.stdout.rstrip()
def parse_int(string: str, fallback: int = 0) -> int:
try:
return int(string, 10)
except Exception:
return fallback
def pid_of(name: str) -> list[int]:
# Get the first pid (there may be many space-separated pids), and parse to int
try:
pids = exec_get("pidof", "-x", name).split()
return [int(pid) for pid in pids]
except subprocess.CalledProcessError:
return []
def set_clipboard(text: str) -> None:
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1)
clipboard.store()
GObject.timeout_add(25, Gtk.main_quit)
Gtk.main()
def show_message(title: str, body: str, icon: str) -> Notify.Notification:
message = Notify.Notification.new(title, body, icon)
message.set_timeout(Notify.EXPIRES_NEVER)
message.set_urgency(Notify.Urgency.CRITICAL)
message.show()
return message