forked from Ulauncher/ulauncher-emoji
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
119 lines (105 loc) Β· 5.23 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
import os
import logging
import sqlite3
from pprint import pprint
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.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
logger = logging.getLogger(__name__)
extension_icon = 'images/icon.png'
db_path = os.path.join(os.path.dirname(__file__), 'emoji.sqlite')
conn = sqlite3.connect(db_path, check_same_thread=False)
conn.row_factory = sqlite3.Row
def normalize_skin_tone(tone):
"""
Converts from the more visual skin tone preferences string to a more
machine-readable format.
"""
if tone == "π default": return ''
elif tone == "ππ» light": return 'light'
elif tone == "ππΌ medium-light": return 'medium-light'
elif tone == "ππ½ medium": return 'medium'
elif tone == "ππΎ medium-dark": return 'medium-dark'
elif tone == "ππΏ dark": return 'dark'
else: return None
class EmojiExtension(Extension):
def __init__(self):
super(EmojiExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.allowed_skin_tones = ["", "dark", "light", "medium", "medium-dark", "medium-light"]
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
icon_style = extension.preferences['emoji_style']
fallback_icon_style = extension.preferences['fallback_emoji_style']
search_term = event.get_argument().replace('%', '') if event.get_argument() else None
search_with_shortcodes = search_term and search_term.startswith(':')
# Add %'s to search term (since LIKE %?% doesn't work)
if search_term and search_with_shortcodes:
search_term = ''.join([search_term, '%'])
elif search_term:
search_term = ''.join(['%', search_term, '%'])
if search_with_shortcodes:
query = '''
SELECT em.name, em.code, em.keywords,
em.icon_apple, em.icon_twemoji, em.icon_noto, em.icon_blobmoji,
skt.icon_apple AS skt_icon_apple, skt.icon_twemoji AS skt_icon_twemoji,
skt.icon_noto AS skt_icon_noto, skt.icon_blobmoji AS skt_icon_blobmoji,
skt.code AS skt_code, sc.code as "shortcode"
FROM emoji AS em
LEFT JOIN skin_tone AS skt
ON skt.name = em.name AND tone = ?
LEFT JOIN shortcode AS sc
ON sc.name = em.name
WHERE sc.code LIKE ?
GROUP BY em.name
ORDER BY length(replace(sc.code, trim('{st}', '%'), ''))
LIMIT 8
'''.format(st=search_term)
else:
query = '''
SELECT em.name, em.code, em.keywords,
em.icon_apple, em.icon_twemoji, em.icon_noto, em.icon_blobmoji,
skt.icon_apple AS skt_icon_apple, skt.icon_twemoji AS skt_icon_twemoji,
skt.icon_noto AS skt_icon_noto, skt.icon_blobmoji AS skt_icon_blobmoji,
skt.code AS skt_code
FROM emoji AS em
LEFT JOIN skin_tone AS skt
ON skt.name = em.name AND tone = ?
WHERE em.name LIKE ?
LIMIT 8
'''
# Display blank prompt if user hasn't typed anything
if not search_term:
search_icon = 'images/%s/icon.png' % icon_style
return RenderResultListAction([
ExtensionResultItem(icon=search_icon,
name='Type in emoji name...',
on_enter=DoNothingAction())
])
skin_tone = normalize_skin_tone(extension.preferences['skin_tone'])
if skin_tone not in extension.allowed_skin_tones:
logger.warning('Unknown skin tone "%s"' % skin_tone)
skin_tone = ''
# Get list of results from sqlite DB
items = []
display_char = extension.preferences['display_char'] != 'no'
for row in conn.execute(query, [skin_tone, search_term]):
if row['skt_code']:
icon = row['skt_icon_%s' % icon_style]
icon = row['skt_icon_%s' % fallback_icon_style] if not icon else icon
code = row['skt_code']
else:
icon = row['icon_%s' % icon_style]
icon = row['icon_%s' % fallback_icon_style] if not icon else icon
code = row['code']
name = row['shortcode'] if search_with_shortcodes else row['name'].capitalize()
if display_char: name += ' | %s' % code
items.append(ExtensionResultItem(icon=icon, name=name,
on_enter=CopyToClipboardAction(code)))
return RenderResultListAction(items)
if __name__ == '__main__':
EmojiExtension().run()