summaryrefslogtreecommitdiff
path: root/state.lua
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2026-01-07 13:33:47 -0800
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2026-01-07 13:33:47 -0800
commitc442abc24ebbddaf8d9fab36375128e06547d65e (patch)
tree4f37b8ed27fd9d70091c6b925605bc70585d9114 /state.lua
parentd9efccd403dbb189e99f6c43fdad16940e695e30 (diff)
downloadmpv-iptv-menu-c442abc24ebbddaf8d9fab36375128e06547d65e.tar.gz
mpv-iptv-menu-c442abc24ebbddaf8d9fab36375128e06547d65e.tar.xz
move state to new module
Diffstat (limited to 'state.lua')
-rw-r--r--state.lua70
1 files changed, 70 insertions, 0 deletions
diff --git a/state.lua b/state.lua
new file mode 100644
index 0000000..472e4d2
--- /dev/null
+++ b/state.lua
@@ -0,0 +1,70 @@
+-- Copyright 2025 David Vazgenovich Shakaryan
+
+local state = {}
+local mt = {}
+mt.__index = mt
+
+function state.new()
+ return setmetatable({
+ menus = {},
+ depth = 0,
+
+ favourites = {},
+ playing_id = nil,
+ }, mt)
+end
+
+function mt:menu()
+ return self.menus[self.depth]
+end
+
+function mt:push_menu(t)
+ local menu = {
+ options = {},
+ cursor = 1,
+ view_top = 1,
+ }
+
+ for k, v in pairs(t) do
+ menu[k] = v
+ end
+
+ self.depth = self.depth + 1
+ self.menus[self.depth] = menu
+end
+
+-- returns index if found
+function mt:favourited(id)
+ for i, v in ipairs(self.favourites) do
+ if v == id then
+ return i
+ end
+ end
+end
+
+function mt:add_favourite(id)
+ self.favourites[#self.favourites+1] = id
+end
+
+function mt:remove_favourite_at(i)
+ table.remove(self.favourites, i)
+end
+
+-- inserts the given id into the favourites array before the next favourited
+-- menu option, starting from the next cursor position, or the end if no such
+-- option is found. this is meant for in-place favouriting from the favourites
+-- menu.
+function mt:insert_favourite_before_next_in_menu(id)
+ local menu = self:menu()
+ for i = menu.cursor+1, #menu.options do
+ local ind = self:favourited(menu.options[i].id)
+ if ind then
+ table.insert(self.favourites, ind, id)
+ return
+ end
+ end
+
+ self:add_favourite(id)
+end
+
+return state