ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
4bff0a
From bc00d9db41a87b7a4b92c46f277e62ad58768420 Mon Sep 17 00:00:00 2001
4bff0a
From: Lennart Poettering <lennart@poettering.net>
4bff0a
Date: Tue, 6 Nov 2018 17:59:58 +0100
4bff0a
Subject: [PATCH] locale-util: add logic to output smiley emojis at various
4bff0a
 happiness levels
4bff0a
4bff0a
(cherry picked from commit 5f1b0cc6d064f7847982e7b680cab3d080aef52e)
4bff0a
4bff0a
Conflicts:
4bff0a
	doc/ENVIRONMENT.md
4bff0a
	src/basic/locale-util.c
4bff0a
	src/basic/locale-util.h
4bff0a
	src/test/test-locale-util.c
4bff0a
4bff0a
Related: #1689832
4bff0a
---
4bff0a
 doc/ENVIRONMENT.md          |  5 +++
4bff0a
 src/basic/locale-util.c     | 81 ++++++++++++++++++++++++++++---------
4bff0a
 src/basic/locale-util.h     | 12 ++++++
4bff0a
 src/test/test-locale-util.c | 12 +++++-
4bff0a
 4 files changed, 90 insertions(+), 20 deletions(-)
4bff0a
4bff0a
diff --git a/doc/ENVIRONMENT.md b/doc/ENVIRONMENT.md
4bff0a
index 85d26fe28c..1e648be640 100644
4bff0a
--- a/doc/ENVIRONMENT.md
4bff0a
+++ b/doc/ENVIRONMENT.md
4bff0a
@@ -37,6 +37,11 @@ All tools:
4bff0a
   useful for debugging, in order to test generators and other code against
4bff0a
   specific kernel command lines.
4bff0a
 
4bff0a
+* `$SYSTEMD_EMOJI=0` — if set, tools such as "systemd-analyze security" will
4bff0a
+  not output graphical smiley emojis, but ASCII alternatives instead. Note that
4bff0a
+  this only controls use of Unicode emoji glyphs, and has no effect on other
4bff0a
+  Unicode glyphs.
4bff0a
+
4bff0a
 systemctl:
4bff0a
 
4bff0a
 * `$SYSTEMCTL_FORCE_BUS=1` — if set, do not connect to PID1's private D-Bus
4bff0a
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
4bff0a
index 3ad352f22f..7cd143ea6f 100644
4bff0a
--- a/src/basic/locale-util.c
4bff0a
+++ b/src/basic/locale-util.c
4bff0a
@@ -16,6 +16,7 @@
4bff0a
 
4bff0a
 #include "def.h"
4bff0a
 #include "dirent-util.h"
4bff0a
+#include "env-util.h"
4bff0a
 #include "fd-util.h"
4bff0a
 #include "hashmap.h"
4bff0a
 #include "locale-util.h"
4bff0a
@@ -347,6 +348,24 @@ bool keymap_is_valid(const char *name) {
4bff0a
         return true;
4bff0a
 }
4bff0a
 
