Merge pull request #637 from psychon/set_markup

Make wibox.widget.textbox:set_markup() throw Lua errors less often
This commit is contained in:
Daniel Hahler 2016-01-20 00:01:09 +01:00
commit cb267e56d5
3 changed files with 26 additions and 8 deletions

View File

@ -96,7 +96,7 @@ function common.list_update(w, buttons, label, data, objects)
if text == nil or text == "" then if text == nil or text == "" then
tbm:set_margins(0) tbm:set_margins(0)
else else
if not pcall(tb.set_markup, tb, text) then if not tb:set_markup_silently(text) then
tb:set_markup("<i>&lt;Invalid text&gt;</i>") tb:set_markup("<i>&lt;Invalid text&gt;</i>")
end end
end end

View File

@ -350,7 +350,7 @@ local function set_text(notification, title, text)
local textbox = notification.textbox local textbox = notification.textbox
local function setMarkup(pattern, replacements) local function setMarkup(pattern, replacements)
textbox:set_markup(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements))) return textbox:set_markup_silently(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements)))
end end
local function setText() local function setText()
textbox:set_text(string.format('%s %s', title, text)) textbox:set_text(string.format('%s %s', title, text))
@ -360,10 +360,9 @@ local function set_text(notification, title, text)
-- it is not interpreted by Pango later. -- it is not interpreted by Pango later.
title = title:gsub(escape_pattern, escape_subs) title = title:gsub(escape_pattern, escape_subs)
-- Try to set the text while only interpreting <br>. -- Try to set the text while only interpreting <br>.
-- (Setting a textbox' .text to an invalid pattern throws a lua error) if not setMarkup("<br.->", "\n") then
if not pcall(setMarkup, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango -- That failed, escape everything which might cause an error from pango
if not pcall(setMarkup, escape_pattern, escape_subs) then if not setMarkup(escape_pattern, escape_subs) then
-- Ok, just ignore all pango markup. If this fails, we got some invalid utf8 -- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
if not pcall(setText) then if not pcall(setText) then
textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>") textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>")

View File

@ -7,6 +7,7 @@
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local gdebug = require("gears.debug")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local lgi = require("lgi") local lgi = require("lgi")
local cairo = lgi.cairo local cairo = lgi.cairo
@ -118,20 +119,38 @@ end
-- @tparam string text The text to set. This can contain pango markup (e.g. -- @tparam string text The text to set. This can contain pango markup (e.g.
-- `<b>bold</b>`). You can use `awful.util.escape` to escape -- `<b>bold</b>`). You can use `awful.util.escape` to escape
-- parts of it. -- parts of it.
function textbox:set_markup(text) -- @treturn[1] boolean true
-- @treturn[2] boolean false
-- @treturn[2] string Error message explaining why the markup was invalid.
function textbox:set_markup_silently(text)
if self._markup == text then if self._markup == text then
return return true
end end
local attr, parsed = Pango.parse_markup(text, -1, 0) local attr, parsed = Pango.parse_markup(text, -1, 0)
-- In case of error, attr is false and parsed is a GLib.Error instance. -- In case of error, attr is false and parsed is a GLib.Error instance.
if not attr then error(parsed.message or tostring(parsed)) end if not attr then
return false, parsed.message or tostring(parsed)
end
self._markup = text self._markup = text
self._layout.text = parsed self._layout.text = parsed
self._layout.attributes = attr self._layout.attributes = attr
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
return true
end
--- Set the text of the textbox (with
-- [Pango markup](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html)).
-- @tparam string text The text to set. This can contain pango markup (e.g.
-- `<b>bold</b>`). You can use `awful.util.escape` to escape
-- parts of it.
function textbox:set_markup(text)
local success, message = self:set_markup_silently(text)
if not success then
gdebug.print_error(message)
end
end end
--- Set a textbox' text. --- Set a textbox' text.