summaryrefslogtreecommitdiff
path: root/mac/.config/mpv/script-modules/scroll-list.lua
blob: 5d8f9fa98d2eee51fb87f1c710d6b6c21f776b66 (plain)
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
local mp = require 'mp'
local scroll_list = {
    global_style = [[]],
    header_style = [[{\q2\fs35\c&00ccff&}]],
    list_style = [[{\q2\fs25\c&Hffffff&}]],
    wrapper_style = [[{\c&00ccff&\fs16}]],
    cursor_style = [[{\c&00ccff&}]],
    selected_style = [[{\c&Hfce788&}]],

    cursor = [[➤\h]],
    indent = [[\h\h\h\h]],

    num_entries = 16,
    wrap = false,
    empty_text = "no entries"
}

--formats strings for ass handling
--this function is based on a similar function from https://github.com/mpv-player/mpv/blob/master/player/lua/console.lua#L110
function scroll_list.ass_escape(str, replace_newline)
    if replace_newline == true then replace_newline = "\\\239\187\191n" end

    --escape the invalid single characters
    str = str:gsub('[\\{}\n]', {
        -- There is no escape for '\' in ASS (I think?) but '\' is used verbatim if
        -- it isn't followed by a recognised character, so add a zero-width
        -- non-breaking space
        ['\\'] = '\\\239\187\191',
        ['{'] = '\\{',
        ['}'] = '\\}',
        -- Precede newlines with a ZWNBSP to prevent ASS's weird collapsing of
        -- consecutive newlines
        ['\n'] = '\239\187\191\\N',
    })

    -- Turn leading spaces into hard spaces to prevent ASS from stripping them
    str = str:gsub('\\N ', '\\N\\h')
    str = str:gsub('^ ', '\\h')

    if replace_newline then
        str = str:gsub("\\N", replace_newline)
    end
    return str
end

--format and return the header string
function scroll_list:format_header_string(str)
    return str
end

--appends the entered text to the overlay
function scroll_list:append(text)
        if text == nil then return end
        self.ass.data = self.ass.data .. text
    end

--appends a newline character to the osd
function scroll_list:newline()
    self.ass.data = self.ass.data .. '\\N'
end

--re-parses the list into an ass string
--if the list is closed then it flags an update on the next open
function scroll_list:update()
    if self.hidden then self.flag_update = true
    else self:update_ass() end
end

--prints the header to the overlay
function scroll_list:format_header()
    self:append(self.header_style)
    self:append(self:format_header_string(self.header))
    self:newline()
end

--formats each line of the list and prints it to the overlay
function scroll_list:format_line(index, item)
    self:append(self.list_style)

    if index == self.selected then self:append(self.cursor_style..self.cursor..self.selected_style)
    else self:append(self.indent) end

    self:append(item.style)
    self:append(item.ass)
    self:newline()
end

