diff options
Diffstat (limited to 'apps/plugins/lua_scripts/fileviewers.lua')
-rwxr-xr-x | apps/plugins/lua_scripts/fileviewers.lua | 465 |
1 files changed, 465 insertions, 0 deletions
diff --git a/apps/plugins/lua_scripts/fileviewers.lua b/apps/plugins/lua_scripts/fileviewers.lua new file mode 100755 index 0000000000..5a9c417d36 --- /dev/null +++ b/apps/plugins/lua_scripts/fileviewers.lua | |||
@@ -0,0 +1,465 @@ | |||
1 | --[[ | ||
2 | /*************************************************************************** | ||
3 | * __________ __ ___. | ||
4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
8 | * \/ \/ \/ \/ \/ | ||
9 | * $Id$ | ||
10 | * | ||
11 | * Copyright (C) 2017 William Wilgus | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | ]] | ||
23 | if ... == nil then rb.splash(rb.HZ * 3, "use 'require'") end | ||
24 | require("printtable") | ||
25 | local _clr = require("color") | ||
26 | local _lcd = require("lcd") | ||
27 | local _print = require("print") | ||
28 | local _timer = require("timer") | ||
29 | |||
30 | require("actions") | ||
31 | local CANCEL_BUTTON = rb.actions.PLA_CANCEL | ||
32 | -------------------------------------------------------------------------------- | ||
33 | -- builds an index of byte position of every line at each bufsz increment | ||
34 | -- in filename; bufsz == 1 would be every line; saves to filename.ext.idx_ext | ||
35 | -- lnbyte should be nil for text files and number of bytes per line for binary | ||
36 | local function build_file_index(filename, idx_ext, bufsz, lnbyte) | ||
37 | |||
38 | if not filename then return end | ||
39 | local file = io.open('/' .. filename, "r") --read | ||
40 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
41 | local fsz = file:seek("end") | ||
42 | local fsz_kb = fsz / 1024 | ||
43 | local count | ||
44 | local ltable = {0} --first index is the beginning of the file | ||
45 | local timer = _timer() | ||
46 | local fread | ||
47 | _lcd:splashf(100, "Indexing file %d Kb", (fsz / 1024)) | ||
48 | |||
49 | if lnbyte then | ||
50 | fread = function(f) return f:read(lnbyte) end | ||
51 | else | ||
52 | lnbyte = -1 | ||
53 | fread = function(f) return f:read("*l") end | ||
54 | end | ||
55 | |||
56 | file:seek("set", 0) | ||
57 | for i = 1, fsz do | ||
58 | if i % bufsz == 0 then | ||
59 | local loc = file:seek() | ||
60 | ltable[#ltable + 1] = loc | ||
61 | _lcd:splashf(1, "Parsing %d of %d Kb", loc / 1024, fsz_kb) | ||
62 | end | ||
63 | if rb.get_plugin_action(0) == CANCEL_BUTTON then | ||
64 | return | ||
65 | end | ||
66 | if not fread(file) then | ||
67 | count = i | ||
68 | break | ||
69 | end | ||
70 | end | ||
71 | |||
72 | local fileidx = io.open('/' .. filename .. idx_ext, "w+") -- write/erase | ||
73 | if fileidx then | ||
74 | fileidx:write(fsz .. "\n") | ||
75 | fileidx:write(count .. "\n") | ||
76 | fileidx:write(bufsz .. "\n") | ||
77 | fileidx:write(lnbyte .. "\n") | ||
78 | fileidx:write(table.concat(ltable, "\n")) | ||
79 | fileidx:close() | ||
80 | _lcd:splashf(100, "Finished in %d seconds", timer.stop() / rb.HZ) | ||
81 | collectgarbage("collect") | ||
82 | else | ||
83 | error("unable to save index file") | ||
84 | end | ||
85 | end -- build_file_index | ||
86 | -------------------------------------------------------------------------------- | ||
87 | |||
88 | --- returns size of original file, total lines buffersize, and table filled | ||
89 | -- with line offsets in index file -> filename | ||
90 | local function load_index_file(filename) | ||
91 | local filesz, count, bufsz, lnbyte | ||
92 | local ltable | ||
93 | local fileidx = io.open('/' .. filename, "r") --read | ||
94 | if fileidx then | ||
95 | local idx = -3 | ||
96 | ltable = {} | ||
97 | fileidx:seek("set", 0) | ||
98 | for line in fileidx:lines() do | ||
99 | if idx == -3 then | ||
100 | filesz = tonumber(line) | ||
101 | elseif idx == -2 then | ||
102 | count = tonumber(line) | ||
103 | elseif idx == -1 then | ||
104 | bufsz = tonumber(line) | ||
105 | elseif idx == 0 then | ||
106 | lnbyte = tonumber(line) | ||
107 | else | ||
108 | ltable[idx] = tonumber(line) | ||
109 | end | ||
110 | idx = idx + 1 | ||
111 | end | ||
112 | fileidx:close() | ||
113 | end | ||
114 | return lnbyte, filesz, count, bufsz, ltable | ||
115 | end -- load_index_file | ||
116 | -------------------------------------------------------------------------------- | ||
117 | |||
118 | -- creates a fixed index with fixed line lengths, perfect for viewing hex files | ||
119 | -- not so great for reading text files but works as a fallback | ||
120 | local function load_fixed_index(bytesperline, filesz, bufsz) | ||
121 | local lnbyte = bytesperline | ||
122 | local count = (filesz + lnbyte - 1) / lnbyte + 1 | ||
123 | local idx_t = {} -- build index | ||
124 | for i = 0, filesz, bufsz do | ||
125 | idx_t[#idx_t + 1] = lnbyte * i | ||
126 | end | ||
127 | return lnbyte, filesz, count, bufsz, idx_t | ||
128 | end -- load_fixed_index | ||
129 | -------------------------------------------------------------------------------- | ||
130 | |||
131 | -- uses print_table to display a whole file | ||
132 | function print_file(filename, maxlinelen, settings) | ||
133 | |||
134 | if not filename then return end | ||
135 | local file = io.open('/' .. filename or "", "r") --read | ||
136 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
137 | maxlinelen = 33 | ||
138 | local hstr = filename | ||
139 | local ftable = {} | ||
140 | table.insert(ftable, 1, hstr) | ||
141 | |||
142 | local tline = #ftable + 1 | ||
143 | local remln = maxlinelen | ||
144 | local posln = 1 | ||
145 | |||
146 | for line in file:lines() do | ||
147 | if line then | ||
148 | if maxlinelen then | ||
149 | if line == "" then | ||
150 | ftable[tline] = ftable[tline] or "" | ||
151 | tline = tline + 1 | ||
152 | remln = maxlinelen | ||
153 | else | ||
154 | line = line:match("%w.+") or "" | ||
155 | end | ||
156 | local linelen = line:len() | ||
157 | while linelen > 0 do | ||
158 | |||
159 | local fsp = line:find("%s", posln + remln - 5) or 0x0 | ||
160 | fsp = fsp - (posln + remln) | ||
161 | if fsp >= 0 then | ||
162 | local fspr = fsp | ||
163 | fsp = line:find("%s", posln + remln) or linelen | ||
164 | fsp = fsp - (posln + remln) | ||
165 | if math.abs(fspr) < fsp then fsp = fspr end | ||
166 | end | ||
167 | if fsp > 5 or fsp < -5 then fsp = 0 end | ||
168 | |||
169 | local str = line:sub(posln, posln + remln + fsp) | ||
170 | local slen = str:len() | ||
171 | ftable[tline] = ftable[tline] or "" | ||
172 | ftable[tline] = ftable[tline] .. str | ||
173 | linelen = linelen - slen | ||
174 | if linelen > 0 then | ||
175 | tline = tline + 1 | ||
176 | posln = posln + slen | ||
177 | remln = maxlinelen | ||
178 | --loop continues | ||
179 | else | ||
180 | ftable[tline] = ftable[tline] .. " " | ||
181 | remln = maxlinelen - slen | ||
182 | posln = 1 | ||
183 | --loop ends | ||
184 | end | ||
185 | |||
186 | end | ||
187 | else | ||
188 | ftable[#ftable + 1] = line | ||
189 | end | ||
190 | |||
191 | |||
192 | end | ||
193 | end | ||
194 | |||
195 | file:close() | ||
196 | |||
197 | _lcd:clear() | ||
198 | _print.clear() | ||
199 | |||
200 | if not settings then | ||
201 | settings = {} | ||
202 | settings.justify = "center" | ||
203 | settings.wrap = true | ||
204 | settings.msel = true | ||
205 | end | ||
206 | settings.hasheader = true | ||
207 | settings.co_routine = nil | ||
208 | |||
209 | local sel = | ||
210 | print_table(ftable, #ftable, settings) | ||
211 | |||
212 | _lcd:splashf(rb.HZ * 2, "%d items {%s}", #sel, table.concat(sel, ", ")) | ||
213 | ftable = nil | ||
214 | end -- print_file | ||
215 | -------------------------------------------------------------------------------- | ||
216 | |||
217 | -- uses print_table to display a portion of a file | ||
218 | function print_file_increment(filename, settings) | ||
219 | |||
220 | if not filename then return end | ||
221 | local file = io.open('/' .. filename, "r") --read | ||
222 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
223 | local fsz = file:seek("end") | ||
224 | local bsz = 1023 | ||
225 | --if small file do it the easier way and load whole file to table | ||
226 | if fsz < 60 * 1024 then | ||
227 | file:close() | ||
228 | print_file(filename, settings) | ||
229 | return | ||
230 | end | ||
231 | |||
232 | local ext = ".idx" | ||
233 | local lnbyte, filesz, count, bufsz, idx_t = load_index_file(filename .. ext) | ||
234 | |||
235 | if not idx_t or fsz ~= filesz then -- build file index | ||
236 | build_file_index(filename, ext, bsz) | ||
237 | lnbyte, filesz, count, bufsz, idx_t = load_index_file(filename .. ext) | ||
238 | end | ||
239 | |||
240 | -- if invalid or user canceled creation fallback to a fixed index | ||
241 | if not idx_t or fsz ~= filesz or count <= 0 then | ||
242 | _lcd:splashf(rb.HZ * 5, "Unable to read file index %s", filename .. ext) | ||
243 | lnbyte, filesz, count, bufsz, idx_t = load_fixed_index(32, fsz, bsz) | ||
244 | end | ||
245 | |||
246 | if not idx_t or fsz ~= filesz or count <= 0 then | ||
247 | _lcd:splashf(rb.HZ * 5, "Unable to load file %s", filename) | ||
248 | return | ||
249 | end | ||
250 | |||
251 | local hstr = filename | ||
252 | local file_t = setmetatable({},{__mode = "kv"}) --weak keys and values | ||
253 | -- this allows them to be garbage collected as space is needed | ||
254 | -- rebuilds when needed | ||
255 | local ovf = 0 | ||
256 | local lpos = 1 | ||
257 | local timer = _timer() | ||
258 | file:seek("set", 0) | ||
259 | |||
260 | function print_co() | ||
261 | while true do | ||
262 | collectgarbage("step") | ||
263 | file_t[1] = hstr --position 1 is ALWAYS header/title | ||
264 | |||
265 | for i = 1, bufsz + ovf do | ||
266 | file_t[lpos + i] = file:read ("*l") | ||
267 | end | ||
268 | ovf = 0 | ||
269 | lpos = lpos + bufsz | ||
270 | |||
271 | local bpos = coroutine.yield() | ||
272 | |||
273 | if bpos <= lpos then -- roll over or scroll up | ||
274 | bpos = (bpos - bufsz) + bpos % bufsz | ||
275 | timer:check(true) | ||
276 | end | ||
277 | |||
278 | lpos = bpos - bpos % bufsz | ||
279 | |||
280 | if lpos < 1 then | ||
281 | lpos = 1 | ||
282 | elseif lpos > count - bufsz then -- partial fill | ||
283 | ovf = count - bufsz - lpos | ||
284 | end | ||
285 | --get position in file of the nearest indexed line | ||
286 | file:seek("set", idx_t[bpos / bufsz + 1]) | ||
287 | |||
288 | -- on really large files if it has been more than 10 minutes | ||
289 | -- since the user scrolled up the screen wipe out the prior | ||
290 | -- items to free memory | ||
291 | if lpos % 5000 == 0 and timer:check() > rb.HZ * 600 then | ||
292 | for i = 1, lpos - 100 do | ||
293 | file_t[i] = nil | ||
294 | end | ||
295 | end | ||
296 | |||
297 | end | ||
298 | end | ||
299 | |||
300 | co = coroutine.create(print_co) | ||
301 | _lcd:clear() | ||
302 | _print.clear() | ||
303 | |||
304 | if not settings then | ||
305 | settings = {} | ||
306 | settings.justify = "center" | ||
307 | settings.wrap = true | ||
308 | end | ||
309 | settings.hasheader = true | ||
310 | settings.co_routine = co | ||
311 | settings.msel = false | ||
312 | |||
313 | table.insert(file_t, 1, hstr) --position 1 is header/title | ||
314 | local sel = | ||
315 | print_table(file_t, count, settings) | ||
316 | file:close() | ||
317 | idx_t = nil | ||
318 | file_t = nil | ||
319 | return sel | ||
320 | end --print_file_increment | ||
321 | -------------------------------------------------------------------------------- | ||
322 | function print_file_hex(filename, bytesperline, settings) | ||
323 | |||
324 | if not filename then return end | ||
325 | local file = io.open('/' .. filename, "r") --read | ||
326 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
327 | local hstr = filename | ||
328 | local bpl = bytesperline | ||
329 | local fsz = file:seek("end") | ||
330 | --[[ | ||
331 | local filesz = file:seek("end") | ||
332 | local bufsz = 1023 | ||
333 | local lnbyte = bytesperline | ||
334 | local count = (filesz + lnbyte - 1) / lnbyte + 1 | ||
335 | |||
336 | local idx_t = {} -- build index | ||
337 | for i = 0, filesz, bufsz do | ||
338 | idx_t[#idx_t + 1] = lnbyte * i | ||
339 | end]] | ||
340 | |||
341 | local lnbyte, filesz, count, bufsz, idx_t = load_fixed_index(bpl, fsz, 1023) | ||
342 | |||
343 | local file_t = setmetatable({},{__mode = "kv"}) --weak keys and values | ||
344 | -- this allows them to be garbage collected as space is needed | ||
345 | -- rebuilds when needed | ||
346 | local ovf = 0 | ||
347 | local lpos = 1 | ||
348 | local timer = _timer() | ||
349 | file:seek("set", 0) | ||
350 | |||
351 | function hex_co() | ||
352 | while true do | ||
353 | collectgarbage("step") | ||
354 | file_t[1] = hstr --position 1 is ALWAYS header/title | ||
355 | |||
356 | for i = 1, bufsz + ovf do | ||
357 | local pos = file:seek() | ||
358 | local s = file:read (lnbyte) | ||
359 | if not s then -- EOF | ||
360 | file_t[lpos + i] = "" | ||
361 | break; | ||
362 | end | ||
363 | local s_len = s:len() | ||
364 | |||
365 | if s_len > 0 then | ||
366 | local fmt = "0x%04X: " .. string.rep("%02X ", s_len) | ||
367 | local schrs = " " .. s:gsub("(%c)", " . ") | ||
368 | file_t[lpos + i] = string.format(fmt, pos, s:byte(1, s_len)) .. | ||
369 | schrs | ||
370 | else | ||
371 | file_t[lpos + i] = string.format("0x%04X: ", pos) | ||
372 | end | ||
373 | end | ||
374 | ovf = 0 | ||
375 | lpos = lpos + bufsz | ||
376 | |||
377 | local bpos = coroutine.yield() | ||
378 | |||
379 | if bpos < lpos then -- roll over or scroll up | ||
380 | bpos = (bpos - bufsz) + bpos % bufsz | ||
381 | timer:check(true) | ||
382 | end | ||
383 | |||
384 | lpos = bpos - bpos % bufsz | ||
385 | |||
386 | if lpos < 1 then | ||
387 | lpos = 1 | ||
388 | elseif lpos > count - bufsz then -- partial fill | ||
389 | ovf = count - bufsz - lpos | ||
390 | end | ||
391 | --get position in file of the nearest indexed line | ||
392 | file:seek("set", idx_t[bpos / bufsz + 1]) | ||
393 | |||
394 | -- on really large files if it has been more than 10 minutes | ||
395 | -- since the user scrolled up the screen wipe out the prior | ||
396 | -- items to free memory | ||
397 | if lpos % 10000 == 0 and timer:check() > rb.HZ * 600 then | ||
398 | for i = 1, lpos - 100 do | ||
399 | file_t[i] = nil | ||
400 | end | ||
401 | end | ||
402 | |||
403 | end | ||
404 | end | ||
405 | |||
406 | co = coroutine.create(hex_co) | ||
407 | |||
408 | local function repl(char) | ||
409 | local ret = "" | ||
410 | if char:sub(1,2) == "0x" then | ||
411 | return string.format("%dd:", tonumber(char:sub(3, -2), 16)) | ||
412 | else | ||
413 | return string.format("%03d ", tonumber(char, 16)) | ||
414 | end | ||
415 | end | ||
416 | |||
417 | |||
418 | _lcd:clear() | ||
419 | _print.clear() | ||
420 | |||
421 | local sel, start, vcur = 1 | ||
422 | table.insert(file_t, 1, hstr) --position 1 is header/title | ||
423 | |||
424 | if not settings then | ||
425 | settings = {} | ||
426 | settings.justify = "left" | ||
427 | settings.wrap = true | ||
428 | settings.msel = false | ||
429 | settings.hfgc = _clr.set( 0, 000, 000, 000) | ||
430 | settings.hbgc = _clr.set(-1, 255, 255, 255) | ||
431 | settings.ifgc = _clr.set(-1, 255, 255, 255) | ||
432 | settings.ibgc = _clr.set( 0, 000, 000, 000) | ||
433 | settings.iselc = _clr.set( 1, 000, 200, 100) | ||
434 | end | ||
435 | |||
436 | settings.hasheader = true | ||
437 | settings.co_routine = co | ||
438 | settings.start = start | ||
439 | settings.curpos = vcur | ||
440 | |||
441 | while sel > 0 do | ||
442 | settings.start = start | ||
443 | settings.curpos = vcur | ||
444 | |||
445 | sel, start, vcur = print_table(file_t, count, settings) | ||
446 | |||
447 | if sel > 1 and file_t[sel] then -- flips between hex and decimal | ||
448 | local s = file_t[sel] | ||
449 | if s:sub(-1) == "\b" then | ||
450 | file_t[sel] = nil | ||
451 | ovf = -(bufsz - 1) | ||
452 | coroutine.resume(co, sel) --rebuild this item | ||
453 | else | ||
454 | s = s:gsub("(0x%x+:)", repl) .. "\b" | ||
455 | file_t[sel] = s:gsub("(%x%x%s)", repl) .. "\b" | ||
456 | end | ||
457 | end | ||
458 | end | ||
459 | |||
460 | file:close() | ||
461 | idx_t = nil | ||
462 | file_t = nil | ||
463 | return sel | ||
464 | end -- print_file_hex | ||
465 | -------------------------------------------------------------------------------- | ||