This commit is contained in:
Sorky 2019-01-21 21:37:45 +11:00
parent 3fbb96ecac
commit 9927b7a88d
7 changed files with 88 additions and 23 deletions

View File

@ -128,19 +128,7 @@ local all_icon_sizes = {
} }
--- List of supported icon formats. --- List of supported icon formats.
local icon_formats = { "png", "xpm", "svg" } local supported_icon_formats = { png = 1, xpm = 2, svg = 3 }
--- Check whether the icon format is supported.
-- @param icon_file Filename of the icon.
-- @return true if format is supported, false otherwise.
local function is_format_supported(icon_file)
for _, f in ipairs(icon_formats) do
if icon_file:match('%.' .. f) then
return true
end
end
return false
end
local icon_lookup_path = nil local icon_lookup_path = nil
--- Get a list of icon lookup paths. --- Get a list of icon lookup paths.
@ -226,21 +214,23 @@ function utils.lookup_icon_uncached(icon_file)
return false return false
end end
if icon_file:sub(1, 1) == '/' and is_format_supported(icon_file) then local icon_file_ext = icon_file:match(".*%.(.-)$")
if icon_file:sub(1, 1) == '/' and supported_icon_formats[icon_file_ext] then
-- If the path to the icon is absolute and its format is -- If the path to the icon is absolute and its format is
-- supported, do not perform a lookup. -- supported, do not perform a lookup.
return gfs.file_readable(icon_file) and icon_file or nil return gfs.file_readable(icon_file) and icon_file or nil
else else
for _, directory in ipairs(get_icon_lookup_path()) do for _, directory in ipairs(get_icon_lookup_path()) do
if is_format_supported(icon_file) and local directory_file = directory .. "/" .. icon_file
gfs.file_readable(directory .. "/" .. icon_file) then if supported_icon_formats[icon_file_ext] and
return directory .. "/" .. icon_file gfs.file_readable(directory_file) then
return directory_file
else else
-- Icon is probably specified without path and format, -- Icon is probably specified without path and format,
-- like 'firefox'. Try to add supported extensions to -- like 'firefox'. Try to add supported extensions to
-- it and see if such file exists. -- it and see if such file exists.
for _, format in ipairs(icon_formats) do for format, _ in pairs(supported_icon_formats) do
local possible_file = directory .. "/" .. icon_file .. "." .. format local possible_file = directory_file .. "." .. format
if gfs.file_readable(possible_file) then if gfs.file_readable(possible_file) then
return possible_file return possible_file
end end

View File

@ -0,0 +1 @@
../../../../icons/awesome16.png

View File

@ -0,0 +1 @@
../../../../icons/awesome16.png

View File

@ -0,0 +1 @@
../../../../icons/awesome16.png

View File

@ -0,0 +1 @@
../../../../icons/awesome16.png

View File

@ -0,0 +1 @@
../../../../icons/awesome16.png

View File

