Skip to content

Commit

Permalink
Print terminal query information if --verbose is enabled.
Browse files Browse the repository at this point in the history
This can help figuring out details while attempting to detect new
terminals.

Issues: #145
  • Loading branch information
hzeller committed Jan 4, 2025
1 parent 83ba403 commit c0bd6d7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
36 changes: 33 additions & 3 deletions src/term-query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/term-query.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/timg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c0bd6d7

Please sign in to comment.