tui: improve handle_background_color: short-circuit (#11067)

* handle_background_color: short-circuit if handled already

* Unit tests for handle_background_color

* set waiting_for_bg_response to false in tui_terminal_after_startup
  By then it should have been received.
This commit is contained in:
Daniel Hahler
2019-09-30 22:00:55 +02:00
committed by GitHub
parent 7e46fcaf23
commit 8a4ae3d664
5 changed files with 165 additions and 70 deletions

View File

@@ -27,6 +27,7 @@ void tinput_init(TermInput *input, Loop *loop)
input->loop = loop;
input->paste = 0;
input->in_fd = 0;
input->waiting_for_bg_response = false;
input->key_buffer = rbuffer_new(KEY_BUFFER_SIZE);
uv_mutex_init(&input->key_buffer_mutex);
uv_cond_init(&input->key_buffer_cond);
@@ -443,6 +444,9 @@ static void set_bg_deferred(void **argv)
// [1] https://en.wikipedia.org/wiki/Luma_%28video%29
static bool handle_background_color(TermInput *input)
{
if (!input->waiting_for_bg_response) {
return false;
}
size_t count = 0;
size_t component = 0;
size_t header_size = 0;
@@ -463,6 +467,7 @@ static bool handle_background_color(TermInput *input)
} else {
return false;
}
input->waiting_for_bg_response = false;
rbuffer_consumed(input->read_stream.buffer, header_size);
RBUFFER_EACH(input->read_stream.buffer, c, i) {
count = i + 1;
@@ -503,6 +508,12 @@ static bool handle_background_color(TermInput *input)
}
return true;
}
#ifdef UNIT_TESTING
bool ut_handle_background_color(TermInput *input)
{
return handle_background_color(input);
}
#endif
static void tinput_read_cb(Stream *stream, RBuffer *buf, size_t count_,
void *data, bool eof)

View File

@@ -12,6 +12,7 @@ typedef struct term_input {
// Phases: -1=all 0=disabled 1=first-chunk 2=continue 3=last-chunk
int8_t paste;
bool waiting;
bool waiting_for_bg_response;
TermKey *tk;
#if TERMKEY_VERSION_MAJOR > 0 || TERMKEY_VERSION_MINOR > 18
TermKey_Terminfo_Getstr_Hook *tk_ti_hook_fn; ///< libtermkey terminfo hook
@@ -28,4 +29,8 @@ typedef struct term_input {
# include "tui/input.h.generated.h"
#endif
#ifdef UNIT_TESTING
bool ut_handle_background_color(TermInput *input);
#endif
#endif // NVIM_TUI_INPUT_H

View File

@@ -296,6 +296,7 @@ static void terminfo_start(UI *ui)
unibi_out(ui, unibi_keypad_xmit);
unibi_out(ui, unibi_clear_screen);
// Ask the terminal to send us the background color.
data->input.waiting_for_bg_response = true;
unibi_out_ext(ui, data->unibi_ext.get_bg);
// Enable bracketed paste
unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste);
@@ -365,6 +366,11 @@ static void tui_terminal_after_startup(UI *ui)
// 2.3 bug(?) which caused slow drawing during startup. #7649
unibi_out_ext(ui, data->unibi_ext.enable_focus_reporting);
flush_buf(ui);
if (data->input.waiting_for_bg_response) {
DLOG("did not get a response for terminal background query");
data->input.waiting_for_bg_response = false;
}
}
static void tui_terminal_stop(UI *ui)