@ -6,9 +6,12 @@
local utils = require("menubar.utils") local utils = require("menubar.utils")
local theme = require("beautiful") local theme = require("beautiful")
local glib = require("lgi").GLib local glib = require("lgi").GLib
local gfs = require("gears.filesystem")
describe("menubar.utils lookup_icon_uncached", function() describe("menubar.utils lookup_icon_uncached", function()
local shimmed = {} local shimmed = {}
local gfs_shim_dir_readable
local gfs_shim_file_readable
local icon_theme local icon_theme
local function assert_found_in_path(icon, path) local function assert_found_in_path(icon, path)
@ -23,13 +26,18 @@ describe("menubar.utils lookup_icon_uncached", function()
glib[name] = function() return retval end glib[name] = function() return retval end
end end
shim('get_home_dir', root .. "/home") shim('get_home_dir', "/home")
shim('get_user_data_dir', root .. "/home/.local/share") shim('get_user_data_dir', "/home/.local/share")
shim('get_system_data_dirs', { shim('get_system_data_dirs', {
root .. "/usr/local/share", "/usr/local/share",
root .. "/usr/share" "/usr/share"
}) })
gfs_shim_dir_readable = gfs.dir_readable
gfs.dir_readable = function(path) return gfs_shim_dir_readable(root..path) end
gfs_shim_file_readable = gfs.file_readable
gfs.file_readable = function(filename) return gfs_shim_file_readable(root..filename) end
icon_theme = theme.icon_theme icon_theme = theme.icon_theme
theme.icon_theme = 'awesome' theme.icon_theme = 'awesome'
end) end)
@ -38,6 +46,8 @@ describe("menubar.utils lookup_icon_uncached", function()
for name, func in pairs(shimmed) do for name, func in pairs(shimmed) do
glib[name] = func glib[name] = func
end end
gfs.dir_readable = gfs_shim_dir_readable
gfs.file_readable = gfs_shim_file_readable
theme.icon_theme = icon_theme theme.icon_theme = icon_theme
end) end)
@ -65,6 +75,66 @@ describe("menubar.utils lookup_icon_uncached", function()
assert_found_in_path('awesome', '/.icons/awesome/64x64/apps/awesome.png') assert_found_in_path('awesome', '/.icons/awesome/64x64/apps/awesome.png')
assert_found_in_path('awesome2', '/.icons/awesome/scalable/apps/awesome2.png') assert_found_in_path('awesome2', '/.icons/awesome/scalable/apps/awesome2.png')
end) end)
-- Check for no argument :: if not icon_file or icon_file == "" then
it('no icon specified', function()
assert.is_false(utils.lookup_icon_uncached())
assert.is_false(utils.lookup_icon_uncached(nil)) -- Same as above?
assert.is_false(utils.lookup_icon_uncached(false))
assert.is_false(utils.lookup_icon_uncached(''))
end)
-- Check for icon not in search path :: if icon_file:sub(1, 1) == '/' and supported_icon_formats[icon_file_ext] then
it('finds icons even those not in the search paths when full path specified', function()
-- Shimmed icon base directories contain the following icons:
--
-- usr/share/icon5.png
-- usr/share/icon6.xpm
-- usr/share/icon7.svg
assert_found_in_path('/usr/share/icon5.png', '/usr/share/icon5.png')
assert_found_in_path('/usr/share/icon6.xpm', '/usr/share/icon6.xpm')
assert_found_in_path('/usr/share/icon7.svg', '/usr/share/icon7.svg')
assert.is_same(nil, utils.lookup_icon_uncached('/.png')) -- supported file does not exist in location
assert.is_same(nil, utils.lookup_icon_uncached('/blah/icon6.png')) -- supported file does not exist in location
end)
-- Check icon with specified extension matching :: if supported_icon_formats[icon_file_ext] and
it('finds icons with specified supported extension in search path', function()
-- Shimmed icon base directories contain the following icons:
--
-- .icons/icon5.png
-- .icons/icon6.xpm
-- .icons/icon7.svg
assert_found_in_path('icon5.png', '/.icons/icon5.png')
assert_found_in_path('icon6.xpm', '/.icons/icon6.xpm')
assert_found_in_path('icon7.svg', '/.icons/icon7.svg')
-- Will fail as file does not exist (but extension is supported)
assert.is_false(utils.lookup_icon_uncached('icon8.png'))
end)
-- Check icon with no (or invalid) extension matching :: if NOT supported_icon_formats[icon_file_ext]
it('finds icons without specified path or extension', function()
-- Shimmed icon base directories contain the following icons:
--
-- .icons/icon6.xpm
-- .icons/icon7.svg
assert_found_in_path('icon6', '/.icons/icon6.xpm') -- Similar to original tests and testing xpm extension
assert_found_in_path('icon7', '/.icons/icon7.svg') -- Similar to original tests and testing svg extension
assert.is_false(utils.lookup_icon_uncached('/png')) -- supported file does not exist in given location
assert.is_false(utils.lookup_icon_uncached('.png')) -- file does not exist
assert.is_false(utils.lookup_icon_uncached('png')) -- file does not exist
assert.is_false(utils.lookup_icon_uncached('icon9')) -- file does not exist
end)
--]]
end) end)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80