Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit baf93d9

Browse files
committed
UI: always use contrete colors for default_colors_set
But add an escape hatch needed for external TUI, so it still can use terminal emulator defaults.
1 parent 36378c3 commit baf93d9

File tree

8 files changed

+73
-8
lines changed

8 files changed

+73
-8
lines changed

runtime/doc/ui.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ a dictionary with these (optional) keys:
3535
`ext_linegrid` Use new revision of the grid events. |ui-linegrid|
3636
`ext_multigrid` Use per-window grid based events. |ui-multigrid|
3737
`ext_hlstate` Use detailed highlight state. |ui-hlstate|
38+
`ext_termcolors` Use external default colors.
3839

3940
Specifying a non-existent option is an error. UIs can check the |api-metadata|
4041
`ui_options` key for supported options. Additionally Nvim (currently) requires
@@ -239,6 +240,13 @@ numerical highlight `id`:s to the actual attributes.
239240
special colors respectively. `cterm_fg` and `cterm_bg` specifies the
240241
default color codes to use in a 256-color terminal.
241242

243+
The rgb values will always be valid colors, by default. If no
244+
colors have been set, they will default to black and white, depending
245+
on 'background'. By setting the `ext_termcolors` option, instead
246+
-1 will be used for unset colors. This is mostly useful for a
247+
TUI implementation, where using the terminal emulators builitn
248+
defaults are expected.
249+
242250
Note: unlike the corresponding events in the first revision, the
243251
screen is not always cleared after sending this event. The GUI has to
244252
repaint the screen with changed background color itself.

src/nvim/api/ui.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ static void ui_set_option(UI *ui, bool init, String name, Object value,
210210
return;
211211
}
212212
ui->rgb = value.data.boolean;
213-
// A little drastic, but only legacy uis need to use this option
214-
if (!init) {
213+
// A little drastic, but only takes effect for legacy uis. For linegrid UI
214+
// only changes metadata for nvim_list_uis(), no refresh needed.
215+
if (!init && !ui->ui_ext[kUILinegrid]) {
215216
ui_refresh();
216217
}
217218
return;
@@ -355,6 +356,12 @@ static void remote_ui_default_colors_set(UI *ui, Integer rgb_fg,
355356
Integer rgb_bg, Integer rgb_sp,
356357
Integer cterm_fg, Integer cterm_bg)
357358
{
359+
if (!ui->ui_ext[kUITermColors]) {
360+
bool dark = (*p_bg == 'd');
361+
rgb_fg = rgb_fg != -1 ? rgb_fg : (dark ? 0xFFFFFF : 0x000000);
362+
rgb_bg = rgb_bg != -1 ? rgb_bg : (dark ? 0x000000 : 0xFFFFFF);
363+
rgb_sp = rgb_sp != -1 ? rgb_sp : 0xFF0000;
364+
}
358365
Array args = ARRAY_DICT_INIT;
359366
ADD(args, INTEGER_OBJ(rgb_fg));
360367
ADD(args, INTEGER_OBJ(rgb_bg));

src/nvim/ui.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ void ui_set_ext_option(UI *ui, UIExtension ext, bool active)
310310
ui->option_set(ui, cstr_as_string((char *)ui_ext_names[ext]),
311311
BOOLEAN_OBJ(active));
312312
}
313+
if (ext == kUITermColors) {
314+
ui_default_colors_set();
315+
}
313316
}
314317

315318
void ui_line(ScreenGrid *grid, int row, int startcol, int endcol, int clearcol,

src/nvim/ui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ typedef enum {
1818
kUILinegrid,
1919
kUIMultigrid,
2020
kUIHlState,
21+
kUITermColors,
2122
kUIExtCount,
2223
} UIExtension;
2324

@@ -29,6 +30,7 @@ EXTERN const char *ui_ext_names[] INIT(= {
2930
"ext_linegrid",
3031
"ext_multigrid",
3132
"ext_hlstate",
33+
"ext_termcolors",
3234
});
3335

3436

test/functional/api/vim_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ describe('API', function()
12781278
ext_linegrid = screen._options.ext_linegrid or false,
12791279
ext_multigrid = false,
12801280
ext_hlstate=false,
1281+
ext_termcolors=false,
12811282
height = 4,
12821283
rgb = true,
12831284
width = 20,

test/functional/terminal/tui_spec.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,13 @@ describe('TUI', function()
256256
end)
257257

258258
it('shows up in nvim_list_uis', function()
259-
feed_data(':echo map(nvim_list_uis(), {k,v -> sort(items(v))})\013')
259+
feed_data(':echo map(nvim_list_uis(), {k,v -> sort(items(filter(v, {k,v -> k[:3] !=# "ext_" })))})\013')
260260
screen:expect([=[
261-
[[['ext_cmdline', v:false], ['ext_hlstate', v:fals|
262-
e], ['ext_linegrid', v:true], ['ext_multigrid', v:|
263-
false], ['ext_popupmenu', v:false], ['ext_tabline'|
264-
, v:false], ['ext_wildmenu', v:false], ['height', |
265-
6], ['rgb', v:false], ['width', 50]]] |
261+
|
262+
{4:~ }|
263+
{5: }|
264+
[[['height', 6], ['rgb', v:false], ['width', 50]]]|
265+
|
266266
{10:Press ENTER or type command to continue}{1: } |
267267
{3:-- TERMINAL --} |
268268
]=])

test/functional/ui/options_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('ui receives option updates', function()
2727
ext_linegrid=false,
2828
ext_hlstate=false,
2929
ext_multigrid=false,
30+
ext_termcolors=false,
3031
}
3132

3233
clear(...)

test/functional/ui/screen_basic_spec.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,46 @@ end)
960960
describe("Screen (line-based)", function()
961961
screen_tests(true)
962962
end)
963+
964+
describe('Screen default colors', function()
965+
local screen
966+
local function startup(light, termcolors)
967+
local extra = (light and ' background=light') or ''
968+
969+
local nvim_argv = {helpers.nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N',
970+
'--cmd', 'set shortmess+=I noswapfile belloff= noshowcmd noruler'..extra,
971+
'--embed'}
972+
local screen_nvim = spawn(nvim_argv)
973+
set_session(screen_nvim)
974+
screen = Screen.new()
975+
screen:attach(termcolors and {rgb=true,ext_termcolors=true} or {rgb=true})
976+
end
977+
978+
it('are dark per default', function()
979+
startup(false, false)
980+
screen:expect{condition=function()
981+
eq({rgb_bg=0, rgb_fg=Screen.colors.White, rgb_sp=Screen.colors.Red,
982+
cterm_bg=0, cterm_fg=0}, screen.default_colors)
983+
end}
984+
end)
985+
986+
it('can be set to light', function()
987+
startup(true, false)
988+
screen:expect{condition=function()
989+
eq({rgb_bg=Screen.colors.White, rgb_fg=0, rgb_sp=Screen.colors.Red,
990+
cterm_bg=0, cterm_fg=0}, screen.default_colors)
991+
end}
992+
end)
993+
994+
it('can be handled by external terminal', function()
995+
startup(false, true)
996+
screen:expect{condition=function()
997+
eq({rgb_bg=-1, rgb_fg=-1, rgb_sp=-1, cterm_bg=0, cterm_fg=0}, screen.default_colors)
998+
end}
999+
1000+
startup(true, true)
1001+
screen:expect{condition=function()
1002+
eq({rgb_bg=-1, rgb_fg=-1, rgb_sp=-1, cterm_bg=0, cterm_fg=0}, screen.default_colors)
1003+
end}
1004+
end)
1005+
end)

0 commit comments

Comments
 (0)