Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show full ToC entry on hold #6729

Merged
merged 15 commits into from Sep 30, 2020
4 changes: 2 additions & 2 deletions frontend/apps/reader/modules/readerstyletweak.lua
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions frontend/apps/reader/modules/readertoc.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion frontend/device/generic/device.lua
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion frontend/util.lua
Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion plugins/statistics.koplugin/main.lua
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/util_spec.lua
Expand Up @@ -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
Expand Down