diff options
author | pommicket <pommicket@gmail.com> | 2023-01-03 12:25:58 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-01-03 12:26:09 -0500 |
commit | 1c74c64c6429ebc3720e519cfb155b0a914e1c24 (patch) | |
tree | 75a4ccb9cfdb7e023ed19aa45b3f791d9a1dc9be /ted.c | |
parent | 7428d8a59298d6a7e45cfdbfa937a770393aaa1c (diff) |
different types of messages
Diffstat (limited to 'ted.c')
-rw-r--r-- | ted.c | 91 |
1 files changed, 71 insertions, 20 deletions
@@ -16,10 +16,48 @@ void die(const char *fmt, ...) { exit(EXIT_FAILURE); } +static void ted_vset_message(Ted *ted, MessageType type, const char *fmt, va_list args) { + char message[sizeof ted->message] = {0}; + vsnprintf(message, sizeof message - 1, fmt, args); + + // output error to log file + char tstr[256]; + time_t t = time(NULL); + struct tm *tm = localtime(&t); + strftime(tstr, sizeof tstr, "%Y-%m-%d %H:%M:%S", tm); + ted_log(ted, "[ERROR %s] %s\n", tstr, message); + + if (type >= ted->message_type) { + ted->message_type = type; + strbuf_cpy(ted->message, message); + } +} + +void ted_set_message(Ted *ted, MessageType type, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + ted_vset_message(ted, type, fmt, args); + va_end(args); +} + void ted_seterr(Ted *ted, const char *fmt, ...) { va_list args; va_start(args, fmt); - vsnprintf(ted->error, sizeof ted->error - 1, fmt, args); + ted_vset_message(ted, MESSAGE_ERROR, fmt, args); + va_end(args); +} + +void ted_warn(Ted *ted, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + ted_vset_message(ted, MESSAGE_WARNING, fmt, args); + va_end(args); +} + +void ted_info(Ted *ted, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + ted_vset_message(ted, MESSAGE_INFO, fmt, args); va_end(args); } @@ -34,24 +72,9 @@ void ted_log(Ted *ted, const char *fmt, ...) { void ted_seterr_to_buferr(Ted *ted, TextBuffer *buffer) { - size_t size = sizeof ted->error; - if (sizeof buffer->error < size) size = sizeof buffer->error; - memcpy(ted->error, buffer->error, size); -} - -bool ted_haserr(Ted *ted) { - return ted->error[0] != '\0'; + ted_seterr(ted, "%s", buffer->error); } -const char *ted_geterr(Ted *ted) { - return ted->error; -} - -void ted_clearerr(Ted *ted) { - ted->error[0] = '\0'; -} - - void ted_out_of_mem(Ted *ted) { ted_seterr(ted, "Out of memory."); } @@ -235,11 +258,10 @@ static void ted_load_font(Ted *ted, const char *filename, Font **out) { } *out = font; } else { - ted_seterr(ted, "Couldn't load font: %s", text_get_err()); - text_clear_err(); + die("Couldn't load font: %s", text_get_err()); } } else { - ted_seterr(ted, "Couldn't find font file. There is probably a problem with your ted installation."); + die("Couldn't find font file. There is probably a problem with your ted installation."); } } @@ -624,3 +646,32 @@ void ted_check_for_node_problems(Ted *ted) { } } } + +MessageType ted_message_type_from_lsp(LSPWindowMessageType type) { + switch (type) { + case LSP_WINDOW_MESSAGE_ERROR: return MESSAGE_ERROR; + case LSP_WINDOW_MESSAGE_WARNING: return MESSAGE_WARNING; + case LSP_WINDOW_MESSAGE_INFO: + case LSP_WINDOW_MESSAGE_LOG: + return MESSAGE_INFO; + } + assert(0); + return MESSAGE_ERROR; +} + +void ted_color_settings_for_message_type(MessageType type, ColorSetting *bg_color, ColorSetting *border_color) { + switch (type) { + case MESSAGE_ERROR: + *bg_color = COLOR_ERROR_BG; + *border_color = COLOR_ERROR_BORDER; + break; + case MESSAGE_WARNING: + *bg_color = COLOR_WARNING_BG; + *border_color = COLOR_WARNING_BORDER; + break; + case MESSAGE_INFO: + *bg_color = COLOR_INFO_BG; + *border_color = COLOR_INFO_BORDER; + break; + } +} |