The other day I saw this toot by Stargirl, and it caught my eye:
Lwan, when built in debug mode, will print the thread id (obtained by gettid(2) on Linux) for every log message, so I decided to give this idea a try, by porting the code to C. Here's what I came up with:
static const char *get_thread_emoji(void)
{
static const char *emojis[] = {
"ðķ", "ðą", "ð", "ðđ", "ð°", "ðĶ", "ðŧ", "ðž", "ðĻ", "ðŊ", "ðĶ", "ðŪ",
"ð·", "ð―", "ðļ", "ðĩ", "ð", "ð§", "ðĶ", "ðĪ", "ðĶ", "ðĶ", "ðĶ", "ðš",
"ð", "ðī", "ðĶ", "ð", "ðŠą", "ð", "ðĶ", "ð", "ð", "ð", "ðŠ°", "ðŠē",
"ðŠģ", "ðĶ", "ðĶ", "ðĶ", "ðĒ", "ð", "ðĶ", "ðĶ", "ðĶ", "ð", "ðĶ", "ðĶ",
"ðĶ", "ðĶ", "ðĄ", "ð ", "ð", "ðŽ", "ðģ", "ð", "ðĶ", "ðĶ", "ð", "ð
",
"ð", "ðĶ", "ðĶ", "ðĶ§", "ðĶĢ", "ð", "ðĶ", "ðĶ", "ðŠ", "ðŦ", "ðĶ", "ðĶ",
"ðĶŽ", "ð", "ð", "ð", "ð", "ð", "ð", "ð", "ðĶ", "ð", "ðĶ", "ð",
"ðĐ", "ð", "ð", "ðĶ", "ðĶĪ", "ðĶ", "ðĶ", "ðĶĒ", "ðĶĐ", "ð", "ð", "ðĶ",
"ðĶĻ", "ðĶĄ", "ðĶŦ", "ðĶĶ", "ðĶĨ", "ð", "ð", "ðŋ", "ðĶ", "ð", "ðē",
};
static __thread const char *emoji;
static unsigned int last_emoji_id;
if (!emoji) {
emoji = emojis[ATOMIC_INC(last_emoji_id) % (int)N_ELEMENTS(emojis)];
}
return emoji;
}
This, of course, is only called when $LANG ends with the string .utf8, and the terminal is set to use colors, as determined by this function:
static bool can_use_colors(void)
{
const char *term;
const char *no_color;
if (!isatty(fileno(stdout)))
return false;
/* From https://no-color.org: "Command-line software which adds ANSI
* color to its output by default should check for a NO_COLOR
* environment variable that, when present and not an empty string
* (regardless of its value), prevents the addition of ANSI color." */
no_color = secure_getenv("NO_COLOR");
if (no_color && no_color[0])
return false;
term = secure_getenv("TERM");
if (term && streq(term, "dumb"))
return false;
return true;
}
While not a 1:1 port from the original Python code, the end result is just as good. Don't take my word for it, though; here's how it looks like:
Copyright ÂĐ 2024 L. A. F. Pereira