-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.lua
More file actions
257 lines (213 loc) · 6.99 KB
/
counter.lua
File metadata and controls
257 lines (213 loc) · 6.99 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
require "windows"
require "iutf8"
if not windows:isSupported() then
inline:toast "The module is not supported on this version of Android"
return
end
local preferences = inline:getDefaultSharedPreferences()
local whitelistPrefs = inline:getSharedPreferences "counter_whitelist"
local DEFAULT_WINDOW_OFFSET = 20
local DEFAULT_WINDOW_TIMEOUT = 5000
local window
local timer = inline:getTimer()
local function getWhitelist()
local set = whitelistPrefs:getStringSet("apps", nil)
if not set then return {} end
local result = {}
local iterator = set:iterator()
while iterator:hasNext() do
local pkg = iterator:next()
result[pkg] = true
end
return result
end
local function saveWhitelist(list)
local HashSet = require "java.util.HashSet"
local set = luajava.new(HashSet)
for pkg in pairs(list) do
set:add(pkg)
end
whitelistPrefs:edit():putStringSet("apps", set):apply()
end
local function isAllowed(input)
if not preferences:getBoolean("counter_use_whitelist", false) then
return true
end
local pkg = input:getPackageName()
local whitelist = getWhitelist()
return whitelist[pkg] == true
end
local function count(text)
local chars = utf8.len(text) or 0
local words = 0
local lines = 1
local sentences = 0
for _ in text:gmatch("%S+") do
words = words + 1
end
for _ in text:gmatch("\n") do
lines = lines + 1
end
for _ in text:gmatch("[%.!%?]+%s") do
sentences = sentences + 1
end
if #text > 0 and text:match("[%.!%?]%s*$") then
-- last sentence already counted above if followed by space
elseif #text > 0 and words > 0 then
sentences = sentences + 1
end
return chars, words, lines, sentences
end
local function formatStats(chars, words, lines, sentences)
local parts = {}
if preferences:getBoolean("counter_show_chars", true) then
table.insert(parts, chars .. " chars")
end
if preferences:getBoolean("counter_show_words", true) then
table.insert(parts, words .. " words")
end
if preferences:getBoolean("counter_show_lines", false) then
table.insert(parts, lines .. " lines")
end
if preferences:getBoolean("counter_show_sentences", false) then
table.insert(parts, sentences .. " sent")
end
if #parts == 0 then
return chars .. " chars"
end
return table.concat(parts, " · ")
end
local function showWindow(input, text)
local timerTask = inline:timerTask(function()
window.close()
end)
local label
local floatingWindow = windows.createAligned(input, {
noBackground = true,
offsetY = preferences:getInt("counter_window_offset", DEFAULT_WINDOW_OFFSET),
onClose = function()
window = nil
timerTask:cancel()
end
}, function(ui)
label = ui.text(text)
return { label }
end)
timer:schedule(timerTask, preferences:getInt("counter_window_timeout", DEFAULT_WINDOW_TIMEOUT))
return {
close = function()
floatingWindow:close()
end,
update = function(newText)
label:setText(newText)
end
}
end
local function updateWindow(input, text)
if window then
window.update(text)
else
window = showWindow(input, text)
end
end
local function watcher(input)
if not isAllowed(input) then
if window then window.close() end
return
end
local text = inline:getText(input)
if #text == 0 then
if window then window.close() end
return
end
local chars, words, lines, sentences = count(text)
local result = formatStats(chars, words, lines, sentences)
updateWindow(input, result)
end
local function addApp(input, query)
local pkg = query:getArgs()
if pkg == "" then
pkg = input:getPackageName()
end
local whitelist = getWhitelist()
whitelist[pkg] = true
saveWhitelist(whitelist)
query:answer("Added: " .. pkg)
end
local function removeApp(input, query)
local pkg = query:getArgs()
if pkg == "" then
pkg = input:getPackageName()
end
local whitelist = getWhitelist()
whitelist[pkg] = nil
saveWhitelist(whitelist)
query:answer("Removed: " .. pkg)
end
local function listApps(_, query)
local whitelist = getWhitelist()
local apps = {}
for pkg in pairs(whitelist) do
table.insert(apps, "• " .. pkg)
end
if #apps == 0 then
query:answer "Whitelist is empty"
else
query:answer("Whitelisted apps:\n" .. table.concat(apps, "\n"))
end
end
return function(module)
module:setCategory "Counter"
module:registerCommand("counter.add", addApp, "Add current app to counter whitelist")
module:registerCommand("counter.remove", removeApp, "Remove current app from counter whitelist")
module:registerCommand("counter.list", listApps, "List whitelisted apps")
module:registerPreferences(function(prefs)
return {
prefs.switch("counter", "Enabled")
:setDefault(false)
:setListener(function(isChecked)
if isChecked then
module:registerWatcher(watcher)
else
module:unregisterWatcher(watcher)
if window then window.close() end
end
end),
prefs.spacer(12),
prefs.card {
prefs.text "Display":bold():size(16),
prefs.spacer(4),
prefs.switch("counter_show_chars", "Characters"):setDefault(true),
prefs.switch("counter_show_words", "Words"):setDefault(true),
prefs.switch("counter_show_lines", "Lines"),
prefs.switch("counter_show_sentences", "Sentences"),
},
prefs.spacer(12),
prefs.card {
prefs.text "Whitelist":bold():size(16),
prefs.spacer(4),
prefs.text "When enabled, counter only shows in whitelisted apps. Use {counter.add}$ in any app to add it.":size(12),
prefs.spacer(4),
prefs.switch("counter_use_whitelist", "Use app whitelist"),
},
prefs.spacer(12),
prefs.card {
prefs.text "Window":bold():size(16),
prefs.spacer(8),
prefs.textInput("counter_window_timeout", "Timeout (ms)")
:setDefault(DEFAULT_WINDOW_TIMEOUT)
:useInt()
:setInputType { "TYPE_CLASS_NUMBER", "TYPE_NUMBER_FLAG_SIGNED" },
prefs.spacer(8),
prefs.textInput("counter_window_offset", "Offset (dp)")
:setDefault(DEFAULT_WINDOW_OFFSET)
:useInt()
:setInputType { "TYPE_CLASS_NUMBER", "TYPE_NUMBER_FLAG_SIGNED" },
},
}
end)
if preferences:getBoolean("counter", false) then
module:registerWatcher(watcher)
end
module:saveLazyLoad()
end