Ported lib/gears to lua 5.2

Tested with lua 5.1: all good

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
This commit is contained in:
Arvydas Sidorenko 2012-06-12 10:13:46 +02:00 committed by Uli Schlachter
parent 26797ab494
commit e789cfaf66
6 changed files with 74 additions and 49 deletions

View File

@ -15,7 +15,7 @@ local type = type
local cairo = require("lgi").cairo local cairo = require("lgi").cairo
local surface = require("gears.surface") local surface = require("gears.surface")
module("gears.color") local color = { mt = {} }
--- Parse a HTML-color. --- Parse a HTML-color.
-- This function can parse colors like #rrggbb and #rrggbbaa. -- This function can parse colors like #rrggbb and #rrggbbaa.
@ -23,7 +23,7 @@ module("gears.color")
-- Thanks to #lua for this. :) -- Thanks to #lua for this. :)
-- @param col The color to parse -- @param col The color to parse
-- @return 4 values which each are in the range [0, 1]. -- @return 4 values which each are in the range [0, 1].
function parse_color(col) function color.parse_color(col)
local rgb = {} local rgb = {}
for pair in string.gmatch(col, "[^#].") do for pair in string.gmatch(col, "[^#].") do
local i = tonumber(pair, 16) local i = tonumber(pair, 16)
@ -51,20 +51,20 @@ end
--- Create a solid pattern --- Create a solid pattern
-- @param col The color for the pattern -- @param col The color for the pattern
-- @return A cairo pattern object -- @return A cairo pattern object
function create_solid_pattern(col) function color.create_solid_pattern(col)
local col = col local col = col
if col == nil then if col == nil then
col = "#000000" col = "#000000"
elseif type(col) == "table" then elseif type(col) == "table" then
col = col.color col = col.color
end end
return cairo.Pattern.create_rgba(parse_color(col)) return cairo.Pattern.create_rgba(color.parse_color(col))
end end
--- Create an image pattern from a png file --- Create an image pattern from a png file
-- @param file The filename of the file -- @param file The filename of the file
-- @return a cairo pattern object -- @return a cairo pattern object
function create_png_pattern(file) function color.create_png_pattern(file)
local file = file local file = file
if type(file) == "table" then if type(file) == "table" then
file = file.file file = file.file
@ -80,15 +80,15 @@ end
local function add_iterator_stops(p, iterator) local function add_iterator_stops(p, iterator)
for k in iterator do for k in iterator do
local sub = string.gmatch(k, "[^,]+") local sub = string.gmatch(k, "[^,]+")
local point, color = sub(), sub() local point, clr = sub(), sub()
p:add_color_stop_rgba(point, parse_color(color)) p:add_color_stop_rgba(point, color.parse_color(clr))
end end
end end
-- Add a list of stops to a given pattern -- Add a list of stops to a given pattern
local function add_stops_table(pat, arg) local function add_stops_table(pat, arg)
for _, stop in ipairs(arg) do for _, stop in ipairs(arg) do
pat:add_color_stop_rgba(stop[1], parse_color(stop[2])) pat:add_color_stop_rgba(stop[1], color.parse_color(stop[2]))
end end
end end
@ -119,7 +119,7 @@ end
-- For the explanation of "&#60;stops&#62;", see create_pattern(). -- For the explanation of "&#60;stops&#62;", see create_pattern().
-- @param arg The argument describing the pattern -- @param arg The argument describing the pattern
-- @return a cairo pattern object -- @return a cairo pattern object
function create_linear_pattern(arg) function color.create_linear_pattern(arg)
local pat local pat
if type(arg) == "string" then if type(arg) == "string" then
@ -144,7 +144,7 @@ end
-- For the explanation of "&#60;stops&#62;", see create_pattern(). -- For the explanation of "&#60;stops&#62;", see create_pattern().
-- @param arg The argument describing the pattern -- @param arg The argument describing the pattern
-- @return a cairo pattern object -- @return a cairo pattern object
function create_radial_pattern(arg) function color.create_radial_pattern(arg)
local pat local pat
if type(arg) == "string" then if type(arg) == "string" then
@ -160,11 +160,11 @@ function create_radial_pattern(arg)
end end
--- Mapping of all supported color types. New entries can be added. --- Mapping of all supported color types. New entries can be added.
types = { local types = {
solid = create_solid_pattern, solid = color.create_solid_pattern,
png = create_png_pattern, png = color.create_png_pattern,
linear = create_linear_pattern, linear = color.create_linear_pattern,
radial = create_radial_pattern radial = color.create_radial_pattern
} }
--- Create a pattern from a given string. --- Create a pattern from a given string.
@ -181,7 +181,7 @@ types = {
-- create_radial_pattern -- create_radial_pattern
-- @param col The string describing the pattern. -- @param col The string describing the pattern.
-- @return a cairo pattern object -- @return a cairo pattern object
function create_pattern(col) function color.create_pattern(col)
if type(col) == "string" then if type(col) == "string" then
local t = string.match(col, "[^:]+") local t = string.match(col, "[^:]+")
if types[t] then if types[t] then
@ -195,9 +195,13 @@ function create_pattern(col)
return types[t](col) return types[t](col)
end end
end end
return create_solid_pattern(col) return color.create_solid_pattern(col)
end end
setmetatable(_M, { __call = function (_, ...) return create_pattern(...) end }) function color.mt:__call(...)
return color.create_pattern(...)
end
return setmetatable(color, color.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -11,12 +11,13 @@ local print = print
local type = type local type = type
local pairs = pairs local pairs = pairs
module("gears.debug") -- gears.debug
local debug = {}
--- Check that the given condition holds true, else throw an error --- Check that the given condition holds true, else throw an error
-- @param cond If this is false, throw a lua error with a backtrace. -- @param cond If this is false, throw a lua error with a backtrace.
-- @param message Message to print in the error (optional). -- @param message Message to print in the error (optional).
function assert(cond, message) function debug.assert(cond, message)
local message = message or cond local message = message or cond
if not cond then if not cond then
error(traceback("Assertion failed: '" .. tostring(message) .. "'")) error(traceback("Assertion failed: '" .. tostring(message) .. "'"))
@ -54,15 +55,17 @@ end
-- @param data Value to inspect. -- @param data Value to inspect.
-- @param tag The name of the value. -- @param tag The name of the value.
-- @return a string that contains the expanded value of data. -- @return a string that contains the expanded value of data.
function dump_return(data, tag) function debug.dump_return(data, tag)
return dump_raw(data, nil, tag) return dump_raw(data, nil, tag)
end end
--- Print the table (or any other value) to the console. --- Print the table (or any other value) to the console.
-- @param data Table to print. -- @param data Table to print.
-- @param tag The name of the table. -- @param tag The name of the table.
function dump(data, tag) function debug.dump(data, tag)
print(dump_return(data, tag)) print(dump_return(data, tag))
end end
return debug
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -4,12 +4,15 @@
-- @release @AWESOME_VERSION@ -- @release @AWESOME_VERSION@
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
require("gears.color") -- gears
require("gears.debug")
require("gears.object")
require("gears.sort")
require("gears.surface")
module("gears") return
{
color = require("gears.color");
debug = require("gears.debug");
object = require("gears.object");
sort = require("gears.sort");
surface = require("gears.surface");
}
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -9,7 +9,8 @@ local pairs = pairs
local type = type local type = type
local error = error local error = error
module("gears.object") -- gears.object
local object = { mt = {} }
-- Verify that obj is indeed a valid object as returned by new() -- Verify that obj is indeed a valid object as returned by new()
local function check(obj) local function check(obj)
@ -34,7 +35,7 @@ end
--- Add a signal to an object. All signals must be added before they can be used. --- Add a signal to an object. All signals must be added before they can be used.
-- @param obj The object -- @param obj The object
-- @param name The name of the new signal. -- @param name The name of the new signal.
function add_signal(obj, name) function object.add_signal(obj, name)
check(obj) check(obj)
if not obj._signals[name] then if not obj._signals[name] then
obj._signals[name] = {} obj._signals[name] = {}
@ -45,7 +46,7 @@ end
-- @param obj The object -- @param obj The object
-- @param name The name of the signal -- @param name The name of the signal
-- @param func The callback to call when the signal is emitted -- @param func The callback to call when the signal is emitted
function connect_signal(obj, name, func) function object.connect_signal(obj, name, func)
local sig = find_signal(obj, name, "connect to") local sig = find_signal(obj, name, "connect to")
sig[func] = func sig[func] = func
end end
@ -54,7 +55,7 @@ end
-- @param obj The object -- @param obj The object
-- @param name The name of the signal -- @param name The name of the signal
-- @param func The callback that should be disconnected -- @param func The callback that should be disconnected
function disconnect_signal(obj, name, func) function object.disconnect_signal(obj, name, func)
local sig = find_signal(obj, name, "disconnect from") local sig = find_signal(obj, name, "disconnect from")
sig[func] = nil sig[func] = nil
end end
@ -65,7 +66,7 @@ end
-- @param ... Extra arguments for the callback functions. Each connected -- @param ... Extra arguments for the callback functions. Each connected
-- function receives the object as first argument and then any extra -- function receives the object as first argument and then any extra
-- arguments that are given to emit_signal() -- arguments that are given to emit_signal()
function emit_signal(obj, name, ...) function object.emit_signal(obj, name, ...)
local sig = find_signal(obj, name, "emit") local sig = find_signal(obj, name, "emit")
for func in pairs(sig) do for func in pairs(sig) do
func(obj, ...) func(obj, ...)
@ -78,7 +79,7 @@ local function new()
local ret = {} local ret = {}
-- Copy all our global functions to our new object -- Copy all our global functions to our new object
for k, v in pairs(_M) do for k, v in pairs(object) do
if type(v) == "function" then if type(v) == "function" then
ret[k] = v ret[k] = v
end end
@ -89,6 +90,10 @@ local function new()
return ret return ret
end end
setmetatable(_M, { __call = function (_, ...) return new(...) end }) function object.mt:__call(...)
return new(...)
end
return setmetatable(object, object.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -9,7 +9,8 @@ local ipairs = ipairs
local table = table local table = table
local error = error local error = error
module("gears.sort") -- gears.sort
local sort = { mt = {} }
local function less_than_comp(a, b) local function less_than_comp(a, b)
return a < b return a < b
@ -19,7 +20,7 @@ end
-- The difference to table.sort() is that this sort is stable. -- The difference to table.sort() is that this sort is stable.
-- @param list The table to sort (we do an in-place sort!). -- @param list The table to sort (we do an in-place sort!).
-- @param comp Comparator used for the sorting -- @param comp Comparator used for the sorting
function sort(list, comp) function sort.sort(list, comp)
local comp = comp or less_than_comp local comp = comp or less_than_comp
-- A table could contain non-integer keys which we have to ignore. -- A table could contain non-integer keys which we have to ignore.
@ -53,10 +54,10 @@ function sort(list, comp)
end end
end end
function test() function sort.test()
local function test_one(t) local function test_one(t)
local len = #t local len = #t
sort(t) sort.sort(t)
if len ~= #t then if len ~= #t then
error("Table lost entries during sort!") error("Table lost entries during sort!")
end end
@ -78,6 +79,10 @@ function test()
return true return true
end end
setmetatable(_M, { __call = function (_, ...) return sort(...) end }) function sort.mt:__call(...)
return sort.sort(...)
end
return setmetatable(sort, sort.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -13,27 +13,32 @@ if tonumber(require("lgi.version")) <= 0.5 then
error("lgi too old, need at least version 0.7 (not yet released?) or recent git") error("lgi too old, need at least version 0.7 (not yet released?) or recent git")
end end
module("gears.surface") -- gears.surface
local surface = { mt = {} }
--- Try to convert the argument into an lgi cairo surface. --- Try to convert the argument into an lgi cairo surface.
-- This is usually needed for loading images by file name. -- This is usually needed for loading images by file name.
function load(surface) function surface.load(_surface)
-- Nil is not changed -- Nil is not changed
if not surface then if not _surface then
return nil return nil
end end
-- lgi cairo surfaces don't get changed either -- lgi cairo surfaces don't get changed either
if cairo.Surface:is_type_of(surface) then if cairo.Surface:is_type_of(_surface) then
return surface return _surface
end end
-- Strings are assumed to be file names and get loaded -- Strings are assumed to be file names and get loaded
if type(surface) == "string" then if type(_surface) == "string" then
surface = capi.awesome.load_image(surface) _surface = capi.awesome.load_image(_surface)
end end
-- Everything else gets forced into a surface -- Everything else gets forced into a surface
return cairo.Surface(surface, true) return cairo.Surface(_surface, true)
end end
setmetatable(_M, { __call = function(_, ...) return load(...) end }) function surface.mt:__call(...)
return surface.load(...)
end
return setmetatable(surface, surface.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80