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
tbm:set_margins(0)
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>")
end
end

View File

@ -350,7 +350,7 @@ local function set_text(notification, title, text)
local textbox = notification.textbox
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
local function setText()
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.
title = title:gsub(escape_pattern, escape_subs)
-- Try to set the text while only interpreting <br>.
-- (Setting a textbox' .text to an invalid pattern throws a lua error)
if not pcall(setMarkup, "<br.->", "\n") then
if not setMarkup("<br.->", "\n") then
-- 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
if not pcall(setText) then
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 gdebug = require("gears.debug")
local beautiful = require("beautiful")
local lgi = require("lgi")
local cairo = lgi.cairo
@ -118,20 +119,38 @@ end
-- @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)
-- @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
return
return true
end
local attr, parsed = Pango.parse_markup(text, -1, 0)
-- 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._layout.text = parsed
self._layout.attributes = attr
self:emit_signal("widget::redraw_needed")
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
--- Set a textbox' text.