dryang / rpms / systemd

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