diff --git a/box.sh b/box.sh index 8d28b7a..5fbf41b 100644 --- a/box.sh +++ b/box.sh @@ -13,16 +13,59 @@ function printtop () { printf("%s\n", blank) } +function printline(a) +{ + printf("* %s%*s *\n", a, maxlength - length(a), "") +} + +# return the string a, with the character at pos, replaced with chr +function replace(a, pos, chr) +{ + return substr(a, 1, pos - 1) chr substr(a, pos + 1) +} + output == 1 && FNR == 1 { printtop() printf("* %*s *\n", maxlength, "") + + prevline = $0 + next } output == 1 { - printf("* %s%*s *\n", $0, maxlength - length, "") + # fix up horizontal issues + gsub("\\|-", "+-", prevline) + gsub("\\| -", "+--", prevline) + gsub("-\\|", "-+", prevline) + gsub("- \\|", "--+", prevline) + + # Fix up vertical issues + + # first fix edges from below + r = split($0, a, "|") + pos = 0 + for (i = 2; i <= r; i++) { + pos += length(a[i - 1]) + 1 + if (substr(prevline, pos, 1) == "-") + prevline = replace(prevline, pos, "+") + } + + # fix edges from above + r = split(prevline, a, "|") + pos = 0 + for (i = 2; i <= r; i++) { + pos += length(a[i - 1]) + 1 + if (substr($0, pos, 1) == "-") + $0 = replace($0, pos, "+") + } + + printline(prevline) + + prevline = $0 } END { + printline(prevline) printf("* %*s *\n", maxlength, "") printtop() }