Skip to content

Instantly share code, notes, and snippets.

@trentgill
Created March 29, 2024 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trentgill/3ec184e4c7aff5b5d1c7e1a42c6f566e to your computer and use it in GitHub Desktop.
Save trentgill/3ec184e4c7aff5b5d1c7e1a42c6f566e to your computer and use it in GitHub Desktop.
local ag =
{grid = {}
,fns = {}}
function ag.init(tab_or_g, fns)
if type(tab_or_g) == 'table' then
-- TODO for version with switchable layers
ag.grid = tab_or_g.grid
ag.fns = tab_or_g.fns
else -- assume grid obj
ag.grid = tab_or_g
ag.fns = fns -- save user provided table of actions
end
-- hook autogrid up to grid interface
ag.grid.key = ag.key
end
local function apply(f, ...)
return f(...)
end
local function maybe_fn_table(obj,...)
-- false signals the obj doesn't exist
if obj == nil then return false end
local typ = type(obj)
if typ == 'function' then
obj(...)
elseif typ == 'table' then
apply(unpack(obj),...)
end
-- true signals this mapping was eval'd
return true
end
function ag.key(x,y,z)
if maybe_fn_table(ag.fns[x .. ',' .. y],z) then return -- individual key
elseif maybe_fn_table(ag.fns['x' .. x],y,z) then return -- column
elseif maybe_fn_table(ag.fns['y' .. y],x,z) then return -- row
end
end
return ag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment