1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 17:23:15 +02:00

core: auto-detect terminal background and apply light theme on first start

Detect the terminal background (via COLORFGBG or an OSC 11 query) before
GUI init and, on the first start, automatically apply the built-in
"light" theme when a light terminal is detected.

The function gui_term_theme_is_light returns an int (1 if light, 0 otherwise,
0 being the safe value when detection is unsure).
This commit is contained in:
Sébastien Helleu
2026-07-04 21:28:55 +02:00
parent ae9e123b6f
commit 08280f472d
22 changed files with 900 additions and 270 deletions
+42 -15
View File
@@ -135,10 +135,13 @@ int weechat_auto_load_scripts = 1; /* auto-load scripts */
/*
* Display WeeChat startup message.
*
* If "term_theme_light" is set, a notice about the automatic "light" theme is
* displayed on first start.
*/
void
weechat_startup_message (void)
weechat_startup_message (int term_theme_light)
{
if (weechat_headless)
{
@@ -183,22 +186,34 @@ weechat_startup_message (void)
if (weechat_first_start)
{
/* message on first run (when weechat.conf is created) */
gui_chat_printf (NULL, "");
gui_chat_printf (NULL, _("Welcome to WeeChat!"));
gui_chat_printf (
NULL,
_("Welcome to WeeChat!\n"
"\n"
"If you are discovering WeeChat, it is recommended to read at "
"least the quickstart guide, and the user's guide if you have "
"some time; they explain main WeeChat concepts.\n"
"All WeeChat docs are available at: https://weechat.org/doc/\n"
"\n"
"Moreover, there is inline help with /help on all commands and "
"options (use Tab key to complete the name).\n"
"The command /fset can help to customize WeeChat.\n"
"\n"
"You can add and connect to an IRC server with /server and "
_("If you are discovering WeeChat, it is recommended to "
"read at least the quickstart guide, and the user's guide if "
"you have some time; they explain main WeeChat concepts."));
gui_chat_printf (
NULL,
_("All WeeChat docs are available at: %s"),
WEECHAT_WEBSITE_DOC);
gui_chat_printf (
NULL,
_("Moreover, there is inline help with /help on all commands and "
"options (use Tab key to complete the name)."));
gui_chat_printf (
NULL,
_("The command /fset can help to customize WeeChat."));
gui_chat_printf (
NULL,
_("You can add and connect to an IRC server with /server and "
"/connect commands (see /help server)."));
if (term_theme_light)
{
gui_chat_printf (
NULL,
_("The \"light\" theme will be automatically applied. "
"Use /theme reset to switch back to the default dark theme."));
}
gui_chat_printf (NULL, "");
gui_chat_printf (NULL, "---");
gui_chat_printf (NULL, "");
@@ -361,6 +376,8 @@ weechat_init_gettext (void)
void
weechat_init (int argc, char *argv[], void (*gui_init_cb)(void))
{
int term_theme_light; /* 1 if a light terminal detected */
weechat_first_start_time = time (NULL); /* initialize start time */
gettimeofday (&weechat_current_start_timeval, NULL);
@@ -369,6 +386,9 @@ weechat_init (int argc, char *argv[], void (*gui_init_cb)(void))
* weechat_current_start_timeval.tv_usec)
^ getpid ());
/* detect the terminal theme, before initializing the GUI */
term_theme_light = gui_term_theme_is_light ();
weeurl_init (); /* initialize URL */
string_init (); /* initialize string */
signal_init (); /* initialize signals */
@@ -408,7 +428,7 @@ weechat_init (int argc, char *argv[], void (*gui_init_cb)(void))
weechat_upgrading = 0;
}
if (!weechat_doc_gen)
weechat_startup_message (); /* display WeeChat startup message */
weechat_startup_message (term_theme_light); /* startup message */
gui_chat_print_lines_waiting_buffer (NULL); /* display lines waiting */
weechat_term_check (); /* warning about wrong $TERM */
weechat_locale_check (); /* warning about wrong locale */
@@ -426,6 +446,13 @@ weechat_init (int argc, char *argv[], void (*gui_init_cb)(void))
weechat_doc_gen_ok = doc_generate (weechat_doc_gen_path);
weechat_quit = 1;
}
if (weechat_first_start && isatty (STDOUT_FILENO) && !weechat_headless && !weechat_doc_gen)
{
/* switch to "light" theme if terminal background was detected as "light" */
if (term_theme_light)
theme_apply ("light");
}
}
/*
+1
View File
@@ -55,6 +55,7 @@
#define WEECHAT_COPYRIGHT_DATE "(C) 2003-2026"
#define WEECHAT_WEBSITE "https://weechat.org/"
#define WEECHAT_WEBSITE_DOC "https://weechat.org/doc/"
#define WEECHAT_WEBSITE_DOWNLOAD "https://weechat.org/download/"
#define WEECHAT_AUTHOR_NAME "Sébastien Helleu"
#define WEECHAT_AUTHOR_EMAIL "flashcode@flashtux.org"
+146
View File
@@ -25,6 +25,14 @@
#include "config.h"
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#ifndef WEECHAT_HEADLESS
#ifdef HAVE_NCURSESW_CURSES_H
#ifdef __sun
@@ -37,6 +45,8 @@
#endif /* HAVE_NCURSESW_CURSES_H */
#endif /* WEECHAT_HEADLESS */
#include "../../core/weechat.h"
/*
* Set "eat_newline_glitch" variable.
@@ -56,3 +66,139 @@ gui_term_set_eat_newline_glitch (int value)
(void) value;
#endif
}
/*
* Auto-detects the terminal background as "light" or "dark".
*
* Best-effort, in order:
*
* 1. Environment variable COLORFGBG (set by rxvt, urxvt, Konsole,
* ...): "fg;bg" or "fg;default;bg"; the last ';'-separated
* component is the bg ANSI color index. 0-6 + 8 are dark,
* 7 + 9-15 are light.
*
* 2. OSC 11 escape query on /dev/tty: write "\033]11;?\033\\",
* wait up to 100 ms for a reply of the form
* "...rgb:RRRR/GGGG/BBBB..." (each component is 1-4 hex digits
* depending on the terminal). Classify the answer by Rec. 601
* luminance computed on the high nibble of each component
* (resolution is plenty for a light/dark binary decision and
* avoids width-normalization headaches).
*
* /dev/tty is used rather than stdin/stdout so a pipe redirection
* does not defeat detection. The select() timeout caps how long a
* silent terminal can stall startup. Headless mode and any I/O error
* short-circuit to the safe default 0 (dark), which is also returned
* when both probes are inconclusive.
*
* MUST be called before curses init: it briefly puts the tty in raw
* mode and reads/writes escape sequences directly.
*
* Returns 1 if a light background is detected, otherwise 0 (0 is the
* preferred value when detection is unsure).
*/
int
gui_term_theme_is_light (void)
{
const char *colorfgbg, *p;
char *endptr;
long bg;
int fd, n, i, len;
struct termios old_attr, new_attr;
fd_set rfds;
struct timeval tv;
char buf[256], *q, *qend;
unsigned long comp_high[3], luminance;
if (weechat_headless)
return 0;
/* 1. COLORFGBG */
colorfgbg = getenv ("COLORFGBG");
if (colorfgbg && colorfgbg[0])
{
p = strrchr (colorfgbg, ';');
if (p)
{
bg = strtol (p + 1, &endptr, 10);
if (endptr != p + 1 && *endptr == '\0')
{
if (bg == 7 || (bg >= 9 && bg <= 15))
return 1;
if ((bg >= 0 && bg <= 6) || bg == 8)
return 0;
}
}
}
/* 2. OSC 11 query on the controlling terminal */
fd = open ("/dev/tty", O_RDWR | O_NOCTTY);
if (fd < 0)
return 0;
if (tcgetattr (fd, &old_attr) != 0)
{
close (fd);
return 0;
}
new_attr = old_attr;
new_attr.c_lflag &= (tcflag_t) ~(ICANON | ECHO);
new_attr.c_cc[VMIN] = 0;
new_attr.c_cc[VTIME] = 0;
if (tcsetattr (fd, TCSANOW, &new_attr) != 0)
{
close (fd);
return 0;
}
n = -1;
if (write (fd, "\033]11;?\033\\", 8) == 8)
{
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 100000; /* 100 ms cap on terminals that won't reply */
if (select (fd + 1, &rfds, NULL, NULL, &tv) > 0)
n = read (fd, buf, sizeof (buf) - 1);
}
tcsetattr (fd, TCSANOW, &old_attr);
close (fd);
if (n <= 0)
return 0;
buf[n] = '\0';
q = strstr (buf, "rgb:");
if (!q)
return 0;
q += 4;
for (i = 0; i < 3; i++)
{
qend = q;
while ((*qend >= '0' && *qend <= '9')
|| (*qend >= 'a' && *qend <= 'f')
|| (*qend >= 'A' && *qend <= 'F'))
qend++;
len = (int)(qend - q);
if (len < 1 || len > 4)
return 0;
/* high nibble of this component: width-independent brightness */
comp_high[i] = strtoul (q, NULL, 16) >> ((len - 1) * 4);
q = qend;
if (i < 2)
{
if (*q != '/')
return 0;
q++;
}
}
/* Rec. 601 with coefficients summing to 1000; max = 15 * 1000 */
luminance = (299UL * comp_high[0]
+ 587UL * comp_high[1]
+ 114UL * comp_high[2]);
return (luminance > 7500) ? 1 : 0;
}
+1
View File
@@ -31,5 +31,6 @@ extern void gui_main_end (int clean_exit);
/* terminal functions (GUI dependent) */
extern void gui_term_set_eat_newline_glitch (int value);
extern int gui_term_theme_is_light (void);
#endif /* WEECHAT_GUI_MAIN_H */