--refreshes the ass text using the contents of the list
function scroll_list:update_ass()
    self.ass.data = self.global_style
    self:format_header()

    if #self.list < 1 then
        self:append(self.empty_text)
        self.ass:update()
        return
    end

    local start = 1
    local finish = start+self.num_entries-1

    --handling cursor positioning
    local mid = math.ceil(self.num_entries/2)+1
    if self.selected+mid > finish then
        local offset = self.selected - finish + mid

        --if we've overshot the end of the list then undo some of the offset
        if finish + offset > #self.list then
            offset = offset - ((finish+offset) - #self.list)
        end

        start = start + offset
        finish = finish + offset
    end

    --making sure that we don't overstep the boundaries
    if start < 1 then start = 1 end
    local overflow = finish < #self.list
    --this is necessary when the number of items in the dir is less than the max
    if not overflow then finish = #self.list end

    --adding a header to show there are items above in the list
    if start > 1 then self:append(self.wrapper_style..(start-1)..' item(s) above\\N\\N') end

    for i=start, finish do
        self:format_line(i, self.list[i])
    end

    if overflow then self:append('\\N'..self.wrapper_style..#self.list-finish..' item(s) remaining') end
    self.ass:update()
end

--moves the selector down the list
function scroll_list:scroll_down()
    if self.selected < #self.list then
        self.selected = self.selected + 1
        self:update_ass()
    elseif self.wrap then
        self.selected = 1
        self:update_ass()
    end
end

--moves the selector up the list
function scroll_list:scroll_up()
    if self.selected > 1 then
        self.selected = self.selected - 1
        self:update_ass()
    elseif self.wrap then
        self.selected = #self.list
        self:update_ass()
    end
end

--moves the selector to the list next page
function scroll_list:move_pagedown()
    if #self.list > self.num_entries then
        self.selected = self.selected + self.num_entries
        if self.selected > #self.list then self.selected = #self.list end
        self:update_ass()
    end
end

--moves the selector to the list previous page
function scroll_list:move_pageup()
    if #self.list > self.num_entries then
        self.selected = self.selected - self.num_entries
        if self.selected < 1 then self.selected = 1 end
        self:update_ass()
    end
end

--moves the selector to the list begin
function scroll_list:move_begin()
    if #self.list > 1 then
        self.selected = 1
        self:update_ass()
    end
end

--moves the selector to the list end
function scroll_list:move_end()
    if #self.list > 1 then
        self.selected = #self.list
        self:update_ass()
    end
end

--adds the forced keybinds
function scroll_list:add_keybinds()
    for _,v in ipairs(self.keybinds) do
        mp.add_forced_key_binding(v[1], 'dynamic/'..self.ass.id..'/'..v[2], v[3], v[4])
    end
end

--removes the forced keybinds
function scroll_list:remove_keybinds()
    for _,v in ipairs(self.keybinds) do
        mp.remove_key_binding('dynamic/'..self.ass.id..'/'..v[2])
    end
end

--opens the list and sets the hidden flag
function scroll_list:open_list()
    self.hidden = false
    if not self.flag_update then self.ass:update()
    else self.flag_update = false ; self:update_ass() end
end

--closes the list and sets the hidden flag
function scroll_list:close_list()
    self.hidden = true
    self.ass:remove()
end

--modifiable function that opens the list
function scroll_list:open()
    if self.hidden then self:add_keybinds() end
    self:open_list()
end

--modifiable function that closes the list
function scroll_list:close()
    self:remove_keybinds()
    self:close_list()
end

--toggles the list
function scroll_list:toggle()
    if self.hidden then self:open()
    else self:close() end
end

--clears the list in-place
function scroll_list:clear()
    local i = 1
    while self.list[i] do
        self.list[i] = nil
        i = i + 1
    end
end

--added alias for ipairs(list.list) for lua 5.1
function scroll_list:ipairs()
    return ipairs(self.list)
end

--append item to the end of the list
function scroll_list:insert(item)
    self.list[#self.list + 1] = item
end

local metatable = {
    __index = function(t, key)
        if scroll_list[key] ~= nil then return scroll_list[key]
        elseif key == "__current" then return t.list[t.selected]
        elseif type(key) == "number" then return t.list[key] end
    end,
    __newindex = function(t, key, value)
        if type(key) == "number" then rawset(t.list, key, value)
        else rawset(t, key, value) end
    end,
    __scroll_list = scroll_list,
    __len = function(t) return #t.list end,
    __ipairs = function(t) return ipairs(t.list) end
}

--creates a new list object
function scroll_list:new()
    local vars
    vars = {
        ass = mp.create_osd_overlay('ass-events'),
        hidden = true,
        flag_update = true,

        header = "header \\N ----------------------------------------------",
        list = {},
        selected = 1,

        keybinds = {
            {'DOWN', 'scroll_down', function() vars:scroll_down() end, {repeatable = true}},
            {'UP', 'scroll_up', function() vars:scroll_up() end, {repeatable = true}},
            {'PGDWN', 'move_pagedown', function() vars:move_pagedown() end, {}},
            {'PGUP', 'move_pageup', function() vars:move_pageup() end, {}},
            {'HOME', 'move_begin', function() vars:move_begin() end, {}},
            {'END', 'move_end', function() vars:move_end() end, {}},
            {'ESC', 'close_browser', function() vars:close() end, {}}
        }
    }
    return setmetatable(vars, metatable)
end

return scroll_list:new()