summaryrefslogtreecommitdiff
path: root/config/mpv/scripts/subs2srsa/utils/switch.lua
blob: 5dac1c6032902cf349b5189ba28678e25599f5c9 (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
--[[
Copyright: Ren Tatsumoto and contributors
License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html

Switch cycles between values in a table.
]]

local make_switch = function(states)
    local self = {
        states = states,
        current_state = 1
    }
    local bump = function()
        self.current_state = self.current_state + 1
        if self.current_state > #self.states then
            self.current_state = 1
        end
    end
    local get = function()
        return self.states[self.current_state]
    end
    local set = function(new_state)
        for idx, value in ipairs(self.states) do
            if value == new_state then
                self.current_state = idx
            end
        end
    end
    return {
        bump = bump,
        get = get,
        set = set,
    }
end

return {
    new = make_switch
}