feat(ui): restore has('gui_running')

Problem:
has('gui_running') is still common in the wild and our answer has
changed over time, causing frustration.
95a6ccbe9f

Solution:
Use stdin_tty/stdout_tty to decide if a UI is (not) a GUI.
This commit is contained in:
Justin M. Keyes 2023-02-27 16:31:05 +01:00
parent 7f424e2b65
commit ce597235a2
8 changed files with 43 additions and 9 deletions

View File

@ -181,6 +181,9 @@ The following new APIs or features were added.
• |nvim_list_uis()| reports all |ui-option| fields. • |nvim_list_uis()| reports all |ui-option| fields.
• Vim's `has('gui_running')` is now supported as a way for plugins to check if
a GUI (not the |TUI|) is attached to Nvim. |has()|
============================================================================== ==============================================================================
CHANGED FEATURES *news-changes* CHANGED FEATURES *news-changes*

View File

@ -352,7 +352,6 @@ static void ui_set_option(UI *ui, bool init, String name, Object value, Error *e
}); });
stdin_fd = (int)value.data.integer; stdin_fd = (int)value.data.integer;
ui->stdin_fd = (int)value.data.integer;
return; return;
} }

View File

@ -3228,7 +3228,9 @@ static void f_has(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
} }
if (!n) { if (!n) {
if (STRNICMP(name, "patch", 5) == 0) { if (STRNICMP(name, "gui_running", 11) == 0) {
n = ui_gui_attached();
} else if (STRNICMP(name, "patch", 5) == 0) {
if (name[5] == '-' if (name[5] == '-'
&& strlen(name) >= 11 && strlen(name) >= 11
&& ascii_isdigit(name[6]) && ascii_isdigit(name[6])

View File

@ -134,6 +134,7 @@ void ui_free_all_mem(void)
} }
#endif #endif
/// Returns true if any `rgb=true` UI is attached.
bool ui_rgb_attached(void) bool ui_rgb_attached(void)
{ {
if (!headless_mode && p_tgc) { if (!headless_mode && p_tgc) {
@ -147,6 +148,18 @@ bool ui_rgb_attached(void)
return false; return false;
} }
/// Returns true if a GUI is attached.
bool ui_gui_attached(void)
{
for (size_t i = 0; i < ui_count; i++) {
bool tui = uis[i]->stdin_tty || uis[i]->stdout_tty;
if (!tui) {
return true;
}
}
return false;
}
/// Returns true if any UI requested `override=true`. /// Returns true if any UI requested `override=true`.
bool ui_override(void) bool ui_override(void)
{ {
@ -599,11 +612,10 @@ Array ui_array(void)
PUT(info, "rgb", BOOLEAN_OBJ(ui->rgb)); PUT(info, "rgb", BOOLEAN_OBJ(ui->rgb));
PUT(info, "override", BOOLEAN_OBJ(ui->override)); PUT(info, "override", BOOLEAN_OBJ(ui->override));
// TUI fields. // TUI fields. (`stdin_fd` is intentionally omitted.)
PUT(info, "term_name", STRING_OBJ(cstr_to_string(ui->term_name))); PUT(info, "term_name", STRING_OBJ(cstr_to_string(ui->term_name)));
PUT(info, "term_background", STRING_OBJ(cstr_to_string(ui->term_background))); PUT(info, "term_background", STRING_OBJ(cstr_to_string(ui->term_background)));
PUT(info, "term_colors", INTEGER_OBJ(ui->term_colors)); PUT(info, "term_colors", INTEGER_OBJ(ui->term_colors));
PUT(info, "stdin_fd", INTEGER_OBJ(ui->stdin_fd));
PUT(info, "stdin_tty", BOOLEAN_OBJ(ui->stdin_tty)); PUT(info, "stdin_tty", BOOLEAN_OBJ(ui->stdin_tty));
PUT(info, "stdout_tty", BOOLEAN_OBJ(ui->stdout_tty)); PUT(info, "stdout_tty", BOOLEAN_OBJ(ui->stdout_tty));

View File

@ -107,7 +107,6 @@ struct ui_t {
char *term_name; char *term_name;
char *term_background; char *term_background;
int term_colors; int term_colors;
int stdin_fd;
bool stdin_tty; bool stdin_tty;
bool stdout_tty; bool stdout_tty;

View File

@ -1399,6 +1399,8 @@ describe('TUI', function()
end) end)
it('in nvim_list_uis()', function() it('in nvim_list_uis()', function()
-- $TERM in :terminal.
local exp_term = is_os('bsd') and 'builtin_xterm' or 'xterm-256color'
local expected = { local expected = {
{ {
chan = 1, chan = 1,
@ -1418,7 +1420,7 @@ describe('TUI', function()
stdout_tty = true, stdout_tty = true,
term_background = '', term_background = '',
term_colors = 256, term_colors = 256,
term_name = 'xterm-256color', -- $TERM in :terminal. term_name = exp_term,
width = 50 width = 50
}, },
} }

View File

@ -1,8 +1,11 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq local Screen = require('test.functional.ui.screen')
local clear = helpers.clear local clear = helpers.clear
local connect = helpers.connect
local eq = helpers.eq
local funcs = helpers.funcs local funcs = helpers.funcs
local is_os = helpers.is_os local is_os = helpers.is_os
local nvim_prog = helpers.nvim_prog
describe('has()', function() describe('has()', function()
before_each(clear) before_each(clear)
@ -69,8 +72,22 @@ describe('has()', function()
end end
end) end)
it('"gui_running"', function()
eq(0, funcs.has('gui_running'))
local tui = Screen.new(50,15)
local gui_session = connect(funcs.serverstart())
local gui = Screen.new(50,15)
eq(0, funcs.has('gui_running'))
tui:attach({ext_linegrid=true, rgb=true, stdin_tty=true, stdout_tty=true})
gui:attach({ext_multigrid=true, rgb=true}, gui_session)
eq(1, funcs.has('gui_running'))
tui:detach()
eq(1, funcs.has('gui_running'))
gui:detach()
eq(0, funcs.has('gui_running'))
end)
it('does not change v:shell_error', function() it('does not change v:shell_error', function()
local nvim_prog = helpers.nvim_prog
funcs.system({nvim_prog, '-es', '+73cquit'}) funcs.system({nvim_prog, '-es', '+73cquit'})
funcs.has('python3') -- use a call whose implementation shells out funcs.has('python3') -- use a call whose implementation shells out
eq(73, funcs.eval('v:shell_error')) eq(73, funcs.eval('v:shell_error'))

View File

@ -312,7 +312,7 @@ function module.is_os(s)
or s == 'bsd') then or s == 'bsd') then
error('unknown platform: '..tostring(s)) error('unknown platform: '..tostring(s))
end end
return ((s == 'win' and module.sysname():find('windows')) return not not ((s == 'win' and module.sysname():find('windows'))
or (s == 'mac' and module.sysname() == 'darwin') or (s == 'mac' and module.sysname() == 'darwin')
or (s == 'freebsd' and module.sysname() == 'freebsd') or (s == 'freebsd' and module.sysname() == 'freebsd')
or (s == 'openbsd' and module.sysname() == 'openbsd') or (s == 'openbsd' and module.sysname() == 'openbsd')