zpmod  b19981f
High-performance Zsh module for script optimization and filesystem helpers
emoji.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
6 /* Canonical module header ordering */
7 #include "zpmod.mdh"
8 #include "zpmod.pro"
9 #include "zpmod_vendor_shims.h"
10 /* System headers after gateway */
11 #include <locale.h>
12 #include <string.h>
13 #include <unistd.h>
14 #if defined(__has_include)
15 #if __has_include(<langinfo.h>)
16 #include <langinfo.h>
17 #define ZPMOD_HAVE_LANGINFO 1
18 #endif
19 #endif
20 #include "zpmod_emoji.h"
21 
22 static int s_cached = -1;
23 
25 int zp_icons_enabled(void) {
26  if (s_cached != -1) {
27  return s_cached;
28  }
29  const char *env = getsparam("ZPMOD_ICONS");
30  if (env) {
31  if (!strcmp(env, "0") || !strcmp(env, "false") || !strcmp(env, "off")) {
32  return (s_cached = 0);
33  }
34  if (!strcmp(env, "1") || !strcmp(env, "true") || !strcmp(env, "on")) {
35  return (s_cached = 1);
36  }
37  }
38  if (!isatty(STDOUT_FILENO)) {
39  return (s_cached = 0);
40  }
41  setlocale(LC_ALL, "");
42 #ifdef ZPMOD_HAVE_LANGINFO
43  const char *cs = nl_langinfo(CODESET);
44  if (cs && (strstr(cs, "UTF-8") || strstr(cs, "utf8") || strstr(cs, "UTF8"))) {
45  return (s_cached = 1);
46  }
47 #else
48  const char *lc = getenv("LC_ALL");
49  if (!lc)
50  lc = getenv("LANG");
51  if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf8") || strstr(lc, "UTF8")))
52  return (s_cached = 1);
53 #endif
54  return (s_cached = 0);
55 }
56 
58 const char *zp_icon(const char *s) { return zp_icons_enabled() ? s : ""; }
const char * zp_icon(const char *s)
Return icon string if enabled, empty string otherwise.
Definition: emoji.c:58
int zp_icons_enabled(void)
Determine if icons should be emitted to stdout.
Definition: emoji.c:25
static int s_cached
Definition: emoji.c:22
Module declaration header (mdh) for zpmod.
Prototype stub for zpmod when building out-of-tree.
Optional terminal/locale detection for emoji support in messages.
char * getsparam(const char *name)
Local, non-invasive shims to suppress benign vendor header warnings.