1 #define _POSIX_C_SOURCE 200809L
14 #define ERR(str) { perror(str); err = EXIT_FAILURE; goto end; }
18 if (setlocale(LC_ALL, "") == NULL) {
26 int err = EXIT_SUCCESS;
28 int tty_fd = open("/dev/tty", O_RDWR);
34 while ((len = getline(&buf, &siz, stdin)) > 0) {
36 if (ioctl(tty_fd, TIOCGWINSZ, &ws) < 0) ERR("ioctl")
38 int term_width = ws.ws_col;
48 size_t adv = mbrtowc(&wc, ptr, len, &mbs);
49 if (adv == (size_t) -1 || adv == (size_t) -2) ERR("mbrtowc")
51 int width = wcwidth(wc);
55 if (*ptr == '\n' || str_width >= term_width - 12) {
56 for (int i = (term_width - str_width) / 2; i > 0; i--)
58 fwrite(last, 1, ptr - last + 1, stdout);