diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-04-14 15:05:02 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-04-14 15:05:02 -0400 |
commit | 3f1715c4339e77c330fdc2e7d8925c6f04543004 (patch) | |
tree | 23534f899ec6132c182be48600ed6f0f41ffe38d | |
parent | c2b7b705f41b7b86f47f22fd4c7a8ecb0a7464d9 (diff) |
-rw-r--r-- | untic.c | 48 |
1 files changed, 39 insertions, 9 deletions
@@ -76,6 +76,37 @@ static const char *ti_strings[] = { }; static const size_t n_strings = sizeof(ti_strings) / sizeof(ti_strings[0]); +int print_entry(int pos, const char *s) +{ + if (pos + (int)strlen(s) + 2 >= COLS) { + printf("\n\t"); + pos = 8; + } else { + printf(" "); + pos++; + } + return pos + printf("%s,", s); +} + +int print_number(int pos, const char *s, int n) +{ + char buf[1023]; + snprintf(buf, sizeof(buf), "%s#%d", s, n); + return print_entry(pos, buf); +} + +int print_string(int pos, const char *s, const char *v) +{ + char buf[1023]; + int bpos = 0; + bpos = snprintf(buf + bpos, sizeof(buf) - bpos, "%s=", s); + while (*v) { + bpos += snprintf(buf + bpos, sizeof(buf) - bpos, "%s", unctrl(*v)); + v++; + } + return print_entry(pos, buf); +} + int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); @@ -118,32 +149,31 @@ int main(int argc, char *argv[]) return 1; } - printf("%s | %s\n", termname(), longname()); + printf("%s | %s", termname(), longname()); + int pos = COLS; for (size_t i = 0; i < n_flags; i++) { if (tigetflag(ti_flags[i]) > 0) { - printf("\t%s,\n", ti_flags[i]); + pos = print_entry(pos, ti_flags[i]); } } + pos = COLS; for (size_t i = 0; i < n_numbers; i++) { int n = tigetnum(ti_numbers[i]); if (n > -1) { - printf("\t%s#%d,\n", ti_numbers[i], n); + pos = print_number(pos, ti_numbers[i], n); } } + pos = COLS; for (size_t i = 0; i < n_strings; i++) { char *s = tigetstr(ti_strings[i]); if (s && s != (char*)-1) { - printf("\t%s=", ti_strings[i]); - while (*s) { - printf("%s", unctrl(*s)); - s++; - } - printf(",\n"); + pos = print_string(pos, ti_strings[i], s); } } + printf("\n"); return 0; } |