X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=retab;fp=retab;h=b04ee10537a945d91d42bf8f6c57fdb2054eef39;hb=e64e720d40790790421054dfcf99d81ef71e8ad3;hp=0000000000000000000000000000000000000000;hpb=7d320db4657f4d73b8e25c1c7947be57c870b5d7;p=hw%2Ftelelco diff --git a/retab b/retab new file mode 100644 index 0000000..b04ee10 --- /dev/null +++ b/retab @@ -0,0 +1,58 @@ +#!/usr/bin/nickle + +string[*][*] lines; +int[*] widths; + +string[*] get_one(file in) { + string l = File::fgets(in); + return String::wordsplit(l, "\t"); +} + +string[*][*] get_all(file in) { + string[...][*] l = {}; + while (!File::end(in)) + l[dim(l)] = get_one(in); + return l; +} + +int[*] find_widths(string[*][*] lines) { + int[...] w = {}; + for (int r = 0; r < dim(lines); r++) { + for (int c = 0; c < dim(lines[r]); c++) { + int len = String::length(lines[r][c]); + if (c >= dim(w)) + w[c] = len; + else + w[c] = max(w[c], len); + } + } + return w; +} + +void print_one(string s, int w) { + int l = String::length(s); + printf ("%s ", s); + while (l < w) { + putchar(' '); + l++; + } +} + +void print_line(string[*] line) { + for (int c = 0; c < dim(line); c++) + print_one(line[c], widths[c]); + putchar('\n'); +} + +void print_all() { + for (int r = 0; r < dim(lines); r++) + print_line(lines[r]); +} + +void doit () { + lines = get_all(stdin); + widths = find_widths(lines); + print_all(); +} + +doit();