Skip to content

Instantly share code, notes, and snippets.

@sebdelsol
Last active May 26, 2025 15:29
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
-- **MOVED to https://github.com/sebdelsol/KOReader.patches**
-- Youd need a version >= v2025.04-12 to be able to show the screen saver's info 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 orig_Screensaver_expandSpecial = Screensaver.expandSpecial
Screensaver.expandSpecial = function(self, message)
local chapter_title = ""
local chapter_percent = 0
local msg = orig_Screensaver_expandSpecial(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)
if ch_read then
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
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)
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
return
end
end
end
end
end
@joespinoza
Copy link

these two options don't show:
%C chapter title
%P chapter percent

@sebdelsol
Copy link
Author

sebdelsol commented May 25, 2025

If you're in the file manager, those won't work: they only work when you're reading a book, because chapter information isn't accessible outside of a book. It should work in a book though.

@joespinoza
Copy link

I'm reading a book, they work, but what i mean is they don't show in the list

@sebdelsol
Copy link
Author

sebdelsol commented May 25, 2025

I'm not sure I understand what's your issue:

Is something else broken ?

@joespinoza
Copy link

ok, i don't have the info button, i'm running koreader 2025.04 in a PW4

@sebdelsol
Copy link
Author

Oh sorry I was not aware of that. I use the nightly build. I should add a warning @ the beginning of the file.

@sebdelsol
Copy link
Author

sebdelsol commented May 25, 2025

I've checked the KOReader commits: you need a version >= v2025.04-12 to have those information.
Tx a lot for your help.

@sebdelsol
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment