1
1
import sys
2
- sys .path .append ('/home/zen/.local/lib/python2.7/site-packages/' )
2
+
3
+ sys .path .append ("/home/zen/.local/lib/python2.7/site-packages/" )
3
4
4
5
import os
6
+ import codecs
5
7
from fuzzywuzzy import process
6
8
7
9
from ulauncher .api .client .Extension import Extension
13
15
from ulauncher .api .shared .action .HideWindowAction import HideWindowAction
14
16
15
17
16
- class DemoExtension (Extension ):
18
+ FILE_PATH = os .path .dirname (sys .argv [0 ])
19
+
17
20
21
+ class UnicodeCharExtension (Extension ):
18
22
def __init__ (self ):
19
- super (DemoExtension , self ).__init__ ()
23
+ super (UnicodeCharExtension , self ).__init__ ()
20
24
self .subscribe (KeywordQueryEvent , KeywordQueryEventListener ())
21
25
22
26
23
27
class KeywordQueryEventListener (EventListener ):
24
-
25
28
def __init__ (self ):
26
- f = open (os .path .dirname (sys .argv [0 ]) + ' /unicode_list.txt' , 'r' )
29
+ f = open (os .path .dirname (sys .argv [0 ]) + " /unicode_list.txt" , "r" )
27
30
data = f .readlines ()
28
31
self .data = {}
29
32
# self.names = []
30
33
self .items = []
31
34
# self.blocks = []
32
35
for item in data :
33
36
item = item .strip ()
34
- name , code , block = item .split (' \t ' )
35
- self .data [name + ' ' + block ] = (name , block , code )
36
- self .items .append (name + ' ' + block )
37
+ name , code , block = item .split (" \t " )
38
+ self .data [name + " " + block ] = (name , block , code )
39
+ self .items .append (name + " " + block )
37
40
# self.names.append(name)
38
41
# self.blocks.append(block)
39
42
@@ -42,14 +45,46 @@ def on_event(self, event, extension):
42
45
arg = event .get_argument ()
43
46
if arg :
44
47
matches = process .extract (arg , self .items , limit = 15 )
48
+
45
49
for m in matches :
46
50
name , block , code = self .data [m [0 ]]
47
- items .append (ExtensionResultItem (icon = 'images/bookmark.svg' ,
48
- name = name + ' - ' + unichr (int (code , 16 )),
49
- description = block ,
50
- on_enter = CopyToClipboardAction (unichr (int (code , 16 )))))
51
+ image_path = self .create_icon_image (code )
52
+ items .append (
53
+ ExtensionResultItem (
54
+ icon = image_path ,
55
+ name = name + " - " + unichr (int (code , 16 )),
56
+ description = block ,
57
+ on_enter = CopyToClipboardAction (unichr (int (code , 16 ))),
58
+ )
59
+ )
51
60
52
61
return RenderResultListAction (items )
53
62
54
- if __name__ == '__main__' :
55
- DemoExtension ().run ()
63
+ def create_icon_image (self , code ):
64
+ path = sys .argv [0 ] + "images/cache/icon_%s.svg" % code
65
+ if os .path .isfile (path ):
66
+ return path
67
+ return create_character_icon (code )
68
+
69
+
70
+ def load_icon_template ():
71
+ with open (os .path .join (FILE_PATH , "images/unicode.svg" ), "r" ) as i :
72
+ return i .read ()
73
+
74
+
75
+ def is_icon_cached (code ):
76
+ return os .path .isfile (os .path .join (FILE_PATH , "images/cache/icon_%s.svg" % code ))
77
+
78
+
79
+ def create_character_icon (code , font = 'sans-serif' ):
80
+ template = load_icon_template ()
81
+ icon = template .replace ("{symbol}" , unichr (int (code , 16 ))).replace ('{font}' , font )
82
+ with codecs .open (
83
+ os .path .join (FILE_PATH , "images/cache/icon_%s.svg" % code ), "w" , "utf-8"
84
+ ) as target :
85
+ target .write (icon )
86
+ return os .path .join (FILE_PATH , "images/cache/icon_%s.svg" % code )
87
+
88
+
89
+ if __name__ == "__main__" :
90
+ UnicodeCharExtension ().run ()
0 commit comments