From c079eb855d07134614f3af4df2833baab892416f Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Sat, 2 Jan 2021 18:17:23 +0100 Subject: [PATCH] Add support for string escapes like \n in label texts (fixes #84) --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/widgets/widget_definitions.rs | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index bc5ff20..0378c40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -364,6 +364,7 @@ dependencies = [ "smart-default", "stoppable_thread", "structopt", + "unescape", ] [[package]] @@ -2000,6 +2001,12 @@ dependencies = [ "serde", ] +[[package]] +name = "unescape" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccb97dac3243214f8d8507998906ca3e2e0b900bf9bf4870477f125b82e68f6e" + [[package]] name = "unicode-segmentation" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 57d96fc..1f235df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ smart-default = "0.6" filedescriptor = "0.7" simple-signal = "1.1" dashmap = "3.11" +unescape = "0.1" [dev-dependencies] pretty_assertions = "0.6.1" diff --git a/src/widgets/widget_definitions.rs b/src/widgets/widget_definitions.rs index 18e8ea3..a8d4365 100644 --- a/src/widgets/widget_definitions.rs +++ b/src/widgets/widget_definitions.rs @@ -342,7 +342,9 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result { // @prop text - the text to display // @prop limit-width - maximum count of characters to display prop(text: as_string, limit_width: as_i32 = i32::MAX) { - gtk_widget.set_text(&text.chars().take(limit_width as usize).collect::()); + let text = text.chars().take(limit_width as usize).collect::(); + let text = unescape::unescape(&text).context(format!("Failed to unescape label text {}", &text))?; + gtk_widget.set_text(&text); }, // @prop markup - Pango markup to display prop(markup: as_string) {