summaryrefslogtreecommitdiff
path: root/wm/somebar/contrib/markup-in-status-messages.patch
blob: ab7e998f5040a0da14cb5433e890aa9356107401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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