Skip to content

Instantly share code, notes, and snippets.

@sebdelsol
Last active May 4, 2025 22:39
Show Gist options
  • Save sebdelsol/aba0d15f9f13d77a2f7a8be67278f93d to your computer and use it in GitHub Desktop.
Save sebdelsol/aba0d15f9f13d77a2f7a8be67278f93d to your computer and use it in GitHub Desktop.
KOReader user patch: add chapter title and % in the screensaver message
local InfoMessage = require("ui/widget/infomessage")
local Math = require("optmath")
local ReaderUI = require("apps/reader/readerui")
local Screensaver = require("ui/screensaver")
local UIManager = require("ui/uimanager")
local _ = require("gettext")
local expandSpecial_orig = Screensaver.expandSpecial
Screensaver.expandSpecial = function(self, message)
local chapter_title = "n/a"
local chapter_percent = 0
local msg = expandSpecial_orig(self, message)
local ui = ReaderUI.instance
if ui then
local current_page = ui.view.state.page or 1
chapter_title = ui.toc:getTocTitleByPage(current_page)
if chapter_title and chapter_title ~= "" then
chapter_title = chapter_title:gsub(" ", "\xC2\xA0") -- replace space with no-break-space
end
local ch_read = ui.toc:getChapterPagesDone(current_page)
ch_read = ch_read + 1
local ch_count = ui.toc:getChapterPageCount(current_page)
if ch_read == 1 then
chapter_percent = 0
elseif ch_read == ch_count then
chapter_percent = 100
else
chapter_percent = Math.round(Math.clamp(((ch_read * 100) / ch_count), 1, 99))
end
end
local replace = {
["%C"] = chapter_title,
["%P"] = chapter_percent,
}
msg = msg:gsub("(%%%a)", replace)
return msg
end
local info_text = ([[
%T title
%A author(s)
%S series
%c current page number
%t total page number
%p percentage read
%h time left in chapter
%H time left in document
%b battery level
%B battery symbol
%C chapter title
%P chapter percent]])
local orig_Screensaver_setMessage = Screensaver.setMessage
Screensaver.setMessage = function(self)
-- local logger = require("logger")
orig_Screensaver_setMessage(self)
for widget in UIManager:topdown_widgets_iter() do
if widget.title == _("Sleep screen message") then
for _i, button in ipairs(widget.buttons[1]) do
if button.text == _("Info") then
button.callback = function()
UIManager:show(InfoMessage:new{
text = _(info_text),
monospace_font = true,
})
end
-- logger.info("Sleep screen messsage Info button patched")
return
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment