#!/bin/sh # The MIT License (MIT) # # Copyright © 2024 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # bsbs [column count...] [filepaths...] # # Book-like Side-By-Side lays out lines read from all inputs given into several # columns, separating them with a special symbol. If no named inputs are given, # lines are read from the standard input, instead of files. # # If a column-count isn't given, it's 2 by default, just like with books. case "$1" in -h|--h|-help|--help) awk '/^# +bsbs/, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac num_columns=2 if [ "$(echo "$1" | grep -E '^[+-]?[0-9]+$' 2> /dev/null)" ]; then num_columns="$1" shift fi if [ "${num_columns}" -lt 1 ]; then num_columns=1 fi # use the current screen height minus 1 height="$(($(tput lines) - 1))" if [ "${height}" -lt 1 ]; then printf "screen/window isn't tall enough to show content\n" > /dev/stderr exit 1 fi awk ' # ignore leading UTF-8 BOMs (byte-order marks) FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } # carriage-returns will ruin side-by-side output, so remove them { gsub(/\r$/, ""); print } ' "$@" | # before laying out lines side-by-side, expand all tabs, using 4 as the # tabstop width expand -t 4 | awk -v num_columns="${num_columns}" -v height="${height}" ' BEGIN { sep = " █" sep_width = width(sep) height-- } # remember all lines; carriage-returns are already removed { lines[NR - 1] = $0 } # width counts items in the string given, ignoring ANSI-style sequences function width(s) { gsub(/\x1b\[([0-9]*[A-HJKST]|[0-9;]*m)/, "", s) return length(s) } # pick a line using a row-column pair as an index function pick(lines, height, page, row, col) { return lines[page + col * height + row] } # round up non-integers function ceil(n) { return (n % 1) ? n - (n % 1) + 1 : n } END { if (NR <= height) { for (i = 0; i < NR; i++) print lines[i] exit } if (num_columns < 1) num_columns = 1 for (i = 0; i < NR; i += height * num_columns) { for (j = 0; j < height; j++) { for (k = 0; k < num_columns; k++) { w = width(pick(lines, height, i, j, k)) if (max_widths[k] < w) max_widths[k] = w } } } widest = 0 for (i in max_widths) { if (widest < max_widths[i]) widest = max_widths[i] } # make enough spaces to hand-pad lines later; these spaces can exceed # the max-count needed, since they are always subsliced when used later spaces = " " nspaces = length(spaces) while (nspaces < widest) { spaces = spaces spaces nspaces *= 2 } total_max_width = 0 for (i = 0; i < num_columns; i++) { if (i > 0) total_max_width += sep_width + 1 total_max_width += max_widths[i] } # make a separator wide enough to match the length of any output line bottom_sep = "································" nsep = length(bottom_sep) while (nsep < total_max_width) { bottom_sep = bottom_sep bottom_sep nsep *= 2 } # separator is used directly, so match the needed width exactly bottom_sep = substr(bottom_sep, 1, total_max_width) # emit lines side by side for (i = 0; i < NR; i += height * num_columns) { # emit a page-bottom/separator line between pages of columns if (i > 0) print bottom_sep for (j = 0; j < height; j++) { # bottom-pad last page-pair with empty lines, so page-scrolling # on viewers like `less` stays in sync with the page boundaries if (NR - i - j <= 0) { print "" continue } for (k = 0; k < num_columns; k++) { l = pick(lines, height, i, j, k) if (k > 0) { printf "%s", sep if (k != num_columns - 1 || l != "") printf " " } printf "%s", l if (k < num_columns - 1) { pad = max_widths[k] - width(l) if (pad > 0) { s = substr(spaces, 1, pad) printf "%s", s } } } print "" } } # end last page with an empty line, instead of the usual page-separator if (NR > 0) print "" } ' | less -JMKiCRS