Debian Codegraph @

obvious/music/init.lua

raw symbols SCIP
1 local backends = require 'obvious.music.backends'
2
3 local awful = require 'awful'
4 local naughty = require 'naughty'
5 local markup = require 'obvious.lib.markup'
6 local hooks = require 'obvious.lib.hooks'
7 local unicode = require 'obvious.lib.unicode'
8 local markup_rope = require 'obvious.lib.markup_rope'
9 local wibox = require 'wibox'
10
11 local min = math.min
12
13 local widget = wibox.widget.textbox()
14 local backend
15 local marquee_timer
16
17 local format = '$title - $album - $artist'
18 local maxlength = 75
19 local unknown = '(unknown)'
20 local marquee = false
21
22 local icons = {
23 playing = '⏵',
24 paused = '⏸',
25 stopped = '⏹',
26 }
27
28 local function format_metadata(format, state, info)
29 if type(format) == 'function' then
30 return format(info)
31 end
32
33 info = setmetatable({ icon = icons[state] }, { __index = info })
34
35 assert(type(format) == 'string')
36
37 return string.gsub(format, '%$(%w+)', function(key)
38 return awful.util.escape(info[key] or unknown)
39 end)
40 end
41
42 local function rotate_markup_string(s, maxlength)
43 return function(rope, rotation_amount)
44 local first_chunk_length = min(rope:len() - rotation_amount, maxlength)
45
46 local rotated = rope:sub(rotation_amount + 1, rotation_amount + first_chunk_length + 1)
47
48 if first_chunk_length < maxlength then
49 rotated = rotated .. rope:sub(1, min(maxlength - first_chunk_length, rotation_amount))
50 end
51
52 return (rotation_amount % rope:len()) + 1, rotated
53 end, markup_rope(s), 1
54 end
55
56 local function scroll_marquee(prefix, s, suffix)
57 local maxlength = maxlength - markup_rope(prefix):len() - markup_rope(suffix):len()
58 for _, rotated in rotate_markup_string(s, maxlength - 3) do
59 widget:set_markup(prefix .. rotated .. '...' .. suffix)
60 local current_width = widget:get_preferred_size()
61 if current_width > (widget.forced_width or 0) then
62 widget.forced_width = current_width
63 end
64
65 coroutine.yield()
66 end
67 end
68
69 local function parse_marquee(format)
70 local start, finish, inner = string.find(format, '<marquee>(.*)</marquee>')
71
72 if not start then
73 return '', format, ''
74 end
75
76 return string.sub(format, 1, start - 1), inner, string.sub(format, finish + 1)
77 end
78
79 local function update(info)
80 widget.forced_width = nil
81 if marquee_timer then
82 hooks.timer.unregister(marquee_timer)
83 marquee_timer = nil
84 end
85
86 if not info then
87 widget:set_markup(markup.fg.color('yellow', 'Music Off'))
88 return
89 end
90
91 if info.state == 'stopped' then
92 widget:set_markup 'Music Stopped'
93 return
94 end
95
96 local formatted = format_metadata(format, info.state, info.info)
97
98 if unicode.length(formatted) > maxlength then
99 if marquee then
100 local marquee_coro = coroutine.create(scroll_marquee)
101 local prefix, marquee_text, suffix = parse_marquee(formatted)
102 local ok, err = coroutine.resume(marquee_coro, prefix, ' ' .. marquee_text, suffix)
103 if not ok then
104 naughty.notify {
105 title = 'Obvious',
106 text = 'Error: ' .. tostring(err),
107 preset = naughty.config.presets.critical,
108 }
109 return
110 end
111
112 marquee_timer = function()
113 local ok, err = coroutine.resume(marquee_coro)
114 if not ok then
115 naughty.notify {
116 title = 'Obvious',
117 text = 'Error: ' .. tostring(err),
118 preset = naughty.config.presets.critical,
119 }
120 hooks.timer.unregister(marquee_timer)
121 marquee_timer = nil
122 end
123 end
124 hooks.timer.register(1, nil, marquee_timer, 'Marquee Timer')
125 return
126 else
127 formatted = markup_rope(formatted):sub(1, maxlength - 3) .. '...'
128 end
129 end
130
131 formatted = table.concat { parse_marquee(formatted) }
132
133 widget:set_markup(formatted)
134 end
135
136 local _M = {}
137
138 function _M.set_format(format_)
139 format = format_
140 end
141
142 function _M.set_length(length_)
143 maxlength = length_
144 end
145
146 function _M.set_unknown(unknown_)
147 unknown = unknown_
148 end
149
150 function _M.set_marquee(marquee_)
151 marquee = marquee_
152 end
153
154 function _M.set_backend(backend_name)
155 local be = assert(backends[backend_name],
156 string.format('Backend %q is not a valid backend', tostring(backend_name)))
157
158 local err
159 be, err = be:configure()
160 assert(be, err or string.format('Unable to set up backend %q', tostring(backend_name)))
161 backend = be
162
163 backend:ontrackchange(update)
164 end
165
166 return setmetatable(_M, { __call = function() return widget end })