2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- Utility module for awful
|
|
|
|
--
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
-- @module awful.util
|
2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local os = os
|
|
|
|
local io = io
|
|
|
|
local assert = assert
|
2016-02-07 15:02:25 +01:00
|
|
|
local load = loadstring or load -- luacheck: globals loadstring (compatibility with Lua 5.1)
|
2008-09-30 17:00:19 +02:00
|
|
|
local loadfile = loadfile
|
2008-09-29 16:49:18 +02:00
|
|
|
local debug = debug
|
2009-07-02 02:04:59 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local ipairs = ipairs
|
2008-09-30 15:50:41 +02:00
|
|
|
local type = type
|
2009-04-15 10:31:41 +02:00
|
|
|
local rtable = table
|
2009-05-27 17:49:20 +02:00
|
|
|
local string = string
|
2015-07-11 00:09:45 +02:00
|
|
|
local lgi = require("lgi")
|
2016-05-16 06:57:15 +02:00
|
|
|
local grect = require("gears.geometry").rectangle
|
2015-10-11 13:14:15 +02:00
|
|
|
local Gio = require("lgi").Gio
|
2015-07-11 00:09:45 +02:00
|
|
|
local Pango = lgi.Pango
|
2008-09-29 16:49:18 +02:00
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
awesome = awesome,
|
|
|
|
mouse = mouse
|
|
|
|
}
|
2015-12-22 19:28:05 +01:00
|
|
|
local gears_debug = require("gears.debug")
|
2015-08-12 14:09:45 +02:00
|
|
|
local floor = math.floor
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
local util = {}
|
|
|
|
util.table = {}
|
2009-04-15 10:46:34 +02:00
|
|
|
|
2012-11-30 22:46:55 +01:00
|
|
|
util.shell = os.getenv("SHELL") or "/bin/sh"
|
2009-05-09 13:28:57 +02:00
|
|
|
|
2015-02-09 20:23:03 +01:00
|
|
|
local displayed_deprecations = {}
|
|
|
|
--- Display a deprecation notice, but only once per traceback.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] see The message to a new method / function to use.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.deprecate(see)
|
2015-02-09 20:23:03 +01:00
|
|
|
local tb = debug.traceback()
|
2015-02-15 10:52:35 +01:00
|
|
|
if displayed_deprecations[tb] then
|
2015-02-09 20:23:03 +01:00
|
|
|
return
|
|
|
|
end
|
2015-02-15 10:52:35 +01:00
|
|
|
displayed_deprecations[tb] = true
|
2015-07-23 21:51:42 +02:00
|
|
|
|
|
|
|
-- Get function name/desc from caller.
|
|
|
|
local info = debug.getinfo(2, "n")
|
|
|
|
local funcname = info.name or "?"
|
2015-12-22 19:28:05 +01:00
|
|
|
local msg = "awful: function " .. funcname .. " is deprecated"
|
2008-11-14 12:09:35 +01:00
|
|
|
if see then
|
2016-04-19 22:37:14 +02:00
|
|
|
if string.sub(see, 1, 3) == 'Use' then
|
|
|
|
msg = msg .. ". " .. see
|
|
|
|
else
|
|
|
|
msg = msg .. ", see " .. see
|
|
|
|
end
|
2008-11-14 12:09:35 +01:00
|
|
|
end
|
2015-12-22 19:28:05 +01:00
|
|
|
gears_debug.print_warning(msg .. ".\n" .. tb)
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2015-07-11 00:09:45 +02:00
|
|
|
--- Get a valid color for Pango markup
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @param color The color.
|
2015-07-11 00:09:45 +02:00
|
|
|
-- @tparam string fallback The color to return if the first is invalid. (default: black)
|
|
|
|
-- @treturn string color if it is valid, else fallback.
|
|
|
|
function util.ensure_pango_color(color, fallback)
|
|
|
|
color = tostring(color)
|
|
|
|
return Pango.Color.parse(Pango.Color(), color) and color or fallback or "black"
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Make i cycle.
|
2011-11-09 21:34:30 +01:00
|
|
|
-- @param t A length. Must be greater than zero.
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @param i An absolute index to fit into #t.
|
2011-11-09 21:34:30 +01:00
|
|
|
-- @return An integer in (1, t) or nil if t is less than or equal to zero.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.cycle(t, i)
|
2011-11-09 21:34:30 +01:00
|
|
|
if t < 1 then return end
|
2013-01-12 21:08:50 +01:00
|
|
|
i = i % t
|
|
|
|
if i == 0 then
|
|
|
|
i = t
|
|
|
|
end
|
2008-09-29 16:49:18 +02:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a directory
|
|
|
|
-- @param dir The directory.
|
|
|
|
-- @return mkdir return code
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.mkdir(dir)
|
2008-09-29 16:49:18 +02:00
|
|
|
return os.execute("mkdir -p " .. dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Eval Lua code.
|
|
|
|
-- @return The return value of Lua code.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.eval(s)
|
2012-06-16 14:22:35 +02:00
|
|
|
return assert(load(s))()
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2009-02-15 20:12:44 +01:00
|
|
|
local xml_entity_names = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
|
2008-09-29 16:49:18 +02:00
|
|
|
--- Escape a string from XML char.
|
|
|
|
-- Useful to set raw text in textbox.
|
|
|
|
-- @param text Text to escape.
|
|
|
|
-- @return Escape text.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.escape(text)
|
2009-01-28 10:05:26 +01:00
|
|
|
return text and text:gsub("['&<>\"]", xml_entity_names) or nil
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2009-02-15 20:12:44 +01:00
|
|
|
local xml_entity_chars = { lt = "<", gt = ">", nbsp = " ", quot = "\"", apos = "'", ndash = "-", mdash = "-", amp = "&" };
|
2008-09-29 16:49:18 +02:00
|
|
|
--- Unescape a string from entities.
|
|
|
|
-- @param text Text to unescape.
|
|
|
|
-- @return Unescaped text.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.unescape(text)
|
2009-01-28 10:05:26 +01:00
|
|
|
return text and text:gsub("&(%a+);", xml_entity_chars) or nil
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2008-09-30 15:50:41 +02:00
|
|
|
--- Check if a file is a Lua valid file.
|
2008-09-30 17:00:19 +02:00
|
|
|
-- This is done by loading the content and compiling it with loadfile().
|
2008-09-30 15:50:41 +02:00
|
|
|
-- @param path The file path.
|
|
|
|
-- @return A function if everything is alright, a string with the error
|
|
|
|
-- otherwise.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.checkfile(path)
|
2008-09-30 17:07:25 +02:00
|
|
|
local f, e = loadfile(path)
|
2008-09-30 15:50:41 +02:00
|
|
|
-- Return function if function, otherwise return error.
|
|
|
|
if f then return f end
|
|
|
|
return e
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Try to restart awesome.
|
|
|
|
-- It checks if the configuration file is valid, and then restart if it's ok.
|
|
|
|
-- If it's not ok, the error will be returned.
|
|
|
|
-- @return Never return if awesome restart, or return a string error.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.restart()
|
|
|
|
local c = util.checkfile(capi.awesome.conffile)
|
2008-09-30 15:50:41 +02:00
|
|
|
|
|
|
|
if type(c) ~= "function" then
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
|
|
|
capi.awesome.restart()
|
|
|
|
end
|
|
|
|
|
2015-11-28 19:33:33 +01:00
|
|
|
--- Get the config home according to the XDG basedir specification.
|
|
|
|
-- @return the config home (XDG_CONFIG_HOME) with a slash at the end.
|
|
|
|
function util.get_xdg_config_home()
|
|
|
|
return (os.getenv("XDG_CONFIG_HOME") or os.getenv("HOME") .. "/.config") .. "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the cache home according to the XDG basedir specification.
|
|
|
|
-- @return the cache home (XDG_CACHE_HOME) with a slash at the end.
|
|
|
|
function util.get_xdg_cache_home()
|
|
|
|
return (os.getenv("XDG_CACHE_HOME") or os.getenv("HOME") .. "/.cache") .. "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the path to the user's config dir.
|
|
|
|
-- This is the directory containing the configuration file ("rc.lua").
|
|
|
|
-- @return A string with the requested path with a slash at the end.
|
|
|
|
function util.get_configuration_dir()
|
|
|
|
return capi.awesome.conffile:match(".*/") or "./"
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the path to a directory that should be used for caching data.
|
|
|
|
-- @return A string with the requested path with a slash at the end.
|
|
|
|
function util.get_cache_dir()
|
|
|
|
return util.get_xdg_cache_home() .. "awesome/"
|
|
|
|
end
|
|
|
|
|
2015-12-11 18:24:30 +01:00
|
|
|
--- Get the path to the directory where themes are installed.
|
|
|
|
-- @return A string with the requested path with a slash at the end.
|
|
|
|
function util.get_themes_dir()
|
|
|
|
return "@AWESOME_THEMES_PATH@" .. "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the path to the directory where our icons are installed.
|
|
|
|
-- @return A string with the requested path with a slash at the end.
|
|
|
|
function util.get_awesome_icon_dir()
|
|
|
|
return "@AWESOME_ICON_PATH@" .. "/"
|
|
|
|
end
|
|
|
|
|
2009-02-15 20:12:44 +01:00
|
|
|
--- Get the user's config or cache dir.
|
|
|
|
-- It first checks XDG_CONFIG_HOME / XDG_CACHE_HOME, but then goes with the
|
|
|
|
-- default paths.
|
|
|
|
-- @param d The directory to get (either "config" or "cache").
|
|
|
|
-- @return A string containing the requested path.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.getdir(d)
|
2008-10-22 17:56:51 +02:00
|
|
|
if d == "config" then
|
2015-11-28 19:33:33 +01:00
|
|
|
-- No idea why this is what is returned, I recommend everyone to use
|
|
|
|
-- get_configuration_dir() instead
|
|
|
|
return util.get_xdg_config_home() .. "awesome/"
|
2008-10-22 17:56:51 +02:00
|
|
|
elseif d == "cache" then
|
2015-11-28 19:33:33 +01:00
|
|
|
return util.get_cache_dir()
|
Various changes to awful.util and invaders
This commit changes various aspects of awful.util and invaders:
awful.util:
- added awful.util.getdir(d)
This function takes one argument and returns a matching directory,
as of now, only "cache" is supported. The return value is either
$XDG_CACHE_HOME/awesome/ or $HOME/.cache/awesome/, XDG_CACHE_HOME
takes precedence
invaders:
- renamed invaders to awesome invaders
at two places in the sourcecode, invaders is referred to as
"Space Invaders for Awesome". As Taiko holds the trademark for
the term "Space Invaders", I changed both of its occurences to
"Awesome Invaders" to avoid conflicts with the law of Japan and
the United States of America (and possibly others)
- added support for XDG_CACHE_HOME
this change adds support for XDG_CACHE_HOME as the cache directory
for highscores and screenshots
- added some parameters to invaders.run()
this change adds three parameters to invaders.run, supplied as a
table. They are "x", "y" and "solidbg".
"x" sets the X coordinate of the playfield
"y" sets the Y coordinate of the playfield
"solidbg" sets the color of the playfield background for people who
have problems with transparency. This still looks rather hackish and
needs to be polished
- changed startup position
up until now, invaders always started at (100,100) on the first
screen, now it starts centered to the screen on which the mouse cursor
is.
2008-10-17 19:05:57 +02:00
|
|
|
end
|
|
|
|
end
|
2008-11-22 17:23:03 +01:00
|
|
|
|
2011-11-02 02:42:37 +01:00
|
|
|
--- Search for an icon and return the full path.
|
2015-02-26 15:40:00 +01:00
|
|
|
-- It searches for the icon path under the given directories with respect to the
|
|
|
|
-- given extensions for the icon filename.
|
2011-11-02 02:42:37 +01:00
|
|
|
-- @param iconname The name of the icon to search for.
|
|
|
|
-- @param exts Table of image extensions allowed, otherwise { 'png', gif' }
|
|
|
|
-- @param dirs Table of dirs to search, otherwise { '/usr/share/pixmaps/' }
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @tparam[opt] string size The size. If this is specified, subdirectories `x`
|
2015-02-26 15:40:00 +01:00
|
|
|
-- of the dirs are searched first.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.geticonpath(iconname, exts, dirs, size)
|
2016-02-07 15:02:25 +01:00
|
|
|
exts = exts or { 'png', 'gif' }
|
|
|
|
dirs = dirs or { '/usr/share/pixmaps/', '/usr/share/icons/hicolor/' }
|
2014-05-03 11:15:14 +02:00
|
|
|
local icontypes = { 'apps', 'actions', 'categories', 'emblems',
|
|
|
|
'mimetypes', 'status', 'devices', 'extras', 'places', 'stock' }
|
2011-11-02 02:42:37 +01:00
|
|
|
for _, d in pairs(dirs) do
|
2014-04-11 23:09:10 +02:00
|
|
|
local icon
|
2011-11-02 02:42:37 +01:00
|
|
|
for _, e in pairs(exts) do
|
2012-05-28 19:16:11 +02:00
|
|
|
icon = d .. iconname .. '.' .. e
|
2012-06-12 20:13:09 +02:00
|
|
|
if util.file_readable(icon) then
|
2011-11-02 02:42:37 +01:00
|
|
|
return icon
|
2012-09-14 21:49:14 +02:00
|
|
|
end
|
2014-04-11 23:09:10 +02:00
|
|
|
if size then
|
|
|
|
for _, t in pairs(icontypes) do
|
|
|
|
icon = string.format("%s%ux%u/%s/%s.%s", d, size, size, t, iconname, e)
|
|
|
|
if util.file_readable(icon) then
|
|
|
|
return icon
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-14 21:49:14 +02:00
|
|
|
end
|
|
|
|
end
|
2011-11-02 02:42:37 +01:00
|
|
|
end
|
|
|
|
|
2015-02-09 20:17:40 +01:00
|
|
|
--- Check if a file exists, is not readable and not a directory.
|
|
|
|
-- @param filename The file path.
|
|
|
|
-- @return True if file exists and is readable.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.file_readable(filename)
|
2008-11-22 17:23:03 +01:00
|
|
|
local file = io.open(filename)
|
|
|
|
if file then
|
2015-02-09 20:17:40 +01:00
|
|
|
local _, _, code = file:read(1)
|
2008-11-22 17:23:03 +01:00
|
|
|
io.close(file)
|
2015-02-09 20:17:40 +01:00
|
|
|
if code == 21 then
|
|
|
|
-- "Is a directory".
|
|
|
|
return false
|
|
|
|
end
|
2008-11-22 17:23:03 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2015-10-11 13:14:15 +02:00
|
|
|
--- Check if a path exists, is readable and is a directory.
|
|
|
|
-- @tparam string path The directory path.
|
|
|
|
-- @treturn boolean True if dir exists and is readable.
|
|
|
|
function util.dir_readable(path)
|
|
|
|
local gfile = Gio.File.new_for_path(path)
|
|
|
|
local gfileinfo = gfile:query_info("standard::type,access::can-read",
|
|
|
|
Gio.FileQueryInfoFlags.NONE)
|
|
|
|
return gfileinfo and gfileinfo:get_file_type() == "DIRECTORY" and
|
|
|
|
gfileinfo:get_attribute_boolean("access::can-read")
|
|
|
|
end
|
|
|
|
|
2015-10-18 13:04:56 +02:00
|
|
|
--- Check if a path is a directory.
|
|
|
|
-- @tparam string path
|
|
|
|
-- @treturn bool True if path exists and is a directory.
|
|
|
|
function util.is_dir(path)
|
2016-04-27 18:28:12 +02:00
|
|
|
return Gio.File.new_for_path(path):query_file_type({}) == "DIRECTORY"
|
2015-10-18 13:04:56 +02:00
|
|
|
end
|
|
|
|
|
2009-04-15 10:31:41 +02:00
|
|
|
local function subset_mask_apply(mask, set)
|
|
|
|
local ret = {}
|
|
|
|
for i = 1, #set do
|
|
|
|
if mask[i] then
|
|
|
|
rtable.insert(ret, set[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function subset_next(mask)
|
|
|
|
local i = 1
|
|
|
|
while i <= #mask and mask[i] do
|
|
|
|
mask[i] = false
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if i <= #mask then
|
|
|
|
mask[i] = 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Return all subsets of a specific set.
|
|
|
|
-- This function, giving a set, will return all subset it.
|
|
|
|
-- For example, if we consider a set with value { 10, 15, 34 },
|
|
|
|
-- it will return a table containing 2^n set:
|
|
|
|
-- { }, { 10 }, { 15 }, { 34 }, { 10, 15 }, { 10, 34 }, etc.
|
|
|
|
-- @param set A set.
|
|
|
|
-- @return A table with all subset.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.subsets(set)
|
2009-04-15 10:31:41 +02:00
|
|
|
local mask = {}
|
|
|
|
local ret = {}
|
|
|
|
for i = 1, #set do mask[i] = false end
|
|
|
|
|
|
|
|
-- Insert the empty one
|
|
|
|
rtable.insert(ret, {})
|
|
|
|
|
|
|
|
while subset_next(mask) do
|
|
|
|
rtable.insert(ret, subset_mask_apply(mask, set))
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Get the nearest rectangle in the given direction. Every rectangle is specified as a table
|
2012-09-14 21:49:14 +02:00
|
|
|
-- with 'x', 'y', 'width', 'height' keys, the same as client or screen geometries.
|
2016-05-16 06:57:15 +02:00
|
|
|
-- @deprecated awful.util.get_rectangle_in_direction
|
2012-09-14 21:49:14 +02:00
|
|
|
-- @param dir The direction, can be either "up", "down", "left" or "right".
|
|
|
|
-- @param recttbl A table of rectangle specifications.
|
|
|
|
-- @param cur The current rectangle.
|
|
|
|
-- @return The index for the rectangle in recttbl closer to cur in the given direction. nil if none found.
|
2016-05-16 06:57:15 +02:00
|
|
|
-- @see gears.geometry
|
2012-09-14 21:49:14 +02:00
|
|
|
function util.get_rectangle_in_direction(dir, recttbl, cur)
|
2016-05-16 06:57:15 +02:00
|
|
|
util.deprecate("gears.geometry.rectangle.get_in_direction")
|
|
|
|
|
|
|
|
return grect.get_in_direction(dir, recttbl, cur)
|
2012-09-14 21:49:14 +02:00
|
|
|
end
|
|
|
|
|
2009-04-15 21:33:21 +02:00
|
|
|
--- Join all tables given as parameters.
|
2009-04-18 22:44:36 +02:00
|
|
|
-- This will iterate all tables and insert all their keys into a new table.
|
2009-04-15 21:33:21 +02:00
|
|
|
-- @param args A list of tables to join
|
2009-04-18 22:44:36 +02:00
|
|
|
-- @return A new table containing all keys from the arguments.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.table.join(...)
|
2009-04-15 10:46:34 +02:00
|
|
|
local ret = {}
|
2016-02-07 15:02:25 +01:00
|
|
|
for _, t in pairs({...}) do
|
2010-10-11 16:55:56 +02:00
|
|
|
if t then
|
|
|
|
for k, v in pairs(t) do
|
2009-04-18 23:22:15 +02:00
|
|
|
if type(k) == "number" then
|
|
|
|
rtable.insert(ret, v)
|
|
|
|
else
|
|
|
|
ret[k] = v
|
|
|
|
end
|
2009-04-18 22:44:36 +02:00
|
|
|
end
|
|
|
|
end
|
2009-04-15 10:46:34 +02:00
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2016-01-16 04:05:24 +01:00
|
|
|
--- Override elements in the first table by the one in the second
|
|
|
|
-- @tparam table t the table to be overriden
|
|
|
|
-- @tparam table set the table used to override members of `t`
|
|
|
|
-- @treturn table t (for convenience)
|
|
|
|
function util.table.crush(t, set)
|
|
|
|
for k, v in pairs(set) do
|
|
|
|
t[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2016-01-16 04:46:35 +01:00
|
|
|
--- Pack all elements with an integer key into a new table
|
|
|
|
-- While both lua and luajit implement __len over sparse
|
|
|
|
-- table, the standard define it as an implementation
|
|
|
|
-- detail.
|
|
|
|
--
|
|
|
|
-- This function remove any non numeric keys from the value set
|
|
|
|
--
|
|
|
|
-- @tparam table t A potentially sparse table
|
|
|
|
-- @treturn table A packed table with all numeric keys
|
|
|
|
function util.table.from_sparse(t)
|
|
|
|
local keys= {}
|
2016-02-07 15:02:25 +01:00
|
|
|
for k in pairs(t) do
|
2016-01-16 04:46:35 +01:00
|
|
|
if type(k) == "number" then
|
|
|
|
keys[#keys+1] = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(keys)
|
|
|
|
|
|
|
|
local ret = {}
|
|
|
|
for _,v in ipairs(keys) do
|
|
|
|
ret[#ret+1] = t[v]
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2009-04-27 18:33:15 +02:00
|
|
|
--- Check if a table has an item and return its key.
|
|
|
|
-- @param t The table.
|
|
|
|
-- @param item The item to look for in values of the table.
|
|
|
|
-- @return The key were the item is found, or nil if not found.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.table.hasitem(t, item)
|
2009-04-27 18:33:15 +02:00
|
|
|
for k, v in pairs(t) do
|
|
|
|
if v == item then
|
|
|
|
return k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-27 17:49:20 +02:00
|
|
|
--- Split a string into multiple lines
|
|
|
|
-- @param text String to wrap.
|
|
|
|
-- @param width Maximum length of each line. Default: 72.
|
|
|
|
-- @param indent Number of spaces added before each wrapped line. Default: 0.
|
|
|
|
-- @return The string with lines wrapped to width.
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.linewrap(text, width, indent)
|
2015-09-20 16:42:24 +02:00
|
|
|
text = text or ""
|
|
|
|
width = width or 72
|
|
|
|
indent = indent or 0
|
2009-05-27 17:49:20 +02:00
|
|
|
|
|
|
|
local pos = 1
|
|
|
|
return text:gsub("(%s+)()(%S+)()",
|
2016-02-07 15:02:25 +01:00
|
|
|
function(_, st, word, fi)
|
2009-05-27 17:49:20 +02:00
|
|
|
if fi - pos > width then
|
|
|
|
pos = st
|
|
|
|
return "\n" .. string.rep(" ", indent) .. word
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2015-09-20 16:42:24 +02:00
|
|
|
--- Count number of lines in a string
|
|
|
|
-- @tparam string text Input string.
|
|
|
|
-- @treturn int Number of lines.
|
|
|
|
function util.linecount(text)
|
|
|
|
return select(2, text:gsub('\n', '\n')) + 1
|
|
|
|
end
|
|
|
|
|
2009-07-02 02:04:59 +02:00
|
|
|
--- 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
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.table.keys(t)
|
2009-07-02 02:04:59 +02:00
|
|
|
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
|
|
|
|
|
2009-07-02 02:05:29 +02:00
|
|
|
--- Filter a tables keys for certain content types
|
|
|
|
-- @param t The table to retrieve the keys for
|
|
|
|
-- @param ... the types to look for
|
|
|
|
-- @return A filtered table with keys
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.table.keys_filter(t, ...)
|
|
|
|
local keys = util.table.keys(t)
|
2009-07-02 02:05:29 +02:00
|
|
|
local keys_filtered = { }
|
|
|
|
for _, k in pairs(keys) do
|
2010-10-12 18:16:39 +02:00
|
|
|
for _, et in pairs({...}) do
|
2009-07-02 02:05:29 +02:00
|
|
|
if type(t[k]) == et then
|
|
|
|
rtable.insert(keys_filtered, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return keys_filtered
|
|
|
|
end
|
|
|
|
|
2009-07-02 02:05:42 +02:00
|
|
|
--- Reverse a table
|
|
|
|
-- @param t the table to reverse
|
|
|
|
-- @return the reversed table
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.table.reverse(t)
|
2009-07-02 02:05:42 +02:00
|
|
|
local tr = { }
|
|
|
|
-- reverse all elements with integer keys
|
|
|
|
for _, v in ipairs(t) do
|
|
|
|
rtable.insert(tr, 1, v)
|
|
|
|
end
|
|
|
|
-- add the remaining elements
|
|
|
|
for k, v in pairs(t) do
|
2009-11-17 07:53:07 +01:00
|
|
|
if type(k) ~= "number" then
|
2009-07-02 02:05:42 +02:00
|
|
|
tr[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tr
|
|
|
|
end
|
|
|
|
|
2009-06-25 01:15:07 +02:00
|
|
|
--- Clone a table
|
|
|
|
-- @param t the table to clone
|
2014-03-14 17:10:24 +01:00
|
|
|
-- @param deep Create a deep clone? (default: true)
|
2009-06-25 01:15:07 +02:00
|
|
|
-- @return a clone of t
|
2014-03-14 17:10:24 +01:00
|
|
|
function util.table.clone(t, deep)
|
2016-02-07 15:02:25 +01:00
|
|
|
deep = deep == nil and true or deep
|
2009-06-25 01:15:07 +02:00
|
|
|
local c = { }
|
|
|
|
for k, v in pairs(t) do
|
2014-03-14 17:10:24 +01:00
|
|
|
if deep and type(v) == "table" then
|
2012-06-12 20:13:09 +02:00
|
|
|
c[k] = util.table.clone(v)
|
2009-10-24 14:51:47 +02:00
|
|
|
else
|
|
|
|
c[k] = v
|
|
|
|
end
|
2009-06-25 01:15:07 +02:00
|
|
|
end
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
2012-02-16 22:00:41 +01:00
|
|
|
---
|
|
|
|
-- Returns an iterator to cycle through, starting from the first element or the
|
2014-03-15 02:56:58 +01:00
|
|
|
-- given index, all elements of a table that match a given criteria.
|
2012-04-02 13:36:12 +02:00
|
|
|
--
|
2012-02-16 22:00:41 +01:00
|
|
|
-- @param t the table to iterate
|
|
|
|
-- @param filter a function that returns true to indicate a positive match
|
|
|
|
-- @param start what index to start iterating from. Default is 1 (=> start of
|
|
|
|
-- the table)
|
2012-06-12 20:13:09 +02:00
|
|
|
function util.table.iterate(t, filter, start)
|
2012-02-16 22:00:41 +01:00
|
|
|
local count = 0
|
|
|
|
local index = start or 1
|
|
|
|
local length = #t
|
|
|
|
|
|
|
|
return function ()
|
|
|
|
while count < length do
|
|
|
|
local item = t[index]
|
2012-06-12 20:13:09 +02:00
|
|
|
index = util.cycle(#t, index + 1)
|
2012-02-16 22:00:41 +01:00
|
|
|
count = count + 1
|
|
|
|
if filter(item) then return item end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-12 20:13:09 +02:00
|
|
|
|
2015-09-20 16:42:24 +02:00
|
|
|
|
|
|
|
--- Merge items from the one table to another one
|
|
|
|
-- @tparam table t the container table
|
|
|
|
-- @tparam table set the mixin table
|
2016-01-19 23:14:29 +01:00
|
|
|
-- @treturn table Return `t` for convenience
|
2015-09-20 16:42:24 +02:00
|
|
|
function util.table.merge(t, set)
|
|
|
|
for _, v in ipairs(set) do
|
|
|
|
table.insert(t, v)
|
|
|
|
end
|
2016-01-19 23:14:29 +01:00
|
|
|
return t
|
2015-09-20 16:42:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-22 01:30:13 +01:00
|
|
|
-- Escape all special pattern-matching characters so that lua interprets them
|
|
|
|
-- literally instead of as a character class.
|
|
|
|
-- Source: http://stackoverflow.com/a/20778724/15690
|
|
|
|
function util.quote_pattern(s)
|
|
|
|
-- All special characters escaped in a string: %%, %^, %$, ...
|
|
|
|
local patternchars = '['..("%^$().[]*+-?"):gsub("(.)", "%%%1")..']'
|
|
|
|
return string.gsub(s, patternchars, "%%%1")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Generate a pattern matching expression that ignores case.
|
|
|
|
-- @param s Original pattern matching expression.
|
|
|
|
function util.query_to_pattern(q)
|
2016-02-07 15:02:25 +01:00
|
|
|
local s = util.quote_pattern(q)
|
2015-01-22 01:30:13 +01:00
|
|
|
-- Poor man's case-insensitive character matching.
|
|
|
|
s = string.gsub(s, "%a",
|
|
|
|
function (c)
|
|
|
|
return string.format("[%s%s]", string.lower(c),
|
|
|
|
string.upper(c))
|
|
|
|
end)
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
2015-08-12 14:09:45 +02:00
|
|
|
--- Round a number to an integer.
|
|
|
|
-- @tparam number x
|
|
|
|
-- @treturn integer
|
|
|
|
function util.round(x)
|
|
|
|
return floor(x + 0.5)
|
|
|
|
end
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
return util
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|