|
- #!/bin/sh
- fname=$(mktemp foobar.XXXXX)
- cat > "$fname"
- awk '
- maxline == 1 {
- if (maxlength < length)
- maxlength = length
- }
-
- function printtop () {
- blank = sprintf("%*s", maxlength + 4, "")
- gsub(" ", "*", blank)
- 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 {
- # 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()
- }
- ' maxline=1 "$fname" maxline=0 output=1 "$fname"
-
- rm "$fname"
|