markup in status message patch

This commit is contained in:
Ben Collerson 2022-11-29 20:33:47 +10:00 committed by Raphael Robatsch
parent fb2c8e5f42
commit c076d343fc

View File

@ -0,0 +1,65 @@
From: Ben Collerson <benc@benc.cc>
Date: Tue, 29 Nov 2022 11:29:15 +1000
Subject: [PATCH] markup in status messages
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Allows pango markup to be used in status messages which allow colours to
be used to highlight parts of status bar messages. eg:
battery: full <span color="#ffff00">🔇0</span> Tue 11-20 20:10
---
src/bar.cpp | 16 +++++++++++++++-
src/bar.hpp | 1 +
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/bar.cpp b/src/bar.cpp
index 507ce62..a96c547 100644
--- a/src/bar.cpp
+++ b/src/bar.cpp
@@ -81,6 +81,11 @@ void BarComponent::setText(const std::string& text)
pango_layout_set_text(pangoLayout.get(), _text->c_str(), _text->size());
}
+void BarComponent::setAttributes(PangoAttrList *attrs)
+{
+ pango_layout_set_attributes(pangoLayout.get(), attrs);
+}
+
Bar::Bar()
{
_pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default()));
@@ -156,7 +161,16 @@ void Bar::setTitle(const std::string& title)
}
void Bar::setStatus(const std::string& status)
{
- _statusCmp.setText(status);
+ char *buf;
+ GError *error = NULL;
+ PangoAttrList *attrs;
+ if (pango_parse_markup(status.c_str(), -1, 0, &attrs, &buf, NULL, &error)) {
+ _statusCmp.setText(buf);
+ _statusCmp.setAttributes(attrs);
+ }
+ else {
+ _statusCmp.setText(error->message);
+ }
}
void Bar::invalidate()
diff --git a/src/bar.hpp b/src/bar.hpp
index 176a1bc..dfc2043 100644
--- a/src/bar.hpp
+++ b/src/bar.hpp
@@ -17,6 +17,7 @@ public:
explicit BarComponent(wl_unique_ptr<PangoLayout> layout);
int width() const;
void setText(const std::string& text);
+ void setAttributes(PangoAttrList *attrs);
wl_unique_ptr<PangoLayout> pangoLayout;
int x {0};
};
--
2.38.1