From 23e5e96cc14db94bf4106e0c6d958edec6a8bf92 Mon Sep 17 00:00:00 2001 From: cancel Date: Tue, 28 Jan 2020 13:30:24 +0900 Subject: [PATCH] Shrink definition of brackpaste sequence getch/ungetch Let a boy have his golf, ok. --- tui_main.c | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/tui_main.c b/tui_main.c index c92ffd7..5c2b888 100644 --- a/tui_main.c +++ b/tui_main.c @@ -2437,34 +2437,24 @@ typedef enum { Brackpaste_seq_end, } Brackpaste_seq; +// Try to getch to complete the sequence of a start or end of brackpet paste +// escape sequence. If it doesn't turn out to be one, unwind it back into the +// event queue with ungetch. Yeah this is golfed let me have fun. staticni Brackpaste_seq brackpaste_seq_getungetch(WINDOW *win) { - int esc1 = wgetch(win); - if (esc1 == '[') { - int esc2 = wgetch(win); - if (esc2 == '2') { - int esc3 = wgetch(win); - if (esc3 == '0') { - int esc4 = wgetch(win); - // Start or end of bracketed paste - if (esc4 == '0' || esc4 == '1') { - int esc5 = wgetch(win); - if (esc5 == '~') { - switch (esc4) { - case '0': - return Brackpaste_seq_begin; - case '1': - return Brackpaste_seq_end; - } - } - ungetch(esc5); - } - ungetch(esc4); - } - ungetch(esc3); - } - ungetch(esc2); - } - ungetch(esc1); + int chs[5], n = 0, begorend; // clang-format off + if ((chs[n++] = wgetch(win)) != '[') goto unwind; + if ((chs[n++] = wgetch(win)) != '2') goto unwind; + if ((chs[n++] = wgetch(win)) != '0') goto unwind; + chs[n++] = begorend = wgetch(win); + if (begorend != '0' && begorend != '1') goto unwind; + if ((chs[n++] = wgetch(win)) != '~') goto unwind; + switch (begorend) { + case '0': return Brackpaste_seq_begin; + case '1': return Brackpaste_seq_end; + } // clang-format on +unwind: + while (n > 0) + ungetch(chs[--n]); return Brackpaste_seq_none; }