Textbox:set_markup(): Print errors to stderr

Instead of throwing a Lua error, the code now just prints an error message to
stderr on invalid markup. For callers which want to handle this case specially,
we add :set_markup_silently() which returns error messages.

Fixes: https://github.com/awesomeWM/awesome/issues/546
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-01-17 16:43:22 +01:00
parent 62495daa66
commit 178204449c
1 changed files with 22 additions and 3 deletions

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.