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

View File

@ -11,12 +11,13 @@ local print = print
local type = type
local pairs = pairs
module("gears.debug")
-- gears.debug
local debug = {}
--- 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 message Message to print in the error (optional).
function assert(cond, message)
function debug.assert(cond, message)
local message = message or cond
if not cond then
error(traceback("Assertion failed: '" .. tostring(message) .. "'"))
@ -54,15 +55,17 @@ end
-- @param data Value to inspect.
-- @param tag The name of the value.
-- @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)
end
--- Print the table (or any other value) to the console.
-- @param data Table to print.
-- @param tag The name of the table.
function dump(data, tag)
function debug.dump(data, tag)
print(dump_return(data, tag))
end
return debug
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -4,12 +4,15 @@
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
require("gears.color")
require("gears.debug")
require("gears.object")
require("gears.sort")
require("gears.surface")
-- gears
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

View File

@ -9,7 +9,8 @@ local pairs = pairs
local type = type
local error = error
module("gears.object")
-- gears.object
local object = { mt = {} }
-- Verify that obj is indeed a valid object as returned by new()
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.
-- @param obj The object
-- @param name The name of the new signal.
function add_signal(obj, name)
function object.add_signal(obj, name)
check(obj)
if not obj._signals[name] then
obj._signals[name] = {}
@ -45,7 +46,7 @@ end
-- @param obj The object
-- @param name The name of the signal
-- @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")
sig[func] = func
end
@ -54,7 +55,7 @@ end
-- @param obj The object
-- @param name The name of the signal
-- @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")
sig[func] = nil
end
@ -65,7 +66,7 @@ end
-- @param ... Extra arguments for the callback functions. Each connected
-- function receives the object as first argument and then any extra
-- 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")
for func in pairs(sig) do
func(obj, ...)
@ -78,7 +79,7 @@ local function new()
local ret = {}
-- 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
ret[k] = v
end
@ -89,6 +90,10 @@ local function new()
return ret
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

View File

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

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")
end
module("gears.surface")
-- gears.surface
local surface = { mt = {} }
--- Try to convert the argument into an lgi cairo surface.
-- This is usually needed for loading images by file name.
function load(surface)
function surface.load(_surface)
-- Nil is not changed
if not surface then
if not _surface then
return nil
end
-- lgi cairo surfaces don't get changed either
if cairo.Surface:is_type_of(surface) then
return surface
if cairo.Surface:is_type_of(_surface) then
return _surface
end
-- Strings are assumed to be file names and get loaded
if type(surface) == "string" then
surface = capi.awesome.load_image(surface)
if type(_surface) == "string" then
_surface = capi.awesome.load_image(_surface)
end
-- Everything else gets forced into a surface
return cairo.Surface(surface, true)
return cairo.Surface(_surface, true)
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