From 923b945be41739c109b5452567a521fa03c44498 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 6 May 2022 01:01:53 -0700 Subject: web: simplify buffering of display data --- web/web_prompt.c | 58 ++++++++++++++------------------------------------------ 1 file changed, 14 insertions(+), 44 deletions(-) (limited to 'web/web_prompt.c') diff --git a/web/web_prompt.c b/web/web_prompt.c index 0278817..2ceb2d4 100644 --- a/web/web_prompt.c +++ b/web/web_prompt.c @@ -2,6 +2,7 @@ #include "web_dom.h" #include "web_prompt.h" #include "web_match.h" +#include "web_misc.h" #include "web_scoreboard.h" #include @@ -12,14 +13,12 @@ int oi_timeout; -struct prompt_state { - char *msgl, *input, *msgr; -}; - -struct prompt_state prompt_buffered, prompt_flushed; - enum prompt_mode pm; +static struct { + bufstr msgl, msgr, input; +} bufs; + void oi() { dom_add_class("#oi", "visible"); @@ -37,58 +36,29 @@ void clear_oi() oi_timeout = 0; } -static inline void buffer_str(char *str, char **buffer, char *flushed) -{ - if (str == *buffer || (str && *buffer && !strcmp(str, *buffer))) - return; - - if (*buffer && *buffer != flushed) - free(*buffer); - - *buffer = str ? strdup(str) : NULL; -} - void prompt_set_msgl(char *str) { - buffer_str(str, &prompt_buffered.msgl, prompt_flushed.msgl); + bufstr_buf(&bufs.msgl, str); } void prompt_set_msgr(char *str) { - buffer_str(str, &prompt_buffered.msgr, prompt_flushed.msgr); + bufstr_buf(&bufs.msgr, str); } void prompt_set_input(char *str) { - buffer_str(str, &prompt_buffered.input, prompt_flushed.input); -} - -static inline bool buffered_str_changed(char *buffered, char *flushed) -{ - return (!buffered != !flushed) || - (buffered && flushed && strcmp(buffered, flushed)); -} - -static inline void free_flushed_str(char *flushed, char *buffered) -{ - if (flushed && flushed != buffered) - free(flushed); + bufstr_buf(&bufs.input, str); } void prompt_flush() { - if (buffered_str_changed(prompt_buffered.msgl, prompt_flushed.msgl)) - dom_set_content("#prompt-msg-l", prompt_buffered.msgl); - if (buffered_str_changed(prompt_buffered.msgr, prompt_flushed.msgr)) - dom_set_content("#prompt-msg-r", prompt_buffered.msgr); - if (buffered_str_changed(prompt_buffered.input, prompt_flushed.input)) - dom_set_content("#prompt-input", prompt_buffered.input); - - free_flushed_str(prompt_flushed.msgl, prompt_buffered.msgl); - free_flushed_str(prompt_flushed.msgr, prompt_buffered.msgr); - free_flushed_str(prompt_flushed.input, prompt_buffered.input); - - prompt_flushed = prompt_buffered; + if (bufstr_changed(&bufs.msgl)) + dom_set_content("#prompt-msg-l", bufstr_flush(&bufs.msgl)); + if (bufstr_changed(&bufs.msgr)) + dom_set_content("#prompt-msg-r", bufstr_flush(&bufs.msgr)); + if (bufstr_changed(&bufs.input)) + dom_set_content("#prompt-input", bufstr_flush(&bufs.input)); } void set_prompt_mode(enum prompt_mode mode) -- cgit v1.2.3-70-g09d2