mirror of
https://github.com/neovim/neovim.git
synced 2025-10-01 07:28:34 +00:00
Merge #9509 'TUI: detect background color'
This commit is contained in:
@@ -83,10 +83,10 @@ EXTERN struct nvim_stats_s {
|
|||||||
int64_t redraw;
|
int64_t redraw;
|
||||||
} g_stats INIT(= { 0, 0 });
|
} g_stats INIT(= { 0, 0 });
|
||||||
|
|
||||||
/* Values for "starting" */
|
// Values for "starting".
|
||||||
#define NO_SCREEN 2 /* no screen updating yet */
|
#define NO_SCREEN 2 // no screen updating yet
|
||||||
#define NO_BUFFERS 1 /* not all buffers loaded yet */
|
#define NO_BUFFERS 1 // not all buffers loaded yet
|
||||||
/* 0 not starting anymore */
|
// 0 not starting anymore
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Number of Rows and Columns in the screen.
|
* Number of Rows and Columns in the screen.
|
||||||
|
@@ -7,8 +7,10 @@
|
|||||||
#include "nvim/api/vim.h"
|
#include "nvim/api/vim.h"
|
||||||
#include "nvim/api/private/helpers.h"
|
#include "nvim/api/private/helpers.h"
|
||||||
#include "nvim/ascii.h"
|
#include "nvim/ascii.h"
|
||||||
|
#include "nvim/charset.h"
|
||||||
#include "nvim/main.h"
|
#include "nvim/main.h"
|
||||||
#include "nvim/aucmd.h"
|
#include "nvim/aucmd.h"
|
||||||
|
#include "nvim/option.h"
|
||||||
#include "nvim/os/os.h"
|
#include "nvim/os/os.h"
|
||||||
#include "nvim/os/input.h"
|
#include "nvim/os/input.h"
|
||||||
#include "nvim/event/rstream.h"
|
#include "nvim/event/rstream.h"
|
||||||
@@ -352,6 +354,85 @@ static bool handle_forced_escape(TermInput *input)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_bg_deferred(void **argv)
|
||||||
|
{
|
||||||
|
char *bgvalue = argv[0];
|
||||||
|
if (starting) {
|
||||||
|
// Wait until after startup, so OptionSet is triggered.
|
||||||
|
loop_schedule(&main_loop, event_create(set_bg_deferred, 1, bgvalue));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!option_was_set("bg") && !strequal((char *)p_bg, bgvalue)) {
|
||||||
|
// Value differs, apply it.
|
||||||
|
set_option_value("bg", 0L, bgvalue, 0);
|
||||||
|
reset_option_was_set("bg");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// During startup, tui.c requests the background color (see `ext.get_bg`).
|
||||||
|
//
|
||||||
|
// Here in input.c, we watch for the terminal response `\e]11;COLOR\a`. If
|
||||||
|
// COLOR matches `rgb:RRRR/GGGG/BBBB` where R, G, and B are hex digits, then
|
||||||
|
// compute the luminance[1] of the RGB color and classify it as light/dark
|
||||||
|
// accordingly. Note that the color components may have anywhere from one to
|
||||||
|
// four hex digits, and require scaling accordingly as values out of 4, 8, 12,
|
||||||
|
// or 16 bits.
|
||||||
|
//
|
||||||
|
// [1] https://en.wikipedia.org/wiki/Luma_%28video%29
|
||||||
|
static bool handle_background_color(TermInput *input)
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
size_t component = 0;
|
||||||
|
uint16_t rgb[] = { 0, 0, 0 };
|
||||||
|
uint16_t rgb_max[] = { 0, 0, 0 };
|
||||||
|
bool eat_backslash = false;
|
||||||
|
bool done = false;
|
||||||
|
bool bad = false;
|
||||||
|
if (rbuffer_size(input->read_stream.buffer) >= 9
|
||||||
|
&& !rbuffer_cmp(input->read_stream.buffer, "\x1b]11;rgb:", 9)) {
|
||||||
|
rbuffer_consumed(input->read_stream.buffer, 9);
|
||||||
|
RBUFFER_EACH(input->read_stream.buffer, c, i) {
|
||||||
|
count = i + 1;
|
||||||
|
if (eat_backslash) {
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
} else if (c == '\x07') {
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
} else if (c == '\x1b') {
|
||||||
|
eat_backslash = true;
|
||||||
|
} else if (bad) {
|
||||||
|
// ignore
|
||||||
|
} else if (c == '/') {
|
||||||
|
if (component < 3) {
|
||||||
|
component++;
|
||||||
|
}
|
||||||
|
} else if (ascii_isxdigit(c)) {
|
||||||
|
if (component < 3 && rgb_max[component] != 0xffff) {
|
||||||
|
rgb_max[component] = (uint16_t)((rgb_max[component] << 4) | 0xf);
|
||||||
|
rgb[component] = (uint16_t)((rgb[component] << 4) | hex2nr(c));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bad = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rbuffer_consumed(input->read_stream.buffer, count);
|
||||||
|
if (done && !bad && rgb_max[0] && rgb_max[1] && rgb_max[2]) {
|
||||||
|
double r = (double)rgb[0] / (double)rgb_max[0];
|
||||||
|
double g = (double)rgb[1] / (double)rgb_max[1];
|
||||||
|
double b = (double)rgb[2] / (double)rgb_max[2];
|
||||||
|
double luminance = (0.299 * r) + (0.587 * g) + (0.114 * b); // CCIR 601
|
||||||
|
char *bgvalue = luminance < 0.5 ? "dark" : "light";
|
||||||
|
DLOG("bg response: %s", bgvalue);
|
||||||
|
loop_schedule(&main_loop, event_create(set_bg_deferred, 1, bgvalue));
|
||||||
|
} else {
|
||||||
|
DLOG("failed to parse bg response");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static void read_cb(Stream *stream, RBuffer *buf, size_t count_, void *data,
|
static void read_cb(Stream *stream, RBuffer *buf, size_t count_, void *data,
|
||||||
bool eof)
|
bool eof)
|
||||||
{
|
{
|
||||||
@@ -381,7 +462,8 @@ static void read_cb(Stream *stream, RBuffer *buf, size_t count_, void *data,
|
|||||||
do {
|
do {
|
||||||
if (handle_focus_event(input)
|
if (handle_focus_event(input)
|
||||||
|| handle_bracketed_paste(input)
|
|| handle_bracketed_paste(input)
|
||||||
|| handle_forced_escape(input)) {
|
|| handle_forced_escape(input)
|
||||||
|
|| handle_background_color(input)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -123,6 +123,7 @@ typedef struct {
|
|||||||
int set_cursor_style, reset_cursor_style;
|
int set_cursor_style, reset_cursor_style;
|
||||||
int save_title, restore_title;
|
int save_title, restore_title;
|
||||||
int enter_undercurl_mode, exit_undercurl_mode, set_underline_color;
|
int enter_undercurl_mode, exit_undercurl_mode, set_underline_color;
|
||||||
|
int get_bg;
|
||||||
} unibi_ext;
|
} unibi_ext;
|
||||||
char *space_buf;
|
char *space_buf;
|
||||||
} TUIData;
|
} TUIData;
|
||||||
@@ -211,6 +212,7 @@ static void terminfo_start(UI *ui)
|
|||||||
data->unibi_ext.reset_scroll_region = -1;
|
data->unibi_ext.reset_scroll_region = -1;
|
||||||
data->unibi_ext.set_cursor_style = -1;
|
data->unibi_ext.set_cursor_style = -1;
|
||||||
data->unibi_ext.reset_cursor_style = -1;
|
data->unibi_ext.reset_cursor_style = -1;
|
||||||
|
data->unibi_ext.get_bg = -1;
|
||||||
data->out_fd = 1;
|
data->out_fd = 1;
|
||||||
data->out_isatty = os_isatty(data->out_fd);
|
data->out_isatty = os_isatty(data->out_fd);
|
||||||
|
|
||||||
@@ -281,6 +283,8 @@ static void terminfo_start(UI *ui)
|
|||||||
unibi_out_ext(ui, data->unibi_ext.save_title);
|
unibi_out_ext(ui, data->unibi_ext.save_title);
|
||||||
unibi_out(ui, unibi_keypad_xmit);
|
unibi_out(ui, unibi_keypad_xmit);
|
||||||
unibi_out(ui, unibi_clear_screen);
|
unibi_out(ui, unibi_clear_screen);
|
||||||
|
// Ask the terminal to send us the background color.
|
||||||
|
unibi_out_ext(ui, data->unibi_ext.get_bg);
|
||||||
// Enable bracketed paste
|
// Enable bracketed paste
|
||||||
unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste);
|
unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste);
|
||||||
|
|
||||||
@@ -1648,6 +1652,9 @@ static void patch_terminfo_bugs(TUIData *data, const char *term,
|
|||||||
#define XTERM_SETAB_16 \
|
#define XTERM_SETAB_16 \
|
||||||
"\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e39%;m"
|
"\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e39%;m"
|
||||||
|
|
||||||
|
data->unibi_ext.get_bg = (int)unibi_add_ext_str(ut, "ext.get_bg",
|
||||||
|
"\x1b]11;?\x07");
|
||||||
|
|
||||||
// Terminals with 256-colour SGR support despite what terminfo says.
|
// Terminals with 256-colour SGR support despite what terminfo says.
|
||||||
if (unibi_get_num(ut, unibi_max_colors) < 256) {
|
if (unibi_get_num(ut, unibi_max_colors) < 256) {
|
||||||
// See http://fedoraproject.org/wiki/Features/256_Color_Terminals
|
// See http://fedoraproject.org/wiki/Features/256_Color_Terminals
|
||||||
|
@@ -269,7 +269,7 @@ describe('TUI', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('tui with non-tty file descriptors', function()
|
describe('TUI with non-tty file descriptors', function()
|
||||||
before_each(helpers.clear)
|
before_each(helpers.clear)
|
||||||
|
|
||||||
after_each(function()
|
after_each(function()
|
||||||
@@ -277,7 +277,7 @@ describe('tui with non-tty file descriptors', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it('can handle pipes as stdout and stderr', function()
|
it('can handle pipes as stdout and stderr', function()
|
||||||
local screen = thelpers.screen_setup(0, '"'..helpers.nvim_prog
|
local screen = thelpers.screen_setup(0, '"'..nvim_prog
|
||||||
..' -u NONE -i NONE --cmd \'set noswapfile noshowcmd noruler\' --cmd \'normal iabc\' > /dev/null 2>&1 && cat testF && rm testF"')
|
..' -u NONE -i NONE --cmd \'set noswapfile noshowcmd noruler\' --cmd \'normal iabc\' > /dev/null 2>&1 && cat testF && rm testF"')
|
||||||
feed_data(':w testF\n:q\n')
|
feed_data(':w testF\n:q\n')
|
||||||
screen:expect([[
|
screen:expect([[
|
||||||
@@ -292,12 +292,12 @@ describe('tui with non-tty file descriptors', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('tui FocusGained/FocusLost', function()
|
describe('TUI FocusGained/FocusLost', function()
|
||||||
local screen
|
local screen
|
||||||
|
|
||||||
before_each(function()
|
before_each(function()
|
||||||
helpers.clear()
|
helpers.clear()
|
||||||
screen = thelpers.screen_setup(0, '["'..helpers.nvim_prog
|
screen = thelpers.screen_setup(0, '["'..nvim_prog
|
||||||
..'", "-u", "NONE", "-i", "NONE", "--cmd", "set noswapfile noshowcmd noruler"]')
|
..'", "-u", "NONE", "-i", "NONE", "--cmd", "set noswapfile noshowcmd noruler"]')
|
||||||
feed_data(":autocmd FocusGained * echo 'gained'\n")
|
feed_data(":autocmd FocusGained * echo 'gained'\n")
|
||||||
feed_data(":autocmd FocusLost * echo 'lost'\n")
|
feed_data(":autocmd FocusLost * echo 'lost'\n")
|
||||||
@@ -459,7 +459,7 @@ end)
|
|||||||
|
|
||||||
-- These tests require `thelpers` because --headless/--embed
|
-- These tests require `thelpers` because --headless/--embed
|
||||||
-- does not initialize the TUI.
|
-- does not initialize the TUI.
|
||||||
describe("tui 't_Co' (terminal colors)", function()
|
describe("TUI 't_Co' (terminal colors)", function()
|
||||||
local screen
|
local screen
|
||||||
local is_freebsd = (string.lower(uname()) == 'freebsd')
|
local is_freebsd = (string.lower(uname()) == 'freebsd')
|
||||||
|
|
||||||
@@ -731,7 +731,7 @@ end)
|
|||||||
|
|
||||||
-- These tests require `thelpers` because --headless/--embed
|
-- These tests require `thelpers` because --headless/--embed
|
||||||
-- does not initialize the TUI.
|
-- does not initialize the TUI.
|
||||||
describe("tui 'term' option", function()
|
describe("TUI 'term' option", function()
|
||||||
local screen
|
local screen
|
||||||
local is_bsd = not not string.find(string.lower(uname()), 'bsd')
|
local is_bsd = not not string.find(string.lower(uname()), 'bsd')
|
||||||
local is_macos = not not string.find(string.lower(uname()), 'darwin')
|
local is_macos = not not string.find(string.lower(uname()), 'darwin')
|
||||||
@@ -783,7 +783,7 @@ end)
|
|||||||
|
|
||||||
-- These tests require `thelpers` because --headless/--embed
|
-- These tests require `thelpers` because --headless/--embed
|
||||||
-- does not initialize the TUI.
|
-- does not initialize the TUI.
|
||||||
describe("tui", function()
|
describe("TUI", function()
|
||||||
local screen
|
local screen
|
||||||
local logfile = 'Xtest_tui_verbose_log'
|
local logfile = 'Xtest_tui_verbose_log'
|
||||||
after_each(function()
|
after_each(function()
|
||||||
@@ -826,3 +826,89 @@ describe("tui", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('TUI background color', function()
|
||||||
|
local screen
|
||||||
|
|
||||||
|
before_each(function()
|
||||||
|
clear()
|
||||||
|
screen = thelpers.screen_setup(0, '["'..nvim_prog
|
||||||
|
..'", "-u", "NONE", "-i", "NONE", "--cmd", "set noswapfile"]')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("triggers OptionSet event on terminal-response", function()
|
||||||
|
feed_data('\027:autocmd OptionSet background echo "did OptionSet, yay!"\n')
|
||||||
|
|
||||||
|
-- The child Nvim is running asynchronously; wait for it to register the
|
||||||
|
-- OptionSet handler.
|
||||||
|
feed_data('\027:autocmd OptionSet\n')
|
||||||
|
screen:expect({any='--- Autocommands ---'})
|
||||||
|
|
||||||
|
feed_data('\012') -- CTRL-L: clear the screen
|
||||||
|
screen:expect([[
|
||||||
|
{1: } |
|
||||||
|
{4:~ }|
|
||||||
|
{4:~ }|
|
||||||
|
{4:~ }|
|
||||||
|
{5:[No Name] 0,0-1 All}|
|
||||||
|
|
|
||||||
|
{3:-- TERMINAL --} |
|
||||||
|
]])
|
||||||
|
feed_data('\027]11;rgb:ffff/ffff/ffff\007')
|
||||||
|
screen:expect{any='did OptionSet, yay!'}
|
||||||
|
end)
|
||||||
|
|
||||||
|
local function assert_bg(color, bg)
|
||||||
|
it('handles '..color..' as '..bg, function()
|
||||||
|
feed_data('\027]11;rgb:'..color..'\007:echo &background\n')
|
||||||
|
screen:expect(string.format([[
|
||||||
|
{1: } |
|
||||||
|
{4:~ }|
|
||||||
|
{4:~ }|
|
||||||
|
{4:~ }|
|
||||||
|
{5:[No Name] 0,0-1 All}|
|
||||||
|
%-5s |
|
||||||
|
{3:-- TERMINAL --} |
|
||||||
|
]], bg))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_bg('0000/0000/0000', 'dark')
|
||||||
|
assert_bg('ffff/ffff/ffff', 'light')
|
||||||
|
assert_bg('000/000/000', 'dark')
|
||||||
|
assert_bg('fff/fff/fff', 'light')
|
||||||
|
assert_bg('00/00/00', 'dark')
|
||||||
|
assert_bg('ff/ff/ff', 'light')
|
||||||
|
assert_bg('0/0/0', 'dark')
|
||||||
|
assert_bg('f/f/f', 'light')
|
||||||
|
|
||||||
|
assert_bg('f/0/0', 'dark')
|
||||||
|
assert_bg('0/f/0', 'light')
|
||||||
|
assert_bg('0/0/f', 'dark')
|
||||||
|
|
||||||
|
assert_bg('1/1/1', 'dark')
|
||||||
|
assert_bg('2/2/2', 'dark')
|
||||||
|
assert_bg('3/3/3', 'dark')
|
||||||
|
assert_bg('4/4/4', 'dark')
|
||||||
|
assert_bg('5/5/5', 'dark')
|
||||||
|
assert_bg('6/6/6', 'dark')
|
||||||
|
assert_bg('7/7/7', 'dark')
|
||||||
|
assert_bg('8/8/8', 'light')
|
||||||
|
assert_bg('9/9/9', 'light')
|
||||||
|
assert_bg('a/a/a', 'light')
|
||||||
|
assert_bg('b/b/b', 'light')
|
||||||
|
assert_bg('c/c/c', 'light')
|
||||||
|
assert_bg('d/d/d', 'light')
|
||||||
|
assert_bg('e/e/e', 'light')
|
||||||
|
|
||||||
|
assert_bg('0/e/0', 'light')
|
||||||
|
assert_bg('0/d/0', 'light')
|
||||||
|
assert_bg('0/c/0', 'dark')
|
||||||
|
assert_bg('0/b/0', 'dark')
|
||||||
|
|
||||||
|
assert_bg('f/0/f', 'dark')
|
||||||
|
assert_bg('f/1/f', 'dark')
|
||||||
|
assert_bg('f/2/f', 'dark')
|
||||||
|
assert_bg('f/3/f', 'light')
|
||||||
|
assert_bg('f/4/f', 'light')
|
||||||
|
end)
|
||||||
|
Reference in New Issue
Block a user