Merge #9708 from justinmk/tui-bg-detect

This commit is contained in:
Justin M. Keyes 2019-03-11 09:25:39 +01:00 committed by GitHub
commit c0a29c3656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 13 deletions

View File

@ -10,6 +10,7 @@
#include "nvim/charset.h"
#include "nvim/main.h"
#include "nvim/aucmd.h"
#include "nvim/ex_docmd.h"
#include "nvim/option.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"
@ -357,15 +358,17 @@ static bool handle_forced_escape(TermInput *input)
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");
if (starting) {
// Wait until after startup, so OptionSet is triggered.
do_cmdline_cmd((bgvalue[0] == 'l')
? "autocmd VimEnter * once nested set background=light"
: "autocmd VimEnter * once nested set background=dark");
} else {
set_option_value("bg", 0L, bgvalue, 0);
reset_option_was_set("bg");
}
}
}
@ -424,7 +427,8 @@ static bool handle_background_color(TermInput *input)
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));
loop_schedule_deferred(&main_loop,
event_create(set_bg_deferred, 1, bgvalue));
} else {
DLOG("failed to parse bg response");
}

View File

@ -839,8 +839,7 @@ describe('TUI background color', function()
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.
-- Wait for the child Nvim to register the OptionSet handler.
feed_data('\027:autocmd OptionSet\n')
screen:expect({any='--- Autocommands ---'})
@ -860,8 +859,14 @@ describe('TUI background color', function()
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([[
feed_data('\027:autocmd OptionSet background :echo &background\n')
-- Wait for the child Nvim to register the OptionSet handler.
feed_data('\027:autocmd OptionSet\n')
screen:expect({any='--- Autocommands ---'})
feed_data('\012') -- CTRL-L: clear the screen
local expected_grid = [[
{1: } |
{4:~ }|
{4:~ }|
@ -869,7 +874,17 @@ describe('TUI background color', function()
{5:[No Name] 0,0-1 All}|
%-5s |
{3:-- TERMINAL --} |
]], bg))
]]
screen:expect(string.format(expected_grid, ''))
feed_data('\027]11;rgb:'..color..'\007')
-- Because bg=dark is the default, we do NOT expect OptionSet event.
if bg == 'dark' then
screen:expect{unchanged=true,
grid=string.format(expected_grid, '')}
else
screen:expect(string.format(expected_grid, bg))
end
end)
end