File: sbs.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 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 # sbs [options...] [column count...] [files...] 27 # 28 # Side-By-Side lays out lines read from all inputs given into several columns, 29 # separating them with a special symbol. If no named inputs are given, lines 30 # are read from the standard input, instead of files. 31 # 32 # If a column-count isn't given, the script tries to find the most columns 33 # which can fit a reasonable width-limit; when even a single column can't fit 34 # that limit, it simply emits all lines, which is the same as using 1 column. 35 # 36 # Hint: you can make handy aliases for this tool 37 # 38 # alias 0='sbs 0' 39 # alias 1='sbs 1' 40 # alias 2='sbs 2' 41 # alias 3='sbs 3' 42 # alias 4='sbs 4' 43 # alias 5='sbs 5' 44 # alias 6='sbs 6' 45 # alias 7='sbs 7' 46 # alias 8='sbs 8' 47 # alias 9='sbs 9' 48 # 49 # The options are, available both in single and double-dash versions 50 # 51 # -h, -help show this help message 52 # -sep use next argument as the column-separator symbol/string 53 # -t, -tab, -tabs use a single tab as the column-separator 54 55 56 sep='█' 57 58 while [ $# -gt 0 ]; do 59 case "$1" in 60 -h|--h|-help|--help) 61 awk '/^# +sbs /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 62 exit 0 63 ;; 64 65 -no-sep|--no-sep|-s|--s|-simple|--simple) 66 sep='' 67 shift 68 continue 69 ;; 70 71 -sep|--sep) 72 if [ $# -lt 2 ]; then 73 printf "forgot the separator value\n" >&2 74 exit 1 75 fi 76 shift 77 sep="$1" 78 shift 79 continue 80 ;; 81 82 -t|--t|-tab|--tab|-tabs|--tabs) 83 sep="\t" 84 shift 85 continue 86 ;; 87 88 --) break ;; 89 90 -*) 91 printf "unsupported option '%s'\n" "$1" >&2 92 exit 1 93 ;; 94 esac 95 96 break 97 done 98 99 num_columns=0 100 if echo "$1" | grep -q -E '^[+-]?[0-9]+$'; then 101 num_columns="$1" 102 shift 103 fi 104 105 [ "$1" = '--' ] && shift 106 107 # show all non-existing files given 108 failed=0 109 for arg in "$@"; do 110 [ "${arg}" = "-" ] && continue 111 [ -e "${arg}" ] && continue 112 printf "no file named \"%s\"\n" "${arg}" >&2 113 failed=1 114 done 115 116 # in case of errors, avoid showing an empty screen 117 [ "${failed}" -gt 0 ] && exit 2 118 119 # allow loading lines from multiple files, ensuring no lines are accidentally 120 # joined across inputs 121 awk 1 "$@" | 122 123 # ignore leading UTF-8 BOMs (byte-order marks) and trailing carriage-returns: 124 # the latter in particular will ruin side-by-side output; doing it separately 125 # using `sed` is faster overall for the script 126 sed 's-^\xef\xbb\xbf--; s-\r$--' | 127 128 # before laying out lines side-by-side, expand all tabs, using 4 as the 129 # tabstop width 130 expand -t 4 | 131 132 # lay lines into side-by-side columns 133 awk -v sep="${sep}" -v num_columns="${num_columns}" ' 134 BEGIN { 135 sep = " " sep 136 sep_width = width(sep) 137 total_max_width = 79 138 max_auto_cols = 26 139 140 # num_columns = 0 141 # detect a leading number, and use it as the max-number of columns 142 if (ARGV[1] + 0 != 0) { 143 num_columns = ARGV[1] + 0 144 delete ARGV[1] 145 } 146 } 147 148 # remember all lines; carriage-returns are already removed 149 { lines[NR] = $0 } 150 151 # width counts items in the string given, ignoring ANSI-style sequences 152 function width(s) { 153 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", s) 154 return length(s) 155 } 156 157 # pick a line using a row-column pair as an index 158 function pick(lines, height, row, col) { 159 return lines[(col - 1) * height + row] 160 } 161 162 function ceiling(n) { return (n % 1) ? n - (n % 1) + 1 : n } 163 164 # see if a list of lines can fit the number of columns given exactly 165 function fits(lines, num_lines, num_cols, height, max_widths, i, j, w, l) { 166 height = ceiling(num_lines / num_cols) 167 168 # prevent too many empty columns 169 if (height * num_cols - num_lines >= height) return 0 170 171 for (i = 1; i <= height; i++) { 172 for (j = 1; j <= num_cols; j++) { 173 w = width(pick(lines, height, i, j)) 174 if (w > total_max_width) return 0 175 if (max_widths[j] < w) max_widths[j] = w 176 } 177 } 178 179 for (i = 1; i <= height; i++) { 180 w = 0 181 182 for (j = 1; j <= num_cols; j++) { 183 if (j > 1) w += sep_width 184 185 l = pick(lines, height, i, j) 186 if (j > 1 && l != "") w++ 187 188 w += width(l) 189 190 if (j < num_cols) { 191 pad = max_widths[j] - width(l) 192 if (pad > 0) { 193 if (l == "") pad++ 194 w += pad 195 } 196 } 197 198 if (w > total_max_width) return 0 199 } 200 } 201 202 return 1 203 } 204 205 # auto-detect an appropriate column-count for the list of lines given 206 function find_max_fit(lines, num_columns) { 207 if (!fits(lines, NR, 1)) return 1 208 209 for (num_columns = max_auto_cols; num_columns >= 2; num_columns--) { 210 if (fits(lines, NR, num_columns)) return num_columns 211 } 212 return 1 213 } 214 215 END { 216 # prevent errors when input has no lines 217 if (NR < 1) exit 218 219 # auto-fit when given a non-positive count, or when not given a count 220 if (num_columns < 1) num_columns = find_max_fit(lines) 221 222 # prevent column-count from exceeding the number of input lines 223 if (num_columns > NR) num_columns = NR 224 225 # emit a single column as lines, quitting right away 226 if (num_columns < 2) { 227 for (i = 1; i <= NR; i++) print lines[i] 228 exit 229 } 230 231 # prevent a trailing empty column 232 num_lines = ceiling(NR / num_columns) 233 if (num_lines * num_columns - NR >= num_lines) num_columns-- 234 235 num_lines = ceiling(NR / num_columns) 236 for (i = 1; i <= num_lines; i++) { 237 for (j = 1; j <= num_columns; j++) { 238 w = width(pick(lines, num_lines, i, j)) 239 if (max_widths[j] < w) max_widths[j] = w 240 } 241 } 242 243 widest = 0 244 for (i = 1; i <= num_columns; i++) { 245 if (widest < max_widths[i]) widest = max_widths[i] 246 } 247 248 # emit lines side by side 249 for (i = 1; i <= num_lines; i++) { 250 for (j = 1; j <= num_columns; j++) { 251 l = pick(lines, num_lines, i, j) 252 253 if (j > 1) { 254 printf "%s", sep 255 if (j != num_columns || l != "") printf " " 256 } 257 258 printf "%s", l 259 260 if (j < num_columns) { 261 pad = max_widths[j] - width(l) 262 # if (pad > 0) printf "%*s", pad, "" 263 for (k = 1; k <= pad; k++) printf " " 264 } 265 } 266 267 print "" 268 } 269 } 270 '