4bff0a
+static bool emoji_enabled(void) {
4bff0a
+        static int cached_emoji_enabled = -1;
4bff0a
+
4bff0a
+        if (cached_emoji_enabled < 0) {
4bff0a
+                int val;
4bff0a
+
4bff0a
+                val = getenv_bool("SYSTEMD_EMOJI");
4bff0a
+                if (val < 0)
4bff0a
+                        cached_emoji_enabled =
4bff0a
+                                is_locale_utf8() &&
4bff0a
+                                !STRPTR_IN_SET(getenv("TERM"), "dumb", "linux");
4bff0a
+                else
4bff0a
+                        cached_emoji_enabled = val;
4bff0a
+        }
4bff0a
+
4bff0a
+        return cached_emoji_enabled;
4bff0a
+}
4bff0a
+
4bff0a
 const char *special_glyph(SpecialGlyph code) {
4bff0a
 
4bff0a
         /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
4bff0a
@@ -359,32 +378,56 @@ const char *special_glyph(SpecialGlyph code) {
4bff0a
         static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = {
4bff0a
                 /* ASCII fallback */
4bff0a
                 [false] = {
4bff0a
-                        [TREE_VERTICAL]      = "| ",
4bff0a
-                        [TREE_BRANCH]        = "|-",
4bff0a
-                        [TREE_RIGHT]         = "`-",
4bff0a
-                        [TREE_SPACE]         = "  ",
4bff0a
-                        [TRIANGULAR_BULLET]  = ">",
4bff0a
-                        [BLACK_CIRCLE]       = "*",
4bff0a
-                        [ARROW]              = "->",
4bff0a
-                        [MDASH]              = "-",
4bff0a
-                        [ELLIPSIS]           = "..."
4bff0a
+                        [TREE_VERTICAL]           = "| ",
4bff0a
+                        [TREE_BRANCH]             = "|-",
4bff0a
+                        [TREE_RIGHT]              = "`-",
4bff0a
+                        [TREE_SPACE]              = "  ",
4bff0a
+                        [TRIANGULAR_BULLET]       = ">",
4bff0a
+                        [BLACK_CIRCLE]            = "*",
4bff0a
+                        [BULLET]                  = "*",
4bff0a
+                        [ARROW]                   = "->",
4bff0a
+                        [MDASH]                   = "-",
4bff0a
+                        [ELLIPSIS]                = "...",
4bff0a
+                        [MU]                      = "u",
4bff0a
+                        [CHECK_MARK]              = "+",
4bff0a
+                        [CROSS_MARK]              = "-",
4bff0a
+                        [ECSTATIC_SMILEY]         = ":-]",
4bff0a
+                        [HAPPY_SMILEY]            = ":-}",
4bff0a
+                        [SLIGHTLY_HAPPY_SMILEY]   = ":-)",
4bff0a
+                        [NEUTRAL_SMILEY]          = ":-|",
4bff0a
+                        [SLIGHTLY_UNHAPPY_SMILEY] = ":-(",
4bff0a
+                        [UNHAPPY_SMILEY]          = ":-{️",
4bff0a
+                        [DEPRESSED_SMILEY]        = ":-[",
4bff0a
                 },
4bff0a
 
4bff0a
                 /* UTF-8 */
4bff0a
                 [true] = {
4bff0a
-                        [TREE_VERTICAL]      = "\342\224\202 ",            /* │  */
4bff0a
-                        [TREE_BRANCH]        = "\342\224\234\342\224\200", /* ├─ */
4bff0a
-                        [TREE_RIGHT]         = "\342\224\224\342\224\200", /* └─ */
4bff0a
-                        [TREE_SPACE]         = "  ",                       /*    */
4bff0a
-                        [TRIANGULAR_BULLET]  = "\342\200\243",             /* ‣ */
4bff0a
-                        [BLACK_CIRCLE]       = "\342\227\217",             /* ● */
4bff0a
-                        [ARROW]              = "\342\206\222",             /* → */
4bff0a
-                        [MDASH]              = "\342\200\223",             /* – */
4bff0a
-                        [ELLIPSIS]           = "\342\200\246",             /* … */
4bff0a
+                        [TREE_VERTICAL]           = "\342\224\202 ",            /* │  */
4bff0a
+                        [TREE_BRANCH]             = "\342\224\234\342\224\200", /* ├─ */
4bff0a
+                        [TREE_RIGHT]              = "\342\224\224\342\224\200", /* └─ */
4bff0a
+                        [TREE_SPACE]              = "  ",                       /*    */
4bff0a
+                        [TRIANGULAR_BULLET]       = "\342\200\243",             /* ‣ */
4bff0a
+                        [BLACK_CIRCLE]            = "\342\227\217",             /* ● */
4bff0a
+                        [BULLET]                  = "\342\200\242",             /* • */
4bff0a
+                        [ARROW]                   = "\342\206\222",             /* → */
4bff0a
+                        [MDASH]                   = "\342\200\223",             /* – */
4bff0a
+                        [ELLIPSIS]                = "\342\200\246",             /* … */
4bff0a
+                        [MU]                      = "\316\274",                 /* μ */
4bff0a
+                        [CHECK_MARK]              = "\342\234\223",             /* ✓ */
4bff0a
+                        [CROSS_MARK]              = "\342\234\227",             /* ✗ */
4bff0a
+                        [ECSTATIC_SMILEY]         = "\360\237\230\207",         /* 😇 */
4bff0a
+                        [HAPPY_SMILEY]            = "\360\237\230\200",         /* 😀 */
4bff0a
+                        [SLIGHTLY_HAPPY_SMILEY]   = "\360\237\231\202",         /* 🙂 */
4bff0a
+                        [NEUTRAL_SMILEY]          = "\360\237\230\220",         /* 😐 */
4bff0a
+                        [SLIGHTLY_UNHAPPY_SMILEY] = "\360\237\231\201",         /* 🙁 */
4bff0a
+                        [UNHAPPY_SMILEY]          = "\360\237\230\250",         /* 😨️️ */
4bff0a
+                        [DEPRESSED_SMILEY]        = "\360\237\244\242",         /* 🤢 */
4bff0a
                 },
4bff0a
         };
4bff0a
 
4bff0a
-        return draw_table[is_locale_utf8()][code];
4bff0a
+        assert(code < _SPECIAL_GLYPH_MAX);
4bff0a
+
4bff0a
+        return draw_table[code >= _SPECIAL_GLYPH_FIRST_SMILEY ? emoji_enabled() : is_locale_utf8()][code];
4bff0a
 }
4bff0a
 
4bff0a
 static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
4bff0a
diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h
4bff0a
index 775fe8bc72..368675f286 100644
4bff0a
--- a/src/basic/locale-util.h
4bff0a
+++ b/src/basic/locale-util.h
4bff0a
@@ -45,9 +45,21 @@ typedef enum {
4bff0a
         TREE_SPACE,
4bff0a
         TRIANGULAR_BULLET,
4bff0a
         BLACK_CIRCLE,
4bff0a
+        BULLET,
4bff0a
         ARROW,
4bff0a
         MDASH,
4bff0a
         ELLIPSIS,
4bff0a
+        MU,
4bff0a
+        CHECK_MARK,
4bff0a
+        CROSS_MARK,
4bff0a
+        _SPECIAL_GLYPH_FIRST_SMILEY,
4bff0a
+        ECSTATIC_SMILEY = _SPECIAL_GLYPH_FIRST_SMILEY,
4bff0a
+        HAPPY_SMILEY,
4bff0a
+        SLIGHTLY_HAPPY_SMILEY,
4bff0a
+        NEUTRAL_SMILEY,
4bff0a
+        SLIGHTLY_UNHAPPY_SMILEY,
4bff0a
+        UNHAPPY_SMILEY,
4bff0a
+        DEPRESSED_SMILEY,
4bff0a
         _SPECIAL_GLYPH_MAX
4bff0a
 } SpecialGlyph;
4bff0a
 
4bff0a
diff --git a/src/test/test-locale-util.c b/src/test/test-locale-util.c
4bff0a
index 8ffae8ca03..0c3f6a62ed 100644
4bff0a
--- a/src/test/test-locale-util.c
4bff0a
+++ b/src/test/test-locale-util.c
4bff0a
@@ -65,7 +65,7 @@ static void test_keymaps(void) {
4bff0a
 
4bff0a
 #define dump_glyph(x) log_info(STRINGIFY(x) ": %s", special_glyph(x))
4bff0a
 static void dump_special_glyphs(void) {
4bff0a
-        assert_cc(ELLIPSIS + 1 == _SPECIAL_GLYPH_MAX);
4bff0a
+        assert_cc(DEPRESSED_SMILEY + 1 == _SPECIAL_GLYPH_MAX);
4bff0a
 
4bff0a
         log_info("/* %s */", __func__);
4bff0a
 
4bff0a
@@ -80,6 +80,16 @@ static void dump_special_glyphs(void) {
4bff0a
         dump_glyph(ARROW);
4bff0a
         dump_glyph(MDASH);
4bff0a
         dump_glyph(ELLIPSIS);
4bff0a
+        dump_glyph(MU);
4bff0a
+        dump_glyph(CHECK_MARK);
4bff0a
+        dump_glyph(CROSS_MARK);
4bff0a
+        dump_glyph(ECSTATIC_SMILEY);
4bff0a
+        dump_glyph(HAPPY_SMILEY);
4bff0a
+        dump_glyph(SLIGHTLY_HAPPY_SMILEY);
4bff0a
+        dump_glyph(NEUTRAL_SMILEY);
4bff0a
+        dump_glyph(SLIGHTLY_UNHAPPY_SMILEY);
4bff0a
+        dump_glyph(UNHAPPY_SMILEY);
4bff0a
+        dump_glyph(DEPRESSED_SMILEY);
4bff0a
 }
4bff0a
 
4bff0a
 int main(int argc, char *argv[]) {