From 92dabc890e15397d823983b688f5beb7aae8d4c2 Mon Sep 17 00:00:00 2001 From: Nooo37 <70270606+Nooo37@users.noreply.github.com> Date: Sat, 6 Nov 2021 19:33:58 +0100 Subject: [PATCH] Refactor layout icon path search and also search in package.path (#137, closes #136) --- layout/centered.lua | 22 ++-------------- layout/deck.lua | 23 +---------------- layout/equalarea.lua | 21 +--------------- layout/horizontal.lua | 22 +--------------- layout/init.lua | 58 +++++++++++++++++++++++++++---------------- layout/mstab.lua | 15 +---------- layout/vertical.lua | 22 +--------------- 7 files changed, 43 insertions(+), 140 deletions(-) diff --git a/layout/centered.lua b/layout/centered.lua index c1f79c2..4e1ad24 100644 --- a/layout/centered.lua +++ b/layout/centered.lua @@ -1,7 +1,4 @@ local awful = require("awful") -local gears = require("gears") -local gcolor = require("gears.color") -local beautiful = require("beautiful") local math = math local mylayout = {} @@ -58,6 +55,7 @@ function mylayout.arrange(p) -- iterate through slaves for idx = 1, nslaves do -- idx=nmaster+1,#p.clients do local c = p.clients[idx + nmaster] + local g if idx % 2 == 0 then g = { x = area.x, @@ -83,20 +81,4 @@ function mylayout.arrange(p) end end -local icon_raw = gears.filesystem.get_configuration_dir() - .. tostring(...):match("^.*bling"):gsub("%.", "/") - .. "/icons/layouts/centered.png" - -local function get_icon() - if icon_raw ~= nil then - return gcolor.recolor_image(icon_raw, beautiful.fg_normal) - else - return nil - end -end - -return { - layout = mylayout, - icon_raw = icon_raw, - get_icon = get_icon, -} +return mylayout diff --git a/layout/deck.lua b/layout/deck.lua index a1e7546..e0500b9 100644 --- a/layout/deck.lua +++ b/layout/deck.lua @@ -1,8 +1,3 @@ -local awful = require("awful") -local gears = require("gears") -local gcolor = require("gears.color") -local beautiful = require("beautiful") - local mylayout = {} mylayout.name = "deck" @@ -39,20 +34,4 @@ function mylayout.arrange(p) end end -local icon_raw = gears.filesystem.get_configuration_dir() - .. tostring(...):match("^.*bling"):gsub("%.", "/") - .. "/icons/layouts/deck.png" - -local function get_icon() - if icon_raw ~= nil then - return gcolor.recolor_image(icon_raw, beautiful.fg_normal) - else - return nil - end -end - -return { - layout = mylayout, - icon_raw = icon_raw, - get_icon = get_icon, -} +return mylayout diff --git a/layout/equalarea.lua b/layout/equalarea.lua index eab2acc..37e972d 100644 --- a/layout/equalarea.lua +++ b/layout/equalarea.lua @@ -1,6 +1,3 @@ -local gears = require("gears") -local gcolor = require("gears.color") -local beautiful = require("beautiful") local math = math local screen = screen local mylayout = {} @@ -77,20 +74,4 @@ function mylayout.arrange(p) divide(p, g, 1, #cls, cls, mwfact, mcount) end -local icon_raw = gears.filesystem.get_configuration_dir() - .. tostring(...):match("^.*bling"):gsub("%.", "/") - .. "/icons/layouts/equalarea.png" - -local function get_icon() - if icon_raw ~= nil then - return gcolor.recolor_image(icon_raw, beautiful.fg_normal) - else - return nil - end -end - -return { - layout = mylayout, - icon_raw = icon_raw, - get_icon = get_icon, -} +return mylayout diff --git a/layout/horizontal.lua b/layout/horizontal.lua index 353b4a8..23f9c9e 100644 --- a/layout/horizontal.lua +++ b/layout/horizontal.lua @@ -1,7 +1,3 @@ -local awful = require("awful") -local gears = require("gears") -local gcolor = require("gears.color") -local beautiful = require("beautiful") local math = math local mylayout = {} @@ -57,20 +53,4 @@ function mylayout.arrange(p) end end -local icon_raw = gears.filesystem.get_configuration_dir() - .. tostring(...):match("^.*bling"):gsub("%.", "/") - .. "/icons/layouts/horizontal.png" - -local function get_icon() - if icon_raw ~= nil then - return gcolor.recolor_image(icon_raw, beautiful.fg_normal) - else - return nil - end -end - -return { - layout = mylayout, - icon_raw = icon_raw, - get_icon = get_icon, -} +return mylayout diff --git a/layout/init.lua b/layout/init.lua index f7ed971..223d9d4 100644 --- a/layout/init.lua +++ b/layout/init.lua @@ -1,30 +1,44 @@ local beautiful = require("beautiful") +local gears = require("gears") -local mstab = require(... .. ".mstab") -beautiful.layout_mstab = mstab.get_icon() +local M = {} +local relative_lua_path = tostring(...) -local vertical = require(... .. ".vertical") -beautiful.layout_vertical = vertical.get_icon() +local function get_layout_icon_path(name) + local relative_icon_path = relative_lua_path + :match("^.*bling"):gsub("%.", "/") + .. "/icons/layouts/" .. name .. ".png" -local horizontal = require(... .. ".horizontal") -beautiful.layout_horizontal = horizontal.get_icon() + for p in package.path:gmatch('([^;]+)') do + p = p:gsub("?.*", "") + local absolute_icon_path = p .. relative_icon_path + if gears.filesystem.file_readable(absolute_icon_path) then + return absolute_icon_path + end + end +end -local centered = require(... .. ".centered") -beautiful.layout_centered = centered.get_icon() +local function get_icon(icon_raw) + if icon_raw ~= nil then + return gears.color.recolor_image(icon_raw, beautiful.fg_normal) + else + return nil + end +end -local equalarea = require(... .. ".equalarea") -beautiful.layout_equalarea = equalarea.get_icon() - -local deck = require(... .. ".deck") -beautiful.layout_deck = deck.get_icon() - -local layout = { - mstab = mstab.layout, - centered = centered.layout, - vertical = vertical.layout, - horizontal = horizontal.layout, - equalarea = equalarea.layout, - deck = deck.layout, +local layouts = { + "mstab", + "vertical", + "horizontal", + "centered", + "equalarea", + "deck" } -return layout +for _, layout_name in ipairs(layouts) do + local icon_raw = get_layout_icon_path(layout_name) + beautiful["layout_" .. layout_name] = get_icon(icon_raw) + M[layout_name] = require(... .. "." .. layout_name) +end + +return M diff --git a/layout/mstab.lua b/layout/mstab.lua index 6cbdb84..639275f 100644 --- a/layout/mstab.lua +++ b/layout/mstab.lua @@ -1,7 +1,6 @@ local awful = require("awful") local gears = require("gears") local wibox = require("wibox") -local gcolor = require("gears.color") local beautiful = require("beautiful") local mylayout = {} @@ -242,16 +241,4 @@ function mylayout.arrange(p) ) end -local icon_raw = gears.filesystem.get_configuration_dir() - .. tostring(...):match("^.*bling"):gsub("%.", "/") - .. "/icons/layouts/mstab.png" - -local function get_icon() - if icon_raw ~= nil then - return gcolor.recolor_image(icon_raw, beautiful.fg_normal) - else - return nil - end -end - -return { layout = mylayout, icon_raw = icon_raw, get_icon = get_icon } +return mylayout diff --git a/layout/vertical.lua b/layout/vertical.lua index f0eb8aa..8b6811e 100644 --- a/layout/vertical.lua +++ b/layout/vertical.lua @@ -1,7 +1,3 @@ -local awful = require("awful") -local gears = require("gears") -local gcolor = require("gears.color") -local beautiful = require("beautiful") local math = math local mylayout = {} @@ -57,20 +53,4 @@ function mylayout.arrange(p) end end -local icon_raw = gears.filesystem.get_configuration_dir() - .. tostring(...):match("^.*bling"):gsub("%.", "/") - .. "/icons/layouts/vertical.png" - -local function get_icon() - if icon_raw ~= nil then - return gcolor.recolor_image(icon_raw, beautiful.fg_normal) - else - return nil - end -end - -return { - layout = mylayout, - icon_raw = icon_raw, - get_icon = get_icon, -} +return mylayout