~pvsr/qpm

qpm/qpm/utils.py -rw-r--r-- 1.2 KiB
8b6b26a0Peter Rice document dmenu-wl support 2 months ago
                                                                                
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
import platform
import subprocess
import sys
from pathlib import Path
from shutil import which
from sys import exit, stderr
from typing import Optional

from xdg import BaseDirectory  # type: ignore

SUPPORTED_MENUS = ["rofi", "dmenu", "dmenu-wl", "applescript"]


def error(msg: str) -> None:
    print(f"Error: {msg}", file=stderr)


def user_data_dir() -> Path:
    if platform.system() == "Linux":
        return Path(BaseDirectory.xdg_data_home) / "qutebrowser"
    if platform.system() == "Darwin":
        return Path.home() / "Library" / "Application Support" / "qutebrowser"
    error("This operation is only implemented for linux and macOS.")
    print(
        "If you're interested in adding support for another OS, send a PR "
        "to github.com/pvsr/qbpm adding the location of qutebrowser data such "
        "as history.sqlite on your OS to user_data_dir() in qpm/utils.py.",
        file=stderr,
    )
    exit(1)


def user_config_dir() -> Path:
    return Path(BaseDirectory.xdg_config_home) / "qutebrowser"


def get_default_menu() -> Optional[str]:
    if sys.platform == "darwin":
        return "applescript"
    for menu_cmd in ["rofi", "dmenu-wl", "dmenu"]:
        if which(menu_cmd) is not None:
            return menu_cmd
    return None