awful.util: add table.keys

Signed-off-by: Gregor Best <farhaven@googlemail.com>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Gregor Best 2009-07-02 02:04:59 +02:00 committed by Julien Danjou
parent b0975c2360
commit f337b88a73
1 changed files with 16 additions and 0 deletions

View File

@ -12,6 +12,8 @@ local loadstring = loadstring
local loadfile = loadfile local loadfile = loadfile
local debug = debug local debug = debug
local print = print local print = print
local pairs = pairs
local ipairs = ipairs
local type = type local type = type
local rtable = table local rtable = table
local pairs = pairs local pairs = pairs
@ -282,4 +284,18 @@ function linewrap(text, width, indent)
end) end)
end end
--- Get a sorted table with all integer keys from a table
-- @param t the table for which the keys to get
-- @return A table with keys
function table.keys(t)
local keys = { }
for k, _ in pairs(t) do
rtable.insert(keys, k)
end
rtable.sort(keys, function (a, b)
return type(a) == type(b) and a < b or false
end)
return keys
end
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80