-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmrl-view
executable file
·744 lines (629 loc) · 19.2 KB
/
mrl-view
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#!/usr/bin/env luajit
-- todo
-- creating new (text) files
-- text editing
-- mark modified and unsaved files in file browser, keep clients open, permitting multitask editing with minimalistic - what is on disk, driven ui.
-- ask to save changes when trying to switch file or perhaps dir?
-- run mrg- mmm- mrl- and luajit scripts directly
-- open context menu with keyboard shortcut
-- escape paths
-- thumbnails
-- thumbnails should be scaled stored rendered framebuffers
-- thus making it seems like the viewer is already running
-- in the thumbnail
-- stick a .mrg file in each dir
-- containing per filename extra information/cache
-- order for custom order sort (useful for slideshows and playlists)
-- position and size/rotation for 2d-view
-- use hardlinks when copying .jpg or .png or .mp3 files on the same file system to reduce disk usage overhead
local S = require('syscall')
-- having this here, enables live-coding, changing the mrl-view file should cause a reload of mrl-view
if true then
S.setenv('MRG_RESTARTER','yes')
S.setenv('MRG_BACKEND','mmm')
end
local Math = require('math')
local Mrg = require('mrg')
local mrg = Mrg.new(1024, 768);
--local mrg = Mrg.new(-1, -1);
local mime = require('mime')
local mrl = require('mrl')
local mime_handler = {} -- go to the bottom of the file to see the list of
-- mimehandlers
local state={
folder ='/home/pippin/src/mrg',
path ='/home/pippin/src/mrg/README.md',
query ='',
child_focus = false,
folder_pan=0
}
local dir = {}
local comp_dir = "/tmp/mrg-" .. S.getpid()
local host = mrg:host_new(comp_dir)
S.setenv("MMM_PATH","/tmp/mrg",1)
local css = [[
.pathbar { }
document { font-size: 2.5%; background: black; color: white; }
/* .folder {border: 1px solid red; } */
/*
.dentry {color:blue; 1px solid black}
.entry {border: 1px solid black }
*/
#current {
/*
border: 2px solid green ; */
color: yellow; }
.content { }
.size { width: 5em; display: block; float: left; }
.size_unit { color: gray }
.fname { display: block; float: left; width: 80%; }
]]
local old_pid = 0
function cleanup_child()
if old_pid ~= 0 then
S.kill(old_pid, 9)
S.waitpid(old_pid)
old_pid = 0
end
end
local current_child_path = nil
local last_w=400
local last_h=300
function dispatch_child (handler, mime_type)
if current_child_path == state.path then
return -- this is the reason some bugs can/could be worked around by setting a different item as active
end
current_child_path = state.path
cleanup_child()
print ('dispatch: ' .. state.path .. ' : '.. mime_type)
mrg:add_timeout(0, function() mrg:queue_draw(nil) return 0 end)
local childpid = S.fork()
if (childpid == 0) then -- in child
if mime_type == 'application/mmm' then
if handler.options then
S.execve(state.path,
{state.path, handler.options},
{"PATH=" .. S.getenv('PATH'),
"MMM_WIDTH=".. string.format("%d", last_w),
"MMM_HEIGHT=".. string.format("%d", last_h),
"MMM_PATH=" .. comp_dir})
else
S.execve(state.path, {state.path},
{"PATH=" .. S.getenv('PATH'),
"MMM_PATH=" .. comp_dir})
end
else
if handler.options then
S.execve(mrl.which(handler.execute),
{mrl.which(handler.execute), state.path, handler.options},
{"PATH=" .. S.getenv('PATH'),
"MMM_WIDTH=".. string.format("%d", last_w),
"MMM_HEIGHT=".. string.format("%d", last_h),
"MMM_PATH=" .. comp_dir})
else
S.execve(mrl.which(handler.execute), {mrl.which(handler.execute), state.path},
{"PATH=" .. S.getenv('PATH'),
"MMM_WIDTH=".. string.format("%d", last_w),
"MMM_HEIGHT=".. string.format("%d", last_h),
"MMM_PATH=" .. comp_dir})
end
end
S.exit()
else -- in parent
cleanup_child()
old_pid = childpid
S.nanosleep(0.09) -- XXX: eeek, avoiding construction race... needs fixing in mmm
end
end
function internal_image (handler)
-- for large images, external is better anyways,.
-- keeping the code for comparison purposes
local em = mrg:em()
local w, h = mrg:image_size(state.path)
local cr = mrg:cr()
local scale = 1.0;
local dw, dh;
local y = 40
cleanup_child()
scale = (mrg:width () - 8 * em) / w
dw, dh = w * scale, h * scale
if dw > mrg:width() - 8 * em then
scale = (mrg:width () - 8 * em) / w
dw, dh = w * scale, h * scale
end
if dh > mrg:height() - y then
scale = (mrg:height() - y) / h
dw, dh = w * scale, h * scale
end
cr:save()
cr:translate(8*em + ((mrg:width()-8 * em) - dw)/2,
(y + ((mrg:height()-y) - dh)/2))
cr:rectangle(0, 0, mrg:width() - 8 * em, mrg:height() - y)
cr:clip()
mrg:image(0, 0, dw, dh, state.path)
cr:restore()
mrg:queue_draw(nil)
end
function store_state()
S.setenv("MRL_STATE", mrl.serialize(state), 1)
end
function restore_state()
if (S.getenv("MRL_STATE")) then
state = loadstring('return ' .. S.getenv("MRL_STATE"))()
end
end
if (#arg >= 1) then
state.path = arg[1]
end
restore_state()
function get_parent(path)
local t = {}
local newp = '';
for str in string.gmatch(path, "([^/]+)") do
t[#t+1] = str
end
for i = 1,#t-1 do
newp = newp .. '/' .. t[i]
end
if newp == '' then newp = '/' end
return newp
end
function go_parent()
set_path (get_parent (state.path))
mrg:queue_draw(null)
end
function go_next()
local cursor = nil
for i,file in pairs(dir) do
if cursor and cursor.path == state.path then
set_path (file.path)
mrg:queue_draw(null)
return
end
cursor = file
end
end
function go_previous()
local cursor = nil
for i,file in pairs(dir) do
if file.path == state.path then
if cursor then
set_path (cursor.path)
mrg:queue_draw(null)
end
return
end
cursor = file
end
end
function collect_dir (path, query)
local dir = {}
local hide_dot = true
for str in string.gmatch(state.path, "(.*)/$") do
path = str
end
for name in S.util.ls(path) do
if name ~= '..' and name ~= '.' then
local combined_path = path .. '/' .. name
local file = {}
local stat = S.stat(combined_path)
if stat then
file.size = stat.size
file.isdir = stat.isdir
else
file.size = -1
file.isdir = false
end
file.path = combined_path
file.name = name
if not hide_dot or string.sub(file.name, 1, 1) ~= '.' then
if query == nil or query == '' then
table.insert(dir, file)
else
if string.match(file.name, '^' .. query) then
table.insert(dir, file)
end
end
end
end
end
table.sort(dir, function (a,b)
if a.isdir == false and b.isdir == true then
return false
elseif a.isdir == true and b.isdir == false then
return true
end
return a.name < b.name
end)
return dir
end
local editing_path = false
function set_path(new_path)
if string.match(new_path, "/$") then
--if stat and stat.isdir then
--state.folder = state.path
local dir = collect_dir (new_path)
if #dir > 0 then
new_path = new_path .. dir[1].name
else
new_path = new_path .. 'empty-dir'
end
--end
end
state.path = new_path
mrg:queue_draw(null)
mrg:set_title(new_path)
local old_folder = state.folder
print (new_path)
local stat = S.stat(state.path)
state.folder = get_parent(state.path)
if state.folder ~= old_folder then
state.query=''
state.folder_pan = 0
else
end
mrg:set_cursor_pos(#state.query)
store_state(state)
-- print ("path:" .. state.path .. "\n" .. "folder:" .. state.folder)
end
set_path (state.path)
function path_bar(mrg)
local cr = mrg:cr()
mrg:start('div.pathbar')
local t = {}
local newp = '';
mrg:text_listen(Mrg.PRESS,
function(event,d1,d2)
set_path '/'
return 0;
end)
mrg:print_xml("<span class='pathentry'> /</span> \n")
mrg:text_listen_done()
local sep = '';
local count = 0
for str in string.gmatch(state.folder, "([^/]+)") do count = count + 1 end
local i = 1
for str in string.gmatch(state.folder, "([^/]+)") do
local foo = newp .. '/' .. str
newp = foo
mrg:text_listen(Mrg.PRESS,
function(event,d1,d2)
set_path(foo)
mrg:queue_draw(null);
return 0;
end)
mrg:start('span.pathentry')
sep='/'
mrg:print(str .. sep)
mrg:close()
mrg:text_listen_done()
i = i + 1
end
if not state.child_focus then
mrg:edit_start(function(new)
state.query=new
end)
mrg:print(state.query)
mrg:edit_end()
else
mrg:print(state.query)
end
mrg:close()
end
function human_size(size)
if size < 1024 then
return (string.format("%1.0f<span class='size_unit'>b</span>", (size)))
elseif size < 1024 * 1024 then
return (string.format("%1.1f<span class='size_unit'>kb</span>", (size/1024)))
elseif size < 1024 * 1024 * 1024 then
return (string.format("%1.1f<span class='size_unit'>mb</span>", (size/1024/1024)))
else
return (string.format("%1.1f<span class='size_unit'>gb</span>", (size/1024/1024/1024)))
end
end
function get_path(path)
local t = {}
local newp = '';
for str in string.gmatch(path, "([^/]+)") do
t[#t+1] = str
end
for i = 1,#t-1 do
newp = newp .. '/' .. t[i]
end
if newp == '' then newp = '/' end
return newp
end
function get_basename(path)
local t = {}
local last = ''
for str in string.gmatch(path, "([^/]+)") do
t[#t+1] = str
last=str
end
return last
end
function get_thumb_path(path)
return get_path(path) .. '/.thumb/' .. get_basename(path)
end
function draw_folder(mrg, path, currpath, details)
local cr = mrg:cr()
cr:save()
cr:translate (0, state.folder_pan)
mrg:start('div.folder')
dir = collect_dir (path, state.query)
for i,file in pairs(dir) do
if file and file.path:has_suffix('_') then
else
mrg:text_listen(Mrg.TAP + Mrg.TAP_AND_HOLD,
function(event,d1,d2)
if event.type == Mrg.TAP then
-- default to set the path clicked
set_path (file.path)
else
local menu=
{{title=file.name, type='edit',
cb=function(new_text)
os.execute(string.format('mv "%s" "%s"', file.path, path .. '/' .. new_text))
mrl.modal_end (mrg)
mrg:queue_draw(nil)
end},
{title='open',
cb=function()
os.execute(string.format('xdg-open "%s"&', file.path))
end},
{title='remove', cb=function()
mrl.modal(mrg, mrg:pointer_x(), mrg:pointer_y(),
{
{title='really?'},
{title='yes',
cb=function()
os.execute('rm -f ' .. file.path)
end},
{title='no', cb=function() end},
})
end},
}
menu[#menu+1]={title='run',
cb=function()
os.execute(string.format('%s &', file.path))
end
}
-- per file context menu
mrl.modal(mrg, event.device_x, event.device_y, menu)
mrg:queue_draw(nil)
end
return 0;
end)
if string.match(file.path, '.JPG') then
local stt = S.stat(get_thumb_path(file.path))
if stt and stt.isreg then
mrg:image(mrg:x(), mrg:y() + mrg:em(), mrg:em()*8, mrg:em()*4,
get_thumb_path(file.path))
end
end
local xml = "<div style='height:4em;'"
if file.isdir then
xml = xml .. "class='dentry' "
else
xml = xml .. "class='entry' "
end
if ( file.path == currpath) then
xml = xml .. " id='current' "
end
xml = xml .. "><span class='fname'>" .. file.name .. "</span>"
if details then
if not file.isdir then
xml = xml .. "<span class='size'>" .. human_size(file.size) .. "</span>"
end
end
xml = xml .. "</div>\n"
mrg:print_xml(xml)
mrg:text_listen_done() -- maybe it is text listening for whole xml take make it icky?
if mrg:y() + state.folder_pan > mrg:height() then break end
end
end
mrg:close()
cr:restore()
end
mrg:set_ui(
function (mrg, data)
local cr = mrg:cr()
local em = mrg:em()
local stat = S.stat(state.path)
local x, y = 0, 0
if not stat then
path_bar(mrg)
print ('not a statable path['..state.path..'] (remove this message if it isnt usedful now)')
else
if stat.isdir or stat.isreg then
x, y = mrg:xy()
draw_folder(mrg, state.folder, state.path, false)
mrg:set_edge_right(mrg:width() - em)
mrg:set_edge_left(8 * em)
mrg:set_edge_top(y)
local mime_type = mime.for_path(state.path)
if stat.isdir then
mime_type = 'dir'
end
local handler = mime_handler[mime_type]
if handler then
if handler.cb then
handler.cb(handler)
else
dispatch_child (handler, mime_type)
end
end
cr:rectangle(0, y, 8 * mrg:em(), mrg:height())
mrg:listen(Mrg.DRAG, function(ev)
state.folder_pan = state.folder_pan + ev.delta_y
mrg:queue_draw(null)
end)
cr:new_path()
mrg:set_edge_right(mrg:width() - em)
mrg:set_edge_left(0)
mrg:set_edge_top(0)
mrg:set_xy(0,0)
path_bar(mrg)
end
end
mrg:add_binding("control-q", NULL, NULL, function (event) mrg:quit() end)
if state.child_focus then
mrg:add_binding("tab", NULL, NULL, function (event) -- XXX: dunno if "meta" is valid binding
state.child_focus = not state.child_focus
mrg:queue_draw(nil)
end )
else
mrg:add_binding("left", NULL, NULL, function (event)
go_parent()
event:stop_propagate()
end)
mrg:add_binding("right", NULL, NULL, function (event)
if S.stat(state.path).isdir then
set_path(state.path .. '/')
mrg:queue_draw(nil)
end
event:stop_propagate()
end)
mrg:add_binding("alt-left", NULL, NULL, function (event)
go_parent()
event:stop_propagate()
end)
mrg:add_binding("up", NULL, NULL, function (event)
go_previous()
event:stop_propagate()
end)
mrg:add_binding("down", NULL, NULL, function (event)
go_next()
event:stop_propagate()
end)
mrg:add_binding("escape", NULL, NULL, function (event)
if state.query == '' then
go_parent()
event:stop_propagate()
end
end)
mrg:add_binding("/", NULL, NULL, function (event)
local stat=S.stat(state.folder .. '/' .. state.query)
if stat and stat.isdir then
set_path(state.folder .. '/' .. state.query)
end
event:stop_propagate()
end)
mrg:add_binding("tab", NULL, NULL, function (event)
local matches={}
for i,file in pairs(dir) do
if string.has_prefix(file.name, state.query) then
table.insert(matches, file)
end
end
if #matches == 0 then
elseif #matches == 1 then
set_path(state.folder .. '/' .. matches[1].name)
else
local common=0
local done = false
while not done do
local first = matches[1].name:byte(common+1)
for i,v in pairs(matches) do
if v.name:byte(common+1) ~= first then
done = true
end
end
if not done then
common = common + 1
end
end
local new = matches[1].name:sub(1, common)
if state.query == new then
state.child_focus = not state.child_focus
mrg:queue_draw(nil)
else
state.query = new
mrg:add_timeout(0, function() mrg:set_cursor_pos(#state.query) return 0 end)
end
end
event:stop_propagate()
end)
mrg:add_binding("backspace", NULL, NULL, function (event)
if state.query == '' and state.path ~= '/' then
local tmp = string.match(state.folder, "([^/]+)$")
set_path(get_parent(state.folder))
state.query=tmp
-- not sure why this needs to go in a timeout
mrg:add_timeout(0, function() mrg:set_cursor_pos(#state.query) return 0 end)
end
end)
end
local clients = host:clients()
for i, client in ipairs(clients) do
local w, h = client:size()
local cx, cy = client:xy()
client:render(mrg, Math.floor(8 * mrg:em()), Math.floor(mrg:em()*1.2))
client:set_xy(8 * mrg:em(), y)
last_w = mrg:width()-(8 * mrg:em())
last_h = mrg:height()-(1.2 * mrg:em())
client:set_size(last_w, last_h)
if client:has_message() ~= 0 then
local message = client:get_message()
-- by having the all target, the compositor acts as a bus
-- and it is up to the clients to know that they are targetd
if message == 'next' then
go_next()
elseif message == 'previous' then
go_previous()
else
mrg:message('unhandled client message : ' .. message)
end
end
end
if state.child_focus then
cr:rectangle(0,0, 8 * em, mrg:height())
mrg:listen(Mrg.PRESS, function(event) state.child_focus = false end)
cr:new_path()
host:register_events()
else
cr:rectangle(8 * em, y, mrg:width() - 9 * em, mrg:height() - y)
mrg:listen(Mrg.PRESS, function(event) state.child_focus = true
end)
cr:new_path()
end
mrl.modal_draw (mrg)
mrg:listen(Mrg.MESSAGE, function(event)
local message = event:message()
if message == 'persist' then
store_state()
cleanup_child ()
end
end)
end)
mrg:css_set(css)
restore_state()
mrg:add_timeout(23000, function()
if slideshow == true then
go_next()
end
return 1
end)
mime_handler['video/mpeg4'] = {execute='mmm-ffplay'}
mime_handler['video/ogv'] = {execute='mmm-ffplay'}
mime_handler['video/mpeg'] = {execute='mmm-ffplay'}
mime_handler['video/theora'] = {execute='mmm-ffplay'}
mime_handler['text/plain'] = {execute='mrl-text-editor'}
--mime_handler['text/plain'] = {execute='mrl-text-editor2'}
mime_handler['image/gif'] = {execute='mmm-gifplay' } -- , options='--loop=1000'}
mime_handler['unknown'] = {execute='mrl-file-info'}
mime_handler['text/mrl'] = {execute='luajit'}
mime_handler['application/mmm'] = {execute='lua'}
mime_handler['image/svg'] = {execute='mrl-svg-view'}
mime_handler['text/html'] = {execute='mrl-html-view'}
--if false then
-- mime_handler['image/jpeg'] = {cb=internal_image}
-- mime_handler['image/png'] = {cb=internal_image}
--else
mime_handler['image/jpeg'] = {execute='mrl-image-view'}
mime_handler['image/png'] = {execute='mrl-image-view'}
--end
print ("[" .. state.path .. "]")
mrg:main()
cleanup_child()
host:destroy()