summary refs log tree commit diff
path: root/.config/mvi/scripts/status-line.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-12-03 12:56:51 +0100
committerRobert Günzler <r@gnzler.io>2020-12-07 19:28:05 +0100
commit0eb11ed2c3dc235349b87d20bf657f2fb9729994 (patch)
treeceba665d591328bb47418ab82bb0de31abff98e5 /.config/mvi/scripts/status-line.lua
parente93cee56264c7e1d07b422d66430d996fd8b0c40 (diff)
Add bin/mvi and config; mpv-based image viewer
Diffstat (limited to '.config/mvi/scripts/status-line.lua')
-rw-r--r--.config/mvi/scripts/status-line.lua108
1 files changed, 108 insertions, 0 deletions
diff --git a/.config/mvi/scripts/status-line.lua b/.config/mvi/scripts/status-line.lua
new file mode 100644
index 0000000..bc8c129
--- /dev/null
+++ b/.config/mvi/scripts/status-line.lua
@@ -0,0 +1,108 @@
+local opts = {
+    enabled = true,
+    position = "bottom-left",
+    size = 36,
+    text = "${filename} [${playlist-pos-1}/${playlist-count}]",
+}
+(require 'mp.options').read_options(opts)
+
+local msg = require 'mp.msg'
+local assdraw = require 'mp.assdraw'
+
+local stale = true
+
+function draw_ass(ass)
+    local ww, wh = mp.get_osd_size()
+    mp.set_osd_ass(ww, wh, ass)
+end
+
+function refresh()
+    if not stale then return end
+    stale = false
+    local expanded = mp.command_native({ "expand-text", opts.text})
+    if not expanded then
+        msg.error("Error expanding status-line")
+        draw_ass("")
+        return
+    end
+    msg.verbose("Status-line changed to: " .. expanded)
+    local w,h = mp.get_osd_size()
+    local an, x, y
+    local margin = 10
+    if opts.position == "top-left" then
+        x = margin
+        y = margin
+        an = 7
+    elseif opts.position == "top-right" then
+        x = w-margin
+        y = margin
+        an = 9
+    elseif opts.position == "bottom-right" then
+        x = w-margin
+        y = h-margin
+        an = 3
+    elseif opts.position == "bottom-left" then
+        x = margin
+        y = h-margin
+        an = 1
+    else
+        msg.error("Invalid position: " .. opts.position)
+        return
+    end
+    local a = assdraw:ass_new()
+    a:new_event()
+    a:an(an)
+    a:pos(x,y)
+    a:append("{\\fs".. opts.size.. "}{\\bord1.0}")
+    a:append(expanded)
+    draw_ass(a.text)
+end
+
+function mark_stale()
+    stale = true
+end
+
+local active = false
+
+function enable()
+    if active then return end
+    active = true
+    local start = 0
+    while true do
+        local s, e, cap = string.find(opts.text, "%${[?!]?([%l%d-/]*)", start)
+        if not s then break end
+        msg.verbose("Observing property " .. cap)
+        mp.observe_property(cap, nil, mark_stale)
+        start = e
+    end
+    mp.observe_property("osd-width", nil, mark_stale)
+    mp.observe_property("osd-height", nil, mark_stale)
+    mp.register_idle(refresh)
+    mark_stale()
+end
+
+
+function disable()
+    if not active then return end
+    active = false
+    mp.unobserve_property(mark_stale)
+    mp.unregister_idle(refresh)
+    ass.status_line = ""
+    draw_ass()
+end
+
+function toggle()
+    if active then
+        disable()
+    else
+        enable()
+    end
+end
+
+if opts.enabled then
+    enable()
+end
+
+mp.add_key_binding(nil, "enable-status-line", enable)
+mp.add_key_binding(nil, "disable-status-line", disable)
+mp.add_key_binding(nil, "toggle-status-line", toggle)