forked from compilelife/ulauncher-searchfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
120 lines (97 loc) · 4.31 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionSmallResultItem import ExtensionSmallResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.OpenAction import OpenAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction
from ulauncher.api.shared.event import PreferencesEvent
from ulauncher.api.shared.event import PreferencesUpdateEvent
from ulauncher.api.shared.event import ItemEnterEvent
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from locator import Locator
from html import escape
from pathlib import Path
locator = Locator()
class Options:
def __init__(self):
self.options = ["-iA"]
def update(self, opt_str):
self.options = opt_str.split(';')
def get_default(self):
return self.options[0]
def get_all(self):
return self.options
opts = Options()
class SearchFileExtension(Extension):
def __init__(self):
super(SearchFileExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(PreferencesEvent, PreferencesEventListener())
self.subscribe(PreferencesUpdateEvent, PreferencesUpdateEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
class PreferencesUpdateEventListener(EventListener):
def on_event(self, event, extension):
if event.id == 'limit':
locator.set_limit(event.new_value)
elif event.id == 'options':
opts.update(event.new_value)
locator.set_locate_opt(opts.get_default())
class PreferencesEventListener(EventListener):
def on_event(self, event, extension):
locator.set_limit(event.preferences['limit'])
opts.update(event.preferences['options'])
locator.set_locate_opt(opts.get_default())
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
results = event.get_data()
items = []
for file in results:
items.append(ExtensionSmallResultItem(icon='images/copy.png',
name = file,
on_enter = CopyToClipboardAction(file)))
return RenderResultListAction(items)
class KeywordQueryEventListener(EventListener):
def __help(self):
all_opt = opts.get_all()
items = []
for i in range(len(all_opt)):
hint_str='locate '+all_opt[i]
query_str='s r '+all_opt[i]+' '
items.append(ExtensionSmallResultItem(icon='images/info.png',
name=hint_str,
on_enter=SetUserQueryAction(
query_str)
))
return items
def get_display_path(self, path):
"""Strip /home/user from path if appropriate."""
path = Path(path)
home = Path.home()
if home in path.parents:
return '~/' + str(path.relative_to(home))
else:
return str(path)
def on_event(self, event, extension):
arg = event.get_argument()
items = []
if arg is None:
items = self.__help()
else:
try:
results = locator.run(arg)
alt_action = ExtensionCustomAction(results, True)
for file in results:
items.append(ExtensionSmallResultItem(icon='images/ok.png',
name = escape(self.get_display_path(file)),
on_enter = OpenAction(file),
on_alt_enter = alt_action))
except Exception as e:
error_info = str(e)
items = [ExtensionSmallResultItem(icon='images/error.png',
name = error_info,
on_enter = CopyToClipboardAction(error_info))]
return RenderResultListAction(items)
if __name__ == '__main__':
SearchFileExtension().run()