From c0bd6d700a04ab8eb8dd5efc08efcb2b3f5b8228 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Sat, 4 Jan 2025 09:53:41 +0100 Subject: [PATCH] Print terminal query information if --verbose is enabled. This can help figuring out details while attempting to detect new terminals. Issues: #145 --- src/term-query.cc | 36 +++++++++++++++++++++++++++++++++--- src/term-query.h | 3 +++ src/timg.cc | 4 +++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/term-query.cc b/src/term-query.cc index cf28221..fe36286 100644 --- a/src/term-query.cc +++ b/src/term-query.cc @@ -42,6 +42,27 @@ static void clean_up_terminal() { s_tty_fd = -1; } +// Global variable; ok for debug logging cause. +static bool s_log_terminal_queries = false; +void EnableTerminalQueryLogging(bool on) { s_log_terminal_queries = on; } + +// Debug print a message and c-escaped data. +static void debug_data(FILE *f, const char *msg, const char *data, size_t len) { + fprintf(f, "\033[1m%s\033[0m'", msg); + for (const char *const end = data + len; data < end; ++data) { + if (*data == '\\') { + fprintf(f, "\\\\"); + } + else if (*data < 0x20) { + fprintf(f, "\\%03o", *data); + } + else { + fprintf(f, "%c", *data); + } + } + fprintf(f, "'"); +} + // Send "query" to terminal and wait for response to arrive within // "time_budget". Use "buffer" with "len" to store results. // Whenever new data arrives, the caller's "response_found_p" response finder @@ -97,8 +118,9 @@ static const char *QueryTerminal(const char *query, char *const buffer, const char *found_pos = nullptr; size_t available = buflen - 1; // Allow for nul termination. char *pos = buffer; - timg::Time now = Time::Now(); - const timg::Time deadline = now + time_budget; + const timg::Time start = Time::Now(); + const timg::Time deadline = start + time_budget; + timg::Time now = start; do { struct timeval timeout = (deadline - now).AsTimeval(); fd_set read_fds; @@ -119,7 +141,12 @@ static const char *QueryTerminal(const char *query, char *const buffer, } while (available && now < deadline); clean_up_terminal(); - + if (s_log_terminal_queries) { + debug_data(stderr, "Query: ", query, query_len); + debug_data(stderr, " Response: ", buffer, pos - buffer); + fprintf(stderr, " (%ldms)\n", + (timg::Time() - start).nanoseconds() / 1'000'000); + } return found_pos; } @@ -342,6 +369,9 @@ TermSizeResult DetermineTermSize() { result.font_height_px = w.ws_ypixel / w.ws_row; } else { + if (s_log_terminal_queries) { + fprintf(stderr, "no usable TIOCGWINSZ, trying cell query.\n"); + } // Alright, TIOCGWINSZ did not return the terminal size, let's // see if it reports character cell size otherwise QueryCellWidthHeight(&result.font_width_px, &result.font_height_px); diff --git a/src/term-query.h b/src/term-query.h index db45a3b..c205acf 100644 --- a/src/term-query.h +++ b/src/term-query.h @@ -18,6 +18,9 @@ namespace timg { +// Debugging help. +void EnableTerminalQueryLogging(bool on); + // Determine size of terminal in pixels we can display. struct TermSizeResult { // Not available values will be negative. diff --git a/src/timg.cc b/src/timg.cc index ef5a79a..6c16262 100644 --- a/src/timg.cc +++ b/src/timg.cc @@ -276,7 +276,7 @@ static int usage(const char *progname, ExitCode exit_code, int width, "\t--color8 : Choose 8 bit color mode for -ph or -pq\n" "\t--version : Print detailed version including used libraries.\n" "\t (%s)\n" - "\t--verbose : Print some stats after images shown.\n" + "\t--verbose : Print some terminal query info and stats.\n" "\t-h : Print this help and exit.\n" "\t--help : Page through detailed manpage-like help and exit.\n" @@ -801,6 +801,8 @@ int main(int argc, char *argv[]) { } } + timg::EnableTerminalQueryLogging(verbose); + // -- A sieve of sanity checks and configuration refinement. if (geometry_width < 1 || geometry_height < 1) {