From ec3ec8dc21c87fb90516b77b5e1bd383a2d38d87 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Wed, 30 Sep 2020 19:56:56 +0200 Subject: [PATCH] Show full ToC entry on hold (#6729) Fix #6728 --- .../apps/reader/modules/readerstyletweak.lua | 4 +-- frontend/apps/reader/modules/readertoc.lua | 13 +++++++++ frontend/device/generic/device.lua | 2 +- frontend/util.lua | 29 ++++++++++++++++++- plugins/statistics.koplugin/main.lua | 2 +- spec/unit/util_spec.lua | 6 ++-- 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/frontend/apps/reader/modules/readerstyletweak.lua b/frontend/apps/reader/modules/readerstyletweak.lua index c00ab371a..56b1c7510 100644 --- a/frontend/apps/reader/modules/readerstyletweak.lua +++ b/frontend/apps/reader/modules/readerstyletweak.lua @@ -91,7 +91,7 @@ function TweakInfoWidget:init() f:close() end end - self.css_text = css:gsub("^%s+", ""):gsub("%s+$", "") + self.css_text = util.trim(css) self.css_frame = FrameContainer:new{ bordersize = Size.border.thin, padding = Size.padding.large, @@ -309,7 +309,7 @@ function ReaderStyleTweak:updateCssText(apply) -- re-reading it, but this will allow a user to experiment -- wihout having to restart KOReader end - css = css:gsub("^%s+", ""):gsub("%s+$", "") + css = util.trim(css) table.insert(css_snippets, css) end if self.book_style_tweak and self.book_style_tweak_enabled then diff --git a/frontend/apps/reader/modules/readertoc.lua b/frontend/apps/reader/modules/readertoc.lua index 00d299e09..e6b7c5b29 100644 --- a/frontend/apps/reader/modules/readertoc.lua +++ b/frontend/apps/reader/modules/readertoc.lua @@ -7,10 +7,12 @@ local Event = require("ui/event") local Font = require("ui/font") local GestureRange = require("ui/gesturerange") local Geom = require("ui/geometry") +local InfoMessage = require("ui/widget/infomessage") local InputContainer = require("ui/widget/container/inputcontainer") local Menu = require("ui/widget/menu") local UIManager = require("ui/uimanager") local logger = require("logger") +local util = require("util") local _ = require("gettext") local Screen = Device.screen local T = require("ffi/util").template @@ -582,6 +584,17 @@ function ReaderToc:onShowToc() end end + function toc_menu:onMenuHold(item) + -- Trim toc_indent + local trimmed_text = util.ltrim(item.text) + local infomessage = InfoMessage:new{ + show_icon = false, + text = trimmed_text, + } + UIManager:show(infomessage) + return true + end + toc_menu.close_callback = function() UIManager:close(menu_container) end diff --git a/frontend/device/generic/device.lua b/frontend/device/generic/device.lua index d0c27272b..6a9e27ffe 100644 --- a/frontend/device/generic/device.lua +++ b/frontend/device/generic/device.lua @@ -422,7 +422,7 @@ function Device:retrieveNetworkInfo() std_out = io.popen('2>/dev/null iwconfig | grep ESSID | cut -d\\" -f2') if std_out then local ssid = std_out:read("*all") - result = result .. "SSID: " .. ssid:gsub("(.-)%s*$", "%1") .. "\n" + result = result .. "SSID: " .. util.trim(ssid) .. "\n" std_out:close() end if os.execute("ip r | grep -q default") == 0 then diff --git a/frontend/util.lua b/frontend/util.lua index ea7a4d689..548d2656e 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -14,7 +14,7 @@ local bor = bit.bor local util = {} ---- Strips all punctuation marks and spaces from a string. +---- Strips all punctuation marks and spaces from a string. ---- @string text the string to be stripped ---- @treturn string stripped text function util.stripPunctuation(text) @@ -24,6 +24,33 @@ function util.stripPunctuation(text) return text:gsub("\226[\128-\131][\128-\191]", ''):gsub("^%p+", ''):gsub("%p+$", '') end +-- Various whitespace trimming helpers, from http://lua-users.org/wiki/CommonFunctions & http://lua-users.org/wiki/StringTrim +---- Remove leading whitespace from string. +---- @string s the string to be trimmed +---- @treturn string trimmed text +function util.ltrim(s) + return (s:gsub("^%s*", "")) +end + +---- Remove trailing whitespace from string. +---- @string s the string to be trimmed +---- @treturn string trimmed text +function util.rtrim(s) + local n = #s + while n > 0 and s:find("^%s", n) do + n = n - 1 + end + return s:sub(1, n) +end + +---- Remove leading & trailing whitespace from string. +---- @string s the string to be trimmed +---- @treturn string trimmed text +function util.trim(s) + local from = s:match"^%s*()" + return from > #s and "" or s:match(".*%S", from) +end + --[[-- Splits a string by a pattern diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index 8ce0989f8..eaf80e748 100644 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -1896,7 +1896,7 @@ end -- For backward compatibility function ReaderStatistics:importFromFile(base_path, item) - item = string.gsub(item, "^%s*(.-)%s*$", "%1") -- trim + item = util.trim(item) if item ~= ".stat" then local statistic_file = FFIUtil.joinPath(base_path, item) if lfs.attributes(statistic_file, "mode") == "directory" then diff --git a/spec/unit/util_spec.lua b/spec/unit/util_spec.lua index 734a813a1..08c974730 100644 --- a/spec/unit/util_spec.lua +++ b/spec/unit/util_spec.lua @@ -31,9 +31,9 @@ describe("util module", function() for arg1 in util.gsplit(command, "[\"'].-[\"']", true) do for arg2 in util.gsplit(arg1, "^[^\"'].-%s+", true) do for arg3 in util.gsplit(arg2, "[\"']", false) do - local trimed = arg3:gsub("^%s*(.-)%s*$", "%1") - if trimed ~= "" then - table.insert(argv, trimed) + local trimmed = util.trim(arg3) + if trimmed ~= "" then + table.insert(argv, trimmed) end end end