This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
118 lines (95 loc) · 4.19 KB
/
__init__.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
#!/usr/bin/env python3
# Copyright (C) 2018 Sylvia van Os <[email protected]>
#
# Pext clipboard module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gettext
import os
from threading import Thread
from time import sleep
import pyperclip
from pext_base import ModuleBase
from pext_helpers import Action
class Module(ModuleBase):
def init(self, settings, q):
try:
lang = gettext.translation('pext_module_clipboard', localedir=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale'), languages=[settings['_locale']])
except FileNotFoundError:
lang = gettext.NullTranslations()
print("No {} translation available for pext_module_clipboard".format(settings['_locale']))
lang.install()
self.q = q
self.settings = settings
self.tempignore = None
self.entries = []
self.clipboard_watcher = Thread(target=self._watch_clipboard)
self.clipboard_watcher.daemon = True
self.clipboard_watcher.start()
def _watch_clipboard(self):
while True:
sleep(0.1)
try:
content = pyperclip.paste()
except Exception:
continue
# Don't save only whitespace
if content.isspace():
continue
if content == self.tempignore or (self.entries and content == self.entries[0]):
continue
try:
self.entries.remove(content)
except ValueError:
pass
self.entries.insert(0, content)
self._update_entries()
if self.settings['_api_version'] >= [0, 4, 0]:
self.q.put([Action.set_entry_context, content, [_("Edit"), _("Remove")]])
self.tempignore = None
def _update_entries(self):
self.q.put([Action.replace_entry_list, self.entries])
if self.settings['_api_version'] >= [0, 5, 0]:
self.q.put([Action.replace_entry_info_dict, {entry: entry for entry in self.entries}])
def stop(self):
pass
def selection_made(self, selection):
if len(selection) == 0:
self._update_entries()
elif len(selection) == 1:
if self.settings['_api_version'] >= [0, 4, 0]:
if selection[0]["context_option"] == _("Edit"):
self.tempignore = selection[0]["value"]
self.q.put([Action.ask_input_multi_line, _("Change clipboard entry to"), selection[0]["value"], selection[0]["value"]])
self.q.put([Action.set_selection, []])
return
elif selection[0]["context_option"] == _("Remove"):
self.tempignore = selection[0]["value"]
try:
self.entries.remove(selection[0]["value"])
except ValueError:
pass
self._update_entries()
self.q.put([Action.set_selection, []])
return
self.q.put([Action.copy_to_clipboard, selection[0]["value"]])
self.q.put([Action.close])
def process_response(self, response, identifier):
if response is not None:
for i, entry in enumerate(self.entries):
if entry == identifier:
self.entries[i] = response
self._update_entries()
if self.settings['_api_version'] >= [0, 4, 0]:
self.q.put([Action.set_entry_context, response, [_("Edit"), _("Remove")]])
self.q.put([Action.set_selection, []])
return