From ba0c32e8d84925f88a8af540ce08b2dccbb52c25 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Fri, 27 Nov 2009 15:07:05 +0000 Subject: Allow formatting the mouse position when rendering an event to a string --- demo.c | 5 ++++- termkey.c | 10 +++++++++- termkey.h.in | 2 ++ termkey_snprint_key.3 | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/demo.c b/demo.c index fe0176f..cf2e07c 100644 --- a/demo.c +++ b/demo.c @@ -8,6 +8,7 @@ int main(int argc, char *argv[]) TERMKEY_CHECK_VERSION; int mouse = 0; + TermKeyFormat format = TERMKEY_FORMAT_VIM; char buffer[50]; TermKey *tk; @@ -20,6 +21,8 @@ int main(int argc, char *argv[]) mouse = atoi(optarg); else mouse = 1000; + format |= TERMKEY_FORMAT_MOUSE_POS; + break; default: fprintf(stderr, "Usage: %s [-m]\n", argv[0]); @@ -41,7 +44,7 @@ int main(int argc, char *argv[]) printf("\e[?%dhMouse mode active\n", mouse); while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) { - termkey_snprint_key(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_VIM); + termkey_snprint_key(tk, buffer, sizeof buffer, &key, format); printf("%s\n", buffer); if(key.type == TERMKEY_TYPE_UNICODE && diff --git a/termkey.c b/termkey.c index 1577e74..add7a53 100644 --- a/termkey.c +++ b/termkey.c @@ -1002,12 +1002,20 @@ size_t termkey_snprint_key(TermKey *tk, char *buffer, size_t len, TermKeyKey *ke { TermKeyMouseEvent ev; int button; - termkey_interpret_mouse(tk, key, &ev, &button, NULL, NULL); + int line, col; + termkey_interpret_mouse(tk, key, &ev, &button, &line, &col); static char *evnames[] = { "Unknown", "Press", "Drag", "Release" }; l = snprintf(buffer + pos, len - pos, "Mouse%s(%d)", evnames[ev], button); + + if(format & TERMKEY_FORMAT_MOUSE_POS) { + if(l <= 0) return pos; + pos += l; + + l = snprintf(buffer + pos, len - pos, " @ (%d,%d)", col, line); + } } break; } diff --git a/termkey.h.in b/termkey.h.in index 9ed82c5..0adf112 100644 --- a/termkey.h.in +++ b/termkey.h.in @@ -174,6 +174,8 @@ typedef enum { TERMKEY_FORMAT_CARETCTRL = 1 << 1, // ^X instead of C-X TERMKEY_FORMAT_ALTISMETA = 1 << 2, // Meta- or M- instead of Alt- or A- TERMKEY_FORMAT_WRAPBRACKET = 1 << 3, // Wrap special keys in brackets like + + TERMKEY_FORMAT_MOUSE_POS = 1 << 8, // Include mouse position if relevant; @ col,line } TermKeyFormat; // Some useful combinations diff --git a/termkey_snprint_key.3 b/termkey_snprint_key.3 index 5a66392..8cb41a2 100644 --- a/termkey_snprint_key.3 +++ b/termkey_snprint_key.3 @@ -26,6 +26,9 @@ Use the name "Meta" or the letter "M" instead of "Alt" or "A". .TP .B TERMKEY_FORMAT_WRAPBRACKET If the key event is a special key instead of unmodified Unicode, wrap it in "". +.TP +.B TERMKEY_FORMAT_MOUSE_POS +If the event is a mouse event, include the position rendered as "@ (col,line)". .PP The following shortcuts are provided for common combinations of format bits: .TP -- cgit v1.2.3-70-g09d2