File: book.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # book [page-height...] [filenames...]
  27 #
  28 # Layout lines on 2 page-like side-by-side columns, just like a book.
  29 #
  30 # Book shows lays out text-lines the same way pairs of pages are laid out in
  31 # books, letting you take advantage of wide screens. Every pair of pages ends
  32 # with a special dotted line to visually separate it from the next pair.
  33 #
  34 # If you're using Linux or MacOS, you may find this cmd-line shortcut useful:
  35 #
  36 # # Like A Book lays lines as pairs of pages, the same way books do it
  37 # lab() { book.sh "$(expr $(tput lines) - 1)" "$@" | less -KiCRS; }
  38 
  39 
  40 # handle help options
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         # show help message, extracting the info-comment at the start
  44         # of this file, and quit
  45         awk '/^# +book/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  46         exit 0
  47     ;;
  48 esac
  49 
  50 awk '
  51 BEGIN {
  52     # detect a leading number, and use it as a height value
  53     if (ARGV[1] + 0 != 0) {
  54         height = ARGV[1] + 0
  55         delete ARGV[1]
  56     }
  57 
  58     # add the current screen height to negative height values
  59     if (height < 0) {
  60         "tput lines" | getline n
  61         close("tput lines")
  62         height += n
  63     }
  64 
  65     # use the current screen height minus 1, when a height either was not
  66     # given explicitly, or is clearly too small
  67     if (height < 2) {
  68         "tput lines" | getline n
  69         close("tput lines")
  70         height = n - 1
  71     }
  72 
  73     # check if utf-8 strings need adapting when finding their width:
  74     # this happens when `awk` is `mawk`, as is the default in debian;
  75     # `mawk` does not count multi-byte UTF-8 symbols as single items
  76     ascii_only = length("█") > 1
  77 }
  78 
  79 # expand all tabs, using 4 as the tabstop width; only takes 1 argument
  80 function expand(s, n, l, res) {
  81     if (s !~ "\t") return s
  82 
  83     n = 0
  84     res = ""
  85 
  86     while (s != "") {
  87         i = index(s, "\t")
  88         if (i == 0) {
  89             res = res s
  90             return res
  91         }
  92 
  93         pre = substr(s, 1, i - 1)
  94         s = substr(s, i + 1)
  95         res = res pre
  96         l = width(pre)
  97         #l = length(pre)
  98         n += l
  99 
 100         switch (n % 4) {
 101             case 0:
 102                 res = res "    "
 103                 continue
 104             case 1:
 105                 res = res "   "
 106                 continue
 107             case 2:
 108                 res = res "  "
 109                 continue
 110             case 3:
 111                 res = res " "
 112                 continue
 113         }
 114     }
 115 
 116     return res
 117 }
 118 
 119 # remember all lines
 120 { lines[NR] = expand($0) }
 121 
 122 # width counts items in the string given, ignoring ANSI-style sequences
 123 function width(s) {
 124     gsub(/\x1b\[([0-9]*[A-HJKST]|[0-9;]*m)/, "", s)
 125     # adapt string if running on `mawk`, as detected at the beginning:
 126     # this is to force `mawk` to count multi-byte UTF-8 sequences as
 127     # single items, by literally turning those into single-byte items
 128     if (ascii_only) gsub(/[^\000-\177]{1,4}/, " ", s)
 129     return length(s)
 130 }
 131 
 132 END {
 133     step = height - 1
 134     if (NR <= step) {
 135         for (i in lines) print lines[i]
 136         exit
 137     }
 138 
 139     for (i = 1; i <= NR; i += 2 * step) {
 140         for (j = 0; j < step; j++) {
 141             l = width(lines[i + j])
 142             if (maxl < l) maxl = l
 143             l = width(lines[i + step + j])
 144             if (maxr < l) maxr = l
 145         }
 146     }
 147 
 148     # make a separator wide enough to match the length of any output line
 149     sep = "·"
 150     widest = maxl + 3 + maxr
 151     while (length(sep) < widest) sep = sep sep
 152     # separator is used directly, so match the needed width exactly
 153     sep = substr(sep, 1, widest)
 154 
 155     # make enough spaces to hand-pad lines later; these spaces can exceed
 156     # the max-count needed, since they are always subsliced when used later
 157     spaces = " "
 158     while (length(spaces) < widest) spaces = spaces spaces
 159 
 160     # emit lines side by side
 161     for (i = 1; i <= NR; i += 2 * step) {
 162         # emit a page-bottom/separator line between page-pairs
 163         if (i > 1) print sep
 164 
 165         for (j = 0; j < step; j++) {
 166             # avoid extra empty page-pair
 167             if (i + j > NR) exit
 168 
 169             l = lines[i + j]
 170             r = lines[i + step + j]
 171 
 172             #printf "%-*s █ %s\n", maxl, l, r
 173 
 174             # pick/emit lines side by side; hand-pad left pages to align
 175             # ANSI-styled text correctly
 176             padl = substr(spaces, 1, maxl - width(l))
 177             printf "%s%s █ %s\n", l, padl, r
 178         }
 179     }
 180 }
 181 ' "$@"