Skip to content

Commit

Permalink
fix handing of tables with nil (#32)
Browse files Browse the repository at this point in the history
The `#` operator requires that all items in the table are not nil
  • Loading branch information
dyphire authored Jan 14, 2024
1 parent 20015d4 commit b51c086
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ local function update_tracks_menu(menu)
local function track_list_cb(_, track_list)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not track_list or #track_list == 0 then return end
if not track_list or next(track_list) == nil then return end

local items_v = build_track_items(track_list, 'video', 'vid', true)
local items_a = build_track_items(track_list, 'audio', 'aid', true)
Expand All @@ -177,7 +177,7 @@ local function update_track_menu(menu, type, prop)
local function track_list_cb(_, track_list)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not track_list or #track_list == 0 then return end
if not track_list or next(track_list) == nil then return end

local items = build_track_items(track_list, type, prop, false)
for _, item in ipairs(items) do table.insert(submenu, item) end
Expand All @@ -193,7 +193,7 @@ local function update_chapters_menu(menu)
local function chapter_list_cb(_, chapter_list)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not chapter_list or #chapter_list == 0 then return end
if not chapter_list or next(chapter_list) == nil then return end

local pos = mp.get_property_number('chapter', -1)
for id, chapter in ipairs(chapter_list) do
Expand Down Expand Up @@ -228,7 +228,7 @@ local function update_editions_menu(menu)
local function edition_list_cb(_, edition_list)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not edition_list or #edition_list == 0 then return end
if not edition_list or next(edition_list) == nil then return end

local current = mp.get_property_number('current-edition', -1)
for id, edition in ipairs(edition_list) do
Expand Down Expand Up @@ -262,7 +262,7 @@ local function update_audio_devices_menu(menu)
local function device_list_cb(_, device_list)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not device_list or #device_list == 0 then return end
if not device_list or next(device_list) == nil then return end

local current = mp.get_property('audio-device', '')
for _, device in ipairs(device_list) do
Expand Down Expand Up @@ -307,7 +307,7 @@ local function update_playlist_menu(menu)
local function playlist_cb(_, playlist)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not playlist or #playlist == 0 then return end
if not playlist or next(playlist) == nil then return end

for id, item in ipairs(playlist) do
submenu[#submenu + 1] = {
Expand All @@ -328,7 +328,7 @@ local function update_profiles_menu(menu)
local function profile_list_cb(_, profile_list)
for i = #submenu, 1, -1 do table.remove(submenu, i) end
menu_items_dirty = true
if not profile_list or #profile_list == 0 then return end
if not profile_list or next(profile_list) == nil then return end

for _, profile in ipairs(profile_list) do
if not (profile.name == 'default' or profile.name:find('gui') or
Expand Down Expand Up @@ -431,7 +431,7 @@ end

-- menu data update callback
local function menu_data_cb(name, items)
if not items or #items == 0 then return end
if not items or next(items) == nil then return end
mp.unobserve_property(menu_data_cb)

menu_items = items
Expand Down Expand Up @@ -470,7 +470,7 @@ mp.register_script_message('update', function(keyword, json)

local data, err = utils.parse_json(json)
if err then msg.error('update: failed to parse json:', err) end
if not data then
if not data or next(data) == nil then
msg.warn('update: ignored message with invalid json:', json)
return
end
Expand Down

0 comments on commit b51c086

Please sign in to comment.