-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathrofi.py
135 lines (118 loc) · 4.44 KB
/
rofi.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from subprocess import run
from typing import Dict, List, Tuple, Union
from ..abstractionhelper import is_installed
from ..models import CANCEL, DEFAULT, Action, CharacterEntry, Shortcut
from .selector import Selector
class Rofi(Selector):
@staticmethod
def supported() -> bool:
return is_installed("rofi")
@staticmethod
def name() -> str:
return "rofi"
def show_character_selection(
self,
characters: List[CharacterEntry],
recent_characters: List[str],
prompt: str,
show_description: bool,
use_icons: bool,
keybindings: Dict[Action, str],
additional_args: List[str],
) -> Tuple[Union[Action, DEFAULT, CANCEL], Union[List[str], Shortcut]]:
parameters = [
"rofi",
"-dmenu",
"-markup-rows",
"-i",
"-multi-select",
"-no-custom",
"-ballot-unselected-str",
"",
"-format",
"i",
"-p",
prompt,
"-kb-custom-11",
keybindings[Action.COPY],
"-kb-custom-12",
keybindings[Action.TYPE],
"-kb-custom-13",
keybindings[Action.CLIPBOARD],
"-kb-custom-14",
keybindings[Action.UNICODE],
"-kb-custom-15",
keybindings[Action.COPY_UNICODE],
"-kb-custom-16",
keybindings[Action.TYPE_NUMERICAL],
*additional_args,
]
if recent_characters:
parameters.extend(["-mesg", self.__format_recent_characters(recent_characters)])
rofi = run(
parameters,
input="\n".join(self.__format_characters(characters, use_icons, show_description)),
capture_output=True,
encoding="utf-8",
)
if 10 <= rofi.returncode <= 19:
return DEFAULT(), Shortcut(rofi.returncode - 10)
action: Union[Action, DEFAULT, CANCEL]
if rofi.returncode == 1:
action = CANCEL()
elif rofi.returncode == 20:
action = Action.COPY
elif rofi.returncode == 21:
action = Action.TYPE
elif rofi.returncode == 22:
action = Action.CLIPBOARD
elif rofi.returncode == 23:
action = Action.UNICODE
elif rofi.returncode == 24:
action = Action.COPY_UNICODE
elif rofi.returncode == 25:
action = Action.TYPE_NUMERICAL
else:
action = DEFAULT()
return action, [characters[int(index)].character for index in rofi.stdout.splitlines()]
def __format_characters(
self, characters: List[CharacterEntry], use_icons: bool, show_description: bool
) -> List[str]:
if use_icons and not show_description:
return [f"\0meta\x1f{entry.description}\x1ficon\x1f<span>{entry.character}</span>" for entry in characters]
elif use_icons and show_description:
return [f"{entry.description}\0icon\x1f<span>{entry.character}</span>" for entry in characters]
elif not use_icons and show_description:
return self.basic_format_characters(characters)
else:
return [f"{entry.character}\0meta\x1f{entry.description}" for entry in characters]
def __format_recent_characters(self, recent_characters: List[str]) -> str:
pairings = [f"\u200e{(index + 1) % 10}: {character}" for index, character in enumerate(recent_characters)]
return " | ".join(pairings)
def show_skin_tone_selection(
self, tones_emojis: List[str], prompt: str, additional_args: List[str]
) -> Tuple[int, str]:
rofi = run(
["rofi", "-dmenu", "-i", "-no-custom", "-p", prompt, *additional_args],
input="\n".join(tones_emojis),
capture_output=True,
encoding="utf-8",
)
return rofi.returncode, rofi.stdout
def show_action_menu(self, additional_args: List[str]) -> List[Action]:
rofi = run(
[
"rofi",
"-dmenu",
"-multi-select",
"-no-custom",
"-ballot-unselected-str",
"",
"-i",
*additional_args,
],
input="\n".join([str(it) for it in Action if it != Action.MENU]),
capture_output=True,
encoding="utf-8",
)
return [Action(action) for action in rofi.stdout.strip().split("\n")]