core/filteredlist.lua
-- Copyright 2011-2012 Nils Nordman <nino at nordman.org>
-- Copyright 2012-2014 Robert Gieseke <rob.g@web.de>
-- License: MIT (see LICENSE)

--[[--
Filtered list wrapper, hijacking `ui.dialogs.list`.

@module textredux.core.filteredlist
]]

local list = require 'textredux.core.list'

local M = {}

local ui_filteredlist = ui.dialogs.list
local current_coroutine

local function convert_multi_column_table(nr_columns, items)
  local _items, _item = {}, {}
  for i, item in ipairs(items) do
    _item[#_item + 1] = item
    if i % nr_columns == 0 then
      _items[#_items + 1] = _item
      _item = {}
    end
  end
  return _items
end

local function index_of(element, table)
  for i, e  in ipairs(table) do
    if e == element then return i end
  end
end

ui.dialogs.list = function(options)
  if not current_coroutine then
    return ui_filteredlist(options)
  end
  local co = current_coroutine
  local title = options.title or ''
  local columns = options.columns
  local items = options.items or {}
  if columns then
    columns = type(columns) == 'string' and { columns } or columns
    if #columns > 1 then items = convert_multi_column_table(#columns, items) end
  end

  local l = list.new(title, items)
  if columns then l.headers = columns end
  l.on_selection = function(l, item)
    local value = index_of(item, items)
    l:close()
    coroutine.resume(co, value)
  end
  l:show()
  return coroutine.yield()
end

-- Wrap
function M.wrap(func)
  return function(...)
    current_coroutine = coroutine.create(func)
    local status, val = coroutine.resume(current_coroutine)
    current_coroutine = nil
    if not status then events.emit(events.ERROR, val) end
  end
end

return M
