summaryrefslogtreecommitdiff
path: root/lua/playlist_iterator.lua
blob: 06e80ad2ee683569d97dfe0aaee0db812f90d5df (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
local PlaylistIterator = {}

function PlaylistIterator:is_playlist(item)
  return item:filepath():match("%.playlist$")
      or item:filepath():match("%.m3u8?$")
end

function PlaylistIterator:create(fs_iterator)
  local iterator = fs_iterator:clone()
  local obj = {};

  local find_matching = function(iterate_fn)
    local next = iterate_fn(iterator);
    while next and (not PlaylistIterator:is_playlist(next) and not next:is_directory()) do
      next = iterate_fn();
    end
    return next;
  end

  function obj:clone()
    return PlaylistIterator:create(iterator)
  end

  function obj:next()
    return find_matching(iterator.next)
  end

  function obj:prev()
    return find_matching(iterator.prev)
  end

  return obj
end

return PlaylistIterator