From 42d3f25034778c121dc458b3d5246896fedc5db2 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Wed, 23 Oct 2024 18:46:02 +0300 Subject: [PATCH] PatchManagement: prettify menu (#12668) --- plugins/patchmanagement.koplugin/main.lua | 230 +++++++++++----------- 1 file changed, 116 insertions(+), 114 deletions(-) diff --git a/plugins/patchmanagement.koplugin/main.lua b/plugins/patchmanagement.koplugin/main.lua index 6c447c9c8..688d1b75f 100644 --- a/plugins/patchmanagement.koplugin/main.lua +++ b/plugins/patchmanagement.koplugin/main.lua @@ -7,7 +7,8 @@ Plugin for managing user patches local DataStorage = require("datastorage") local lfs = require("libs/libkoreader-lfs") -if lfs.attributes(DataStorage:getDataDir() .. "/patches", "mode") ~= "directory" then +local patch_dir = DataStorage:getDataDir() .. "/patches" +if lfs.attributes(patch_dir, "mode") ~= "directory" then return { disabled = true } end @@ -20,30 +21,41 @@ local sort = require("sort") local userPatch = require("userpatch") local _ = require("gettext") local Screen = Device.screen +local T = require("ffi/util").template local PatchManagement = WidgetContainer:extend{ name = "patch_management", + disable_ext = ".disabled", + patches = nil, +} + +local priority_first = tonumber(userPatch.early_once) +local priority_last = tonumber(userPatch.on_exit) +local priorities = { + [priority_first] = _("On startup, only after update"), + [tonumber(userPatch.early)] = _("On startup"), + [tonumber(userPatch.late)] = _("After setup"), + [tonumber(userPatch.before_exit)] = _("Before exit"), + [priority_last] = _("On exit"), } function PatchManagement:init() - self.patch_dir = DataStorage:getDataDir() .. "/patches" - self.disable_ext = ".disabled" - self.patches = nil - self:getAvailablePatches() self.ui.menu:registerToMainMenu(self) end function PatchManagement:getAvailablePatches() self.patches = {} - for priority = tonumber(userPatch.early_once), tonumber(userPatch.on_exit) do - self.patches[priority] = {} + for priority = priority_first, priority_last do + if priorities[priority] then + self.patches[priority] = {} + end end - for entry in lfs.dir(self.patch_dir) do - local mode = lfs.attributes(self.patch_dir .. "/" .. entry, "mode") - if mode == "file" then - for priority = tonumber(userPatch.early_once), tonumber(userPatch.on_exit) do - if entry:match("^" .. priority .. "%d*%-") and entry:find(".lua", 1, true) then + for entry in lfs.dir(patch_dir) do + local mode = lfs.attributes(patch_dir .. "/" .. entry, "mode") + if mode == "file" and entry:find(".lua", 1, true) then + for priority = priority_first, priority_last do + if priorities[priority] and entry:match("^" .. priority .. "%d*%-") then -- only lua files, starting with "%d+%-" table.insert(self.patches[priority], entry) break @@ -52,74 +64,54 @@ function PatchManagement:getAvailablePatches() end end - for priority = tonumber(userPatch.early_once), tonumber(userPatch.on_exit) do - table.sort(self.patches[priority], sort.natsort_cmp()) + for priority = priority_first, priority_last do + if priorities[priority] and #self.patches[priority] > 1 then + table.sort(self.patches[priority], sort.natsort_cmp()) + end end end function PatchManagement:getSubMenu(priority) - if self.patches == nil then - return {} - end - local function getExecutionStatus(patch_name) - return userPatch.execution_status[patch_name] == false and " ⚠" or "" - end local sub_menu = {} for i, patch in ipairs(self.patches[priority]) do local ext = ".lua" -- strip anything after ".lua" in patch_name - local patch_name = patch - patch_name = patch_name:sub(1, patch_name:find(ext, 1, true) + ext:len() - 1) + local patch_name = patch:sub(1, patch:find(ext, 1, true) + ext:len() - 1) table.insert(sub_menu, { - text = patch_name .. getExecutionStatus(patch_name), - checked_func = function() - return patch:find("%.lua$") ~= nil - end, - callback = function() - local extension_pos = patch:find(ext, 1, true) - if extension_pos then - local is_patch_enabled = extension_pos == patch:len() - (ext:len() - 1) - if is_patch_enabled then -- patch name ends with ".lua" - local disabled_name = patch .. self.disable_ext - os.rename(self.patch_dir .. "/" .. patch, - self.patch_dir .. "/" .. disabled_name) - patch = disabled_name - else -- patch name name contains ".lua" - local enabled_name = patch:sub(1, extension_pos + ext:len() - 1) - os.rename(self.patch_dir .. "/" .. patch, - self.patch_dir .. "/" .. enabled_name) - patch = enabled_name - end + text = patch_name:sub(1,-5) .. (userPatch.execution_status[patch_name] == false and " ⚠" or ""), + checked_func = function() + return patch:find("%.lua$") ~= nil + end, + callback = function() + local extension_pos = patch:find(ext, 1, true) + if extension_pos then + local is_patch_enabled = extension_pos == patch:len() - (ext:len() - 1) + if is_patch_enabled then -- patch name ends with ".lua" + local disabled_name = patch .. self.disable_ext + os.rename(patch_dir .. "/" .. patch, patch_dir .. "/" .. disabled_name) + patch = disabled_name + else -- patch contains ".lua" + local enabled_name = patch:sub(1, extension_pos + ext:len() - 1) + os.rename(patch_dir .. "/" .. patch, patch_dir .. "/" .. enabled_name) + patch = enabled_name end - UIManager:askForRestart( - _("Patches changed. Current set of patches will be applied on next restart.")) - end, - hold_callback = function() - local patch_fullpath = self.patch_dir .. "/" .. patch - if self.ui.texteditor then - local function done_callback() - UIManager:askForRestart( - _("Patches might have changed. Current set of patches will be applied on next restart.")) - end - self.ui.texteditor:quickEditFile(patch_fullpath, done_callback, false) - else - local file = io.open(patch_fullpath, "rb") - if not file then - return "" - end - local patch_content = file:read("*all") - file:close() - - local textviewer - textviewer = TextViewer:new{ - title = patch, - text = patch_content, - text_type = "code", - } - UIManager:show(textviewer) + end + UIManager:askForRestart( + _("Patches changed. Current set of patches will be applied on next restart.")) + end, + hold_callback = function() + local patch_fullpath = patch_dir .. "/" .. patch + if self.ui.texteditor then + local function done_callback() + UIManager:askForRestart( + _("Patches might have changed. Current set of patches will be applied on next restart.")) end - end, - }) + self.ui.texteditor:quickEditFile(patch_fullpath, done_callback, false) + else + TextViewer.openFile(patch_fullpath) + end + end, + }) end return sub_menu end @@ -136,55 +128,65 @@ Patches are an advanced feature, so be careful what you do!]]) function PatchManagement:addToMainMenu(menu_items) menu_items.patch_management = { text = _("Patch management"), - enabled_func = function() - if self.patches == nil then - return false - end - for i = tonumber(userPatch.early_once), tonumber(userPatch.on_exit) do - if #self.patches[i] > 0 then - return true -- we have at least one patch in the patches folder - end - end - return false - end, - sub_item_table = { - { - text = _("About patch management"), - callback = function() - UIManager:show(InfoMessage:new{ + sub_item_table_func = function() + local sub_item_table = { + { + text = _("About patch management"), + keep_menu_open = true, + callback = function() + UIManager:show(InfoMessage:new{ text = about_text, width = math.floor(Screen:getWidth() * 0.9), }) - end, - separator = true, - keep_menu_open = true, - }, - { - text = _("Patches executed:"), - enabled = false, - }, - } - } - - local sub_menu_text = {} - sub_menu_text[tonumber(userPatch.early_once)] = _("On startup, only after update") - sub_menu_text[tonumber(userPatch.early)] = _("On startup") - sub_menu_text[tonumber(userPatch.late)] = _("After setup") - sub_menu_text[tonumber(userPatch.before_exit)] = _("Before exit") - sub_menu_text[tonumber(userPatch.on_exit)] = _("On exit") - - for i = tonumber(userPatch.early_once), tonumber(userPatch.on_exit) do - if sub_menu_text[i] then - table.insert(menu_items.patch_management.sub_item_table, - { - text = sub_menu_text[i], - enabled_func = function() - return #self.patches[i] > 0 end, - sub_item_table = self:getSubMenu(i) - }) - end - end + separator = true, + }, + { + text_func = function() + local count = 0 + for _, status in pairs(userPatch.execution_status) do + if status then + count = count + 1 + end + end + return T(_("Patches executed: %1"), count) + end, + keep_menu_open = true, + callback = function() + local txt = {} + for patch, status in pairs(userPatch.execution_status) do + if status then + table.insert(txt, patch:sub(1,-5)) + end + end + if #txt > 0 then + table.sort(txt, sort.natsort_cmp()) + UIManager:show(InfoMessage:new{ + text = table.concat(txt, "\n"), + monospace_font = true, + }) + end + end, + separator = true, + }, + } + + self:getAvailablePatches() + for priority = priority_first, priority_last do + if priorities[priority] then + table.insert(sub_item_table, { + text = priorities[priority], + enabled_func = function() + return #self.patches[priority] > 0 + end, + sub_item_table = self:getSubMenu(priority), + }) + end + end + + return sub_item_table + end, + } end return PatchManagement