iomenu

interactive text selection menu
Log | Files | Refs | README | LICENSE

commit 5ac10691e817e4bfb903449a99614a1184d03a7e
parent 8614ad4e2b6c08fcf82366637ee1cdf7662434f4
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Sun,  3 Sep 2017 12:04:49 +0200

more descriptive names

Diffstat:
Miomenu.c | 125++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mutf8.c | 20++++++++++----------
Mutf8.h | 12++++++------
3 files changed, 86 insertions(+), 71 deletions(-)

diff --git a/iomenu.c b/iomenu.c @@ -31,7 +31,7 @@ static int opt[128], rows = 0; static char *prompt = ""; static void -freelines(void) +free_lines(void) { if (linev) { for (; linec > 0; linec--) @@ -47,13 +47,13 @@ die(const char *s) { tcsetattr(ttyfd, TCSANOW, &termios); close(ttyfd); - freelines(); + free_lines(); perror(s); exit(EXIT_FAILURE); } static void -setterminal(void) +set_terminal(void) { struct termios new; @@ -72,7 +72,7 @@ setterminal(void) } static void -resetterminal(void) +reset_terminal(void) { int i; @@ -88,7 +88,7 @@ resetterminal(void) } static void -readlines(void) +read_lines(void) { int size = 0; size_t len; @@ -115,41 +115,53 @@ readlines(void) } while (++linec, ++matchc); } +static size_t +string_width(char *s) +{ + int width = 0; + + while (*s) + if (*s++ == '\t') + width += (width + 7) % 8; + + return width; +} + static char * -format(char *s, int cols) +format(char *str, int cols) { - int i = 0; - long r = 0; - char *f = formatted; - - while (*s && i < cols) { - if (*s == '\t') { - int t = 8 - i % 8; - while (t-- && i < cols) { - *f++ = ' '; - i++; + int col = 0; + long rune = 0; + char *fmt = formatted; + + while (*str && col < cols) { + if (*str == '\t') { + int t = 8 - col % 8; + while (t-- && col < cols) { + *fmt++ = ' '; + col++; } - s++; - - } else if (utf8torune(&r, s) && utf8isprint(r)) { - int j = utf8len(s); - while (j--) - *f++ = *s++; - i++; - - } else { - *f++ = '?'; - i++; - s++; + str++; + + } else if (utf8_to_rune(&rune, str) && rune_is_print(rune)) { + int i = utf8_len(str); + while (i--) + *fmt++ = *str++; + col++; + + } else { + *fmt++ = '?'; + col++; + str++; } } - *f = '\0'; + *fmt = '\0'; return formatted; } static void -printlines(void) +print_lines(void) { int printed = 0, i = current - current % rows; @@ -172,13 +184,13 @@ printlines(void) } static void -printscreen(void) +print_screen(void) { int cols = ws.ws_col - 1; fputs("\r\033[K", stderr); - printlines(); + print_lines(); fprintf(stderr, "\033[%dA\r", rows); if (*prompt) { @@ -194,7 +206,7 @@ printscreen(void) } static int -matchline(char *line, char **tokv, int tokc) +match_line(char *line, char **tokv, int tokc) { if (opt['#'] && line[0] == '#') return 2; @@ -220,10 +232,13 @@ move(signed int sign) } static void -movepg(signed int sign) +move_page(signed int sign) { int i = current - current % rows + rows * sign; + if (!opt['l']) + return; + if (0 > i || i > matchc) return; @@ -255,7 +270,7 @@ filter(void) matchc = 0; for (i = 0; i < linec; i++) - if (matchline(linev[i], tokv, tokc)) + if (match_line(linev[i], tokv, tokc)) matchv[matchc++] = linev[i]; free(tokv); @@ -265,7 +280,7 @@ filter(void) } static void -removeword() +remove_word() { int len = strlen(input) - 1, i; @@ -280,7 +295,7 @@ removeword() } static void -addchar(char key) +add_char(char key) { int len = strlen(input); @@ -293,7 +308,7 @@ addchar(char key) } static void -printselection(void) +print_selection(void) { if (opt['#']) { char **match = matchv + current; @@ -334,7 +349,7 @@ top: break; case CTL('W'): - removeword(); + remove_word(); break; case 127: @@ -357,14 +372,14 @@ top: if (fgetc(stdin) != '~') break; /* FALLTHROUGH */ case ALT('v'): - movepg(-1); + move_page(-1); break; case CSI('6'): /* page down */ if (fgetc(stdin) != '~') break; /* FALLTHROUGH */ case CTL('V'): - movepg(+1); + move_page(+1); break; case CTL('I'): /* tab */ @@ -375,7 +390,7 @@ top: case CTL('J'): /* enter */ case CTL('M'): - printselection(); + print_selection(); return EXIT_SUCCESS; case ALT('['): @@ -387,7 +402,7 @@ top: goto top; default: - addchar((char) key); + add_char((char) key); } return CONTINUE; @@ -400,7 +415,7 @@ sigwinch() die("ioctl"); rows = MIN(opt['l'], ws.ws_row - 1); - printscreen(); + print_screen(); signal(SIGWINCH, sigwinch); } @@ -413,7 +428,7 @@ usage(void) } static void -parseopt(int argc, char *argv[]) +parse_opt(int argc, char *argv[]) { memset(opt, 0, 128 * sizeof (int)); @@ -454,11 +469,11 @@ parseopt(int argc, char *argv[]) int main(int argc, char *argv[]) { - int exitcode; + int exit_code; - parseopt(argc, argv); + parse_opt(argc, argv); - readlines(); + read_lines(); filter(); if (!freopen("/dev/tty", "r", stdin) || @@ -466,17 +481,17 @@ main(int argc, char *argv[]) die("freopen"); ttyfd = open("/dev/tty", O_RDWR); - setterminal(); + set_terminal(); sigwinch(); input[0] = '\0'; - while ((exitcode = key()) == CONTINUE) - printscreen(); - printscreen(); + while ((exit_code = key()) == CONTINUE) + print_screen(); + print_screen(); - resetterminal(); + reset_terminal(); close(ttyfd); - freelines(); + free_lines(); - return exitcode; + return exit_code; } diff --git a/utf8.c b/utf8.c @@ -34,7 +34,7 @@ * or 0 if ti is misencoded. */ size_t -utf8len(char *s) +utf8_len(char *s) { unsigned char *sp = (unsigned char *) s; int i, len = (*sp < 0x80) ? 1 : /* 0xxxxxxx < 10000000 */ @@ -62,7 +62,7 @@ utf8len(char *s) * 0 if rune is too long. */ size_t -utf8runelen(long r) +rune_len(long r) { return (r <= 0x0000007f) ? 1 : (r <= 0x000007ff) ? 2 : (r <= 0x0000ffff) ? 3 : (r <= 0x001fffff) ? 4 : @@ -76,10 +76,10 @@ utf8runelen(long r) * Return the number of bytes read or 0 if the string is misencoded. */ size_t -utf8torune(long *r, char *s) +utf8_to_rune(long *r, char *s) { char mask[] = { 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; - size_t i, len = utf8len(s); + size_t i, len = utf8_len(s); if (len == 0 || len > 6 || (size_t) len > strlen(s)) return 0; @@ -92,7 +92,7 @@ utf8torune(long *r, char *s) *r = (*r << 6) | (*s++ & 0x3f); /* 10xxxxxx */ /* overlong sequences */ - if (utf8runelen(*r) != len) + if (rune_len(*r) != len) return 0; return len; @@ -103,7 +103,7 @@ utf8torune(long *r, char *s) * Returns 1 if the rune is a valid unicode code point and 0 if not. */ int -utf8runeisunicode(long r) +rune_is_unicode(long r) { return !( (r > 0x10ffff) || /* outside range */ @@ -125,14 +125,14 @@ utf8runeisunicode(long r) * code points. */ int -utf8check(char *s) +utf8_check(char *s) { size_t shift, len = strlen(s); long r = 0; while (len > 0) { - shift = utf8torune(&r, s); - if (!shift || !utf8runeisunicode(r)) + shift = utf8_to_rune(&r, s); + if (!shift || !rune_is_unicode(r)) return 0; s += shift; @@ -146,7 +146,7 @@ utf8check(char *s) * Return 1 if the rune is a printable character, and 0 otherwise. */ int -utf8isprint(long r) +rune_is_print(long r) { return (0x1f < r && r != 0x7f && r < 0x80) || 0x9f < r; } diff --git a/utf8.h b/utf8.h @@ -1,6 +1,6 @@ -size_t utf8len(char *); -size_t utf8runelen(long); -size_t utf8torune(long *, char *); -int utf8isunicode(long); -int utf8check(char *); -int utf8isprint(long); +size_t utf8_len(char *); +size_t rune_len(long); +size_t utf8_to_rune(long *, char *); +int utf8_is_unicode(long); +int utf8_check(char *); +int rune_is_print(long);