Add format-string version of tui_abort and use it to display SQLite errors

This commit is contained in:
Daniel Schulte 2020-11-22 18:11:58 +01:00
parent 1f64677fcf
commit 862c56d91e
4 changed files with 29 additions and 2 deletions

View File

@ -30,7 +30,7 @@ size_t current_page_count = 0;
size_t title_offset = 0;
bool any_title_in_next_half = false;
#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { fprintf(stderr, "%s failed: (%d) %s\n", #x, res, sqlite3_errstr(res)); std::abort(); }}
#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { tui_abort("Database error:\n%s failed: (%d) %s", #x, res, sqlite3_errstr(res)); }}
static termpaint_attr* get_attr(const AttributeSetType type, const bool highlight=false)
{

26
tui.cpp
View File

@ -5,6 +5,8 @@
#include <string>
#include <vector>
#include <stdarg.h>
termpaint_integration *integration;
termpaint_terminal *terminal;
termpaint_surface *surface;
@ -666,3 +668,27 @@ bool tui_handle_action(const Event &event, const std::vector<action> &actions)
it->func();
return true;
}
void tui_abort(const char *fmt, ...)
{
va_list ap, ap2;
va_start(ap, fmt);
va_copy(ap2, ap);
int required = vsnprintf(nullptr, 0, fmt, ap);
va_end(ap);
if(required < 0) {
va_end(ap2);
abort();
}
std::vector<char> buffer(required + 1, 0);
int written = vsnprintf(buffer.data(), required + 1, fmt, ap2);
if(written != required) {
va_end(ap2);
abort();
}
const std::string message(buffer.data());
const size_t cols = termpaint_surface_width(surface);
message_box("Error", simple_wrap(message, cols/2));
exit(1);
}

1
tui.h
View File

@ -84,3 +84,4 @@ extern void update_progress(progress_info *info, const int val, const int maxval
extern void end_progress(progress_info *info);
extern void tui_abort(std::string message);
extern void tui_abort(const char *fmt, ...);

2
yt.cpp
View File

@ -6,7 +6,7 @@
#include "tui.h"
#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { fprintf(stderr, "%s failed: (%d) %s\n", #x, res, sqlite3_errstr(res)); std::abort(); }}
#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { tui_abort("Database error:\n%s failed: (%d) %s", #x, res, sqlite3_errstr(res)); }}
using json = nlohmann::json;
struct yt_config yt_config;