forked from carlosgaldino/alfred-emoji-workflow
-
Notifications
You must be signed in to change notification settings - Fork 2
/
emoji.rb
executable file
·210 lines (179 loc) · 4.83 KB
/
emoji.rb
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env ruby
# encoding: utf-8
require 'tmpdir'
require 'optparse'
require 'json'
require 'shellwords'
require 'pathname'
class Integer
def to_unicode
self.to_s(16).rjust(4, '0')
end
end
class Array
def to_codepoint_string
self.map {|item| item.is_a?(Numeric) ? '0x' + item.to_unicode : item.to_s}.join(', ')
end
end
PWD = Pathname.new File.expand_path(File.dirname(__FILE__))
EMOJI_DB_PATH = PWD.join('./emoji-db/')
MARSHAL_TMP_FILE = File.expand_path('./alfred-emoji-workflow-cache', Dir.tmpdir)
STDERR.puts '===='
STDERR.puts "ARGV: `#{ARGV}`"
STDERR.puts '===='
option_array = ARGV.join(' ').split
OptionParser.new do |opts|
opts.program_name = File.basename(__FILE__)
opts.summary_indent = " "
opts.summary_width = 20
opts.on("-t", "--tone [number]", /^\d+$/, "Include skin tone") do |t|
if t.to_i % 11 == 0
$skin_tone = (t.to_i / 11).to_s
else
$skin_tone = t
end
end
opts.on("-d", "--debug", "Run in debug mode (no cache)") do |_o|
STDERR.puts "Debug mode enabled"
$debug_mode = true
end
end.parse!(option_array)
def reset_marshal_cache
File.open(MARSHAL_TMP_FILE, File::RDWR|File::CREAT, 0644) do |f|
fc = {
'search_strings' => {},
'db' => JSON.load(IO.read(EMOJI_DB_PATH.join('emoji-db.json')))
}
fc['db'].each do |k, v|
puts [k, v]
fc['db'][k]['name'] = fc['db'][k]['name'] || ''
fc['search_strings'][k] = [
'',
(v['name'] || '').split,
v['keywords'],
v['codepoints'].map(&:to_unicode),
fc['db'][k]['fitz'] ? 'fitz' : [],
'',
].compact.join(' ').downcase
if fc['db'][k]['image']
fc['db'][k]['image'] = EMOJI_DB_PATH.join(fc['db'][k]['image'])
else
STDERR.puts "Emoji #{k} is missing an image"
end
if fc['db'][k]['fitz']
fc['db'][k]['fitz'].each do |k, v|
v.merge!({ 'image' => EMOJI_DB_PATH.join(v['image']) })
end
end
end
f.rewind
f.write(Marshal.dump(fc))
f.flush
f.truncate(f.pos)
fc
end
end
if $debug_mode
reset_marshal_cache
STDERR.puts "Marshal cache reset!"
puts JSON.pretty_generate({
:items => [
{
:uid => '__debug__',
:title => "Debug \u{1f41b}",
:subtitle => "Emoji database has been reset",
}
],
})
exit 0
end
EMOJI_OBJ = begin
File.open(MARSHAL_TMP_FILE, File::RDWR|File::CREAT, 0644) {|f| Marshal.load(f.read)}
rescue ArgumentError
STDERR.puts "Marshal cache could not be loaded. Resetting!"
reset_marshal_cache
end
### SEARCH SHIT
exact_matches = []
matches = []
unless option_array.empty?
query = option_array.join(' ').downcase.strip
STDERR.puts "QUERY: `#{query}`"
if query.strip == ''
# show everything if no query is provided
matches = EMOJI_OBJ['db'].keys
else
EMOJI_OBJ['search_strings'].each do |key, ss|
if ss.include?(" #{query} ")
exact_matches.push key
STDERR.puts "`#{EMOJI_OBJ['db'][key]['name']}` is an exact match!"
elsif ss.include?(query)
matches.push key
STDERR.puts "`#{EMOJI_OBJ['db'][key]['name']}` is a match!"
end
end
end
end
STDERR.puts JSON.pretty_generate(ENV.to_h)
items = (exact_matches + matches).map do |emoji_key|
STDERR.puts "CODEPOINT: `#{emoji_key}`"
emoji = EMOJI_OBJ['db'][emoji_key]
if $skin_tone && emoji['fitz'] && emoji['fitz'][$skin_tone]
emoji = emoji['fitz'][$skin_tone]
end
path = emoji['image']
codepoints = emoji['codepoints']
STDERR.puts "KEYWORDS: `#{EMOJI_OBJ['search_strings'][emoji_key]}`"
STDERR.puts path
emojilib_name = emoji['emojilib_name'] ? ":#{emoji['emojilib_name']}:" : ''
unicode_txt = codepoints.pack('U*')
codepoint_txt = codepoints.to_codepoint_string
subtitle = "Copy #{unicode_txt} to clipboard"
mods = {
:ctrl => {
:valid => true,
:arg => emoji_key,
:subtitle => "Copy #{emoji_key} to clipboard",
:variables => {
:active_key => 'ctrl'
},
},
:shift => {
:valid => !!emoji['emojilib_name'],
:arg => emoji['emojilib_name'],
:subtitle => emoji['emojilib_name'] ? "Copy #{emojilib_name} to clipboard" : "No emojilib name :..(",
:variables => {
:active_key => 'shift'
},
},
:alt => {
:valid => true,
:arg => codepoint_txt,
:subtitle => "Copy '#{codepoint_txt}' to clipboard",
},
:cmd => {
:valid => true,
:arg => path,
:subtitle => "Reveal image for #{unicode_txt} in Finder",
:variables => {
:active_key => 'cmd'
},
},
}
{
:arg => unicode_txt,
:uid => emoji_key,
:variables => {},
:icon => {
:path => path,
},
# :type => 'file:skipcheck',
:title => emoji['name'],
:quicklookurl => path,
:subtitle => subtitle,
:mods => mods,
}
end
puts JSON.pretty_generate({
:items => items,
})