Look for both user themes and default themes

Plus speed things up
This commit is contained in:
David Heinemeier Hansson
2026-01-01 16:06:10 -07:00
parent 934285b7c4
commit 1b8da49425

View File

@@ -4,45 +4,64 @@
Name = "omarchythemes" Name = "omarchythemes"
NamePretty = "Omarchy Themes" NamePretty = "Omarchy Themes"
-- Check if file exists using Lua (no subprocess)
local function file_exists(path)
local f = io.open(path, "r")
if f then
f:close()
return true
end
return false
end
-- Get first matching file from directory using ls (single call for fallback)
local function first_image_in_dir(dir)
local handle = io.popen("ls -1 '" .. dir .. "' 2>/dev/null | head -n 1")
if handle then
local file = handle:read("*l")
handle:close()
if file and file ~= "" then
return dir .. "/" .. file
end
end
return nil
end
-- The main function elephant will call -- The main function elephant will call
function GetEntries() function GetEntries()
local entries = {} local entries = {}
local theme_dir = os.getenv("HOME") .. "/.config/omarchy/themes" local user_theme_dir = os.getenv("HOME") .. "/.config/omarchy/themes"
local omarchy_path = os.getenv("OMARCHY_PATH") or ""
local default_theme_dir = omarchy_path .. "/themes"
-- First, get all theme directories local seen_themes = {}
local find_dirs_cmd = "find -L '" .. theme_dir .. "' -mindepth 1 -maxdepth 1 -type d 2>/dev/null"
local handle = io.popen(find_dirs_cmd) -- Helper function to process themes from a directory
local function process_themes_from_dir(theme_dir)
-- Single find call to get all theme directories
local handle = io.popen("find -L '" .. theme_dir .. "' -mindepth 1 -maxdepth 1 -type d 2>/dev/null")
if not handle then if not handle then
return entries return
end end
for theme_path in handle:lines() do for theme_path in handle:lines() do
local theme_name = theme_path:match(".*/(.+)$") local theme_name = theme_path:match(".*/(.+)$")
if theme_name then if theme_name and not seen_themes[theme_name] then
-- find preview image seen_themes[theme_name] = true
local find_preview_cmd = "find -L '"
.. theme_path -- Check for preview images directly (no subprocess)
.. "' -maxdepth 1 -type f \\( -name 'preview.png' -o -name 'preview.jpg' \\) 2>/dev/null | head -n 1"
local preview_handle = io.popen(find_preview_cmd)
local preview_path = nil local preview_path = nil
local preview_png = theme_path .. "/preview.png"
local preview_jpg = theme_path .. "/preview.jpg"
if preview_handle then if file_exists(preview_png) then
preview_path = preview_handle:read("*l") preview_path = preview_png
preview_handle:close() elseif file_exists(preview_jpg) then
end preview_path = preview_jpg
else
-- If no preview found, use first image from backgrounds folder -- Fallback: get first image from backgrounds (one ls call)
if not preview_path or preview_path == "" then preview_path = first_image_in_dir(theme_path .. "/backgrounds")
local bg_cmd = "find -L '"
.. theme_path
.. "/backgrounds' -maxdepth 1 -type f \\( -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' \\) 2>/dev/null | head -n 1"
local bg_handle = io.popen(bg_cmd)
if bg_handle then
preview_path = bg_handle:read("*l")
bg_handle:close()
end
end end
if preview_path and preview_path ~= "" then if preview_path and preview_path ~= "" then
@@ -65,6 +84,13 @@ function GetEntries()
end end
handle:close() handle:close()
end
-- Process user themes first (they take precedence)
process_themes_from_dir(user_theme_dir)
-- Then process default themes (only if not already seen)
process_themes_from_dir(default_theme_dir)
return entries return entries
end end