Debian Codegraph @

src/basic/glyph-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "env-util.h"
6 #include "glyph-util.h"
7 #include "locale-util.h"
8 #include "strv.h"
9
10 bool emoji_enabled(void) {
11 static int cached_emoji_enabled = -1;
12
13 if (cached_emoji_enabled < 0) {
14 int val = getenv_bool("SYSTEMD_EMOJI");
15 if (val >= 0)
16 return (cached_emoji_enabled = val);
17
18 const char *term = getenv("TERM");
19 if (!term || STR_IN_SET(term, "dumb", "linux"))
20 return (cached_emoji_enabled = false);
21
22 cached_emoji_enabled = is_locale_utf8();
23 }
24
25 return cached_emoji_enabled;
26 }
27
28 const char* glyph_full(Glyph code, bool force_utf) {
29
30 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
31 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
32 * works reasonably well on the Linux console. For details see:
33 *
34 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
35 */
36
37 static const char* const draw_table[2][_GLYPH_MAX] = {
38 /* ASCII fallback */
39 [false] = {
40 [GLYPH_SPACE] = " ",
41 [GLYPH_TREE_VERTICAL] = "| ",
42 [GLYPH_TREE_BRANCH] = "|-",
43 [GLYPH_TREE_RIGHT] = "`-",
44 [GLYPH_TREE_SPACE] = " ",
45 [GLYPH_TREE_TOP] = ",-",
46 [GLYPH_VERTICAL_DOTTED] = ":",
47 [GLYPH_HORIZONTAL_DOTTED] = "-",
48 [GLYPH_HORIZONTAL_FAT] = "=",
49 [GLYPH_TRIANGULAR_BULLET] = ">",
50 [GLYPH_BLACK_CIRCLE] = "*",
51 [GLYPH_WHITE_CIRCLE] = "*",
52 [GLYPH_MULTIPLICATION_SIGN] = "x",
53 [GLYPH_CIRCLE_ARROW] = "*",
54 [GLYPH_BULLET] = "*",
55 [GLYPH_MU] = "u",
56 [GLYPH_CHECK_MARK] = "+",
57 [GLYPH_CROSS_MARK] = "-",
58 [GLYPH_LIGHT_SHADE] = "-",
59 [GLYPH_DARK_SHADE] = "X",
60 [GLYPH_FULL_BLOCK] = "#",
61 [GLYPH_SIGMA] = "S",
62 [GLYPH_ARROW_UP] = "^",
63 [GLYPH_ARROW_DOWN] = "v",
64 [GLYPH_ARROW_LEFT] = "<-",
65 [GLYPH_ARROW_RIGHT] = "->",
66 [GLYPH_ELLIPSIS] = "...",
67 [GLYPH_EXTERNAL_LINK] = "[LNK]",
68 [GLYPH_ECSTATIC_SMILEY] = ":-]",
69 [GLYPH_HAPPY_SMILEY] = ":-}",
70 [GLYPH_SLIGHTLY_HAPPY_SMILEY] = ":-)",
71 [GLYPH_NEUTRAL_SMILEY] = ":-|",
72 [GLYPH_SLIGHTLY_UNHAPPY_SMILEY] = ":-(",
73 [GLYPH_UNHAPPY_SMILEY] = ":-{",
74 [GLYPH_DEPRESSED_SMILEY] = ":-[",
75 [GLYPH_LOCK_AND_KEY] = "o-,",
76 [GLYPH_TOUCH] = "O=", /* Yeah, not very convincing, can you do it better? */
77 [GLYPH_RECYCLING] = "~",
78 [GLYPH_DOWNLOAD] = "\\",
79 [GLYPH_SPARKLES] = "*",
80 [GLYPH_LOW_BATTERY] = "!",
81 [GLYPH_WARNING_SIGN] = "!",
82 [GLYPH_COMPUTER_DISK] = "o",
83 [GLYPH_WORLD] = "W",
84 [GLYPH_RED_CIRCLE] = "o",
85 [GLYPH_ORANGE_CIRCLE] = "o",
86 [GLYPH_YELLOW_CIRCLE] = "o",
87 [GLYPH_BLUE_CIRCLE] = "o",
88 [GLYPH_GREEN_CIRCLE] = "o",
89 [GLYPH_SUPERHERO] = "S",
90 [GLYPH_PUMPKIN] = "P",
91 [GLYPH_IDCARD] = "@",
92 [GLYPH_HOME] = "^",
93 [GLYPH_ROCKET] = "^",
94 [GLYPH_BROOM] = "/",
95 [GLYPH_KEYBOARD] = "K",
96 [GLYPH_CLOCK] = "O",
97 [GLYPH_LABEL] = "L",
98 [GLYPH_SHELL] = "$",
99 },
100
101 /* UTF-8 */
102 [true] = {
103 /* This exists to allow more consistent handling of optional whitespace */
104 [GLYPH_SPACE] = " ",
105
106 /* The following are multiple glyphs in both ASCII and in UNICODE */
107 [GLYPH_TREE_VERTICAL] = UTF8("│ "),
108 [GLYPH_TREE_BRANCH] = UTF8("├─"),
109 [GLYPH_TREE_RIGHT] = UTF8("└─"),
110 [GLYPH_TREE_SPACE] = UTF8(" "),
111 [GLYPH_TREE_TOP] = UTF8("┌─"),
112
113 /* Single glyphs in both cases */
114 [GLYPH_VERTICAL_DOTTED] = UTF8("┆"),
115 [GLYPH_HORIZONTAL_DOTTED] = UTF8("┄"),
116 [GLYPH_HORIZONTAL_FAT] = UTF8("━"),
117 [GLYPH_TRIANGULAR_BULLET] = UTF8("‣"),
118 [GLYPH_BLACK_CIRCLE] = UTF8("●"),
119 [GLYPH_WHITE_CIRCLE] = UTF8("○"),
120 [GLYPH_MULTIPLICATION_SIGN] = UTF8("×"),
121 [GLYPH_CIRCLE_ARROW] = UTF8("↻"),
122 [GLYPH_BULLET] = UTF8("•"),
123 [GLYPH_MU] = UTF8("μ"), /* actually called: GREEK SMALL LETTER MU */
124 [GLYPH_CHECK_MARK] = UTF8("✓"),
125 [GLYPH_CROSS_MARK] = UTF8("✗"), /* actually called: BALLOT X */
126 [GLYPH_LIGHT_SHADE] = UTF8("░"),
127 [GLYPH_DARK_SHADE] = UTF8("▒"),
128 [GLYPH_FULL_BLOCK] = UTF8("█"),
129 [GLYPH_SIGMA] = UTF8("Σ"),
130 [GLYPH_ARROW_UP] = UTF8("↑"), /* actually called: UPWARDS ARROW */
131 [GLYPH_ARROW_DOWN] = UTF8("↓"), /* actually called: DOWNWARDS ARROW */
132
133 /* Single glyph in Unicode, two in ASCII */
134 [GLYPH_ARROW_LEFT] = UTF8("←"), /* actually called: LEFTWARDS ARROW */
135 [GLYPH_ARROW_RIGHT] = UTF8("→"), /* actually called: RIGHTWARDS ARROW */
136
137 /* Single glyph in Unicode, three in ASCII */
138 [GLYPH_ELLIPSIS] = UTF8("…"), /* actually called: HORIZONTAL ELLIPSIS */
139
140 /* Three glyphs in Unicode, five in ASCII */
141 [GLYPH_EXTERNAL_LINK] = UTF8("[🡕]"), /* actually called: NORTH EAST SANS-SERIF ARROW, enclosed in [] */
142
143 /* These smileys are a single glyph in Unicode, and three in ASCII */
144 [GLYPH_ECSTATIC_SMILEY] = UTF8("😇"), /* actually called: SMILING FACE WITH HALO */
145 [GLYPH_HAPPY_SMILEY] = UTF8("😀"), /* actually called: GRINNING FACE */
146 [GLYPH_SLIGHTLY_HAPPY_SMILEY] = UTF8("🙂"), /* actually called: SLIGHTLY SMILING FACE */
147 [GLYPH_NEUTRAL_SMILEY] = UTF8("😐"), /* actually called: NEUTRAL FACE */
148 [GLYPH_SLIGHTLY_UNHAPPY_SMILEY] = UTF8("🙁"), /* actually called: SLIGHTLY FROWNING FACE */
149 [GLYPH_UNHAPPY_SMILEY] = UTF8("😨"), /* actually called: FEARFUL FACE */
150 [GLYPH_DEPRESSED_SMILEY] = UTF8("🤢"), /* actually called: NAUSEATED FACE */
151
152 /* This emoji is a single character cell glyph in Unicode, and three in ASCII */
153 [GLYPH_LOCK_AND_KEY] = UTF8("🔐"), /* actually called: CLOSED LOCK WITH KEY */
154
155 /* This emoji is a single character cell glyph in Unicode, and two in ASCII */
156 [GLYPH_TOUCH] = UTF8("👆"), /* actually called: BACKHAND INDEX POINTING UP */
157
158 /* These four emojis are single character cell glyphs in Unicode and also in ASCII. */
159 [GLYPH_RECYCLING] = UTF8("♻️"), /* actually called: UNIVERSAL RECYCLNG SYMBOL */
160 [GLYPH_DOWNLOAD] = UTF8("⤵️"), /* actually called: RIGHT ARROW CURVING DOWN */
161 [GLYPH_SPARKLES] = UTF8("✨"),
162 [GLYPH_LOW_BATTERY] = UTF8("🪫"),
163 [GLYPH_WARNING_SIGN] = UTF8("⚠️"),
164 [GLYPH_COMPUTER_DISK] = UTF8("💽"),
165 [GLYPH_WORLD] = UTF8("🌍"),
166 [GLYPH_RED_CIRCLE] = UTF8("🔴"),
167 [GLYPH_ORANGE_CIRCLE] = UTF8("🟠"),
168 [GLYPH_YELLOW_CIRCLE] = UTF8("🟡"),
169 [GLYPH_BLUE_CIRCLE] = UTF8("🔵"),
170 [GLYPH_GREEN_CIRCLE] = UTF8("🟢"),
171 [GLYPH_SUPERHERO] = UTF8("🦸"),
172 [GLYPH_PUMPKIN] = UTF8("🎃"),
173 [GLYPH_IDCARD] = UTF8("🪪"),
174 [GLYPH_HOME] = UTF8("🏠"),
175 [GLYPH_ROCKET] = UTF8("🚀"),
176 [GLYPH_BROOM] = UTF8("🧹"),
177 [GLYPH_KEYBOARD] = UTF8("⌨️"),
178 [GLYPH_CLOCK] = UTF8("🕗"),
179 [GLYPH_LABEL] = UTF8("🏷️"),
180 [GLYPH_SHELL] = UTF8("🐚"),
181 },
182 };
183
184 if (code < 0)
185 return NULL;
186
187 assert(code < _GLYPH_MAX);
188 return draw_table[force_utf || (code >= _GLYPH_FIRST_EMOJI ? emoji_enabled() : is_locale_utf8())][code];
189 }