Created
August 31, 2022 21:31
-
-
Save rawiriblundell/fb645a7cbfcd3189c6f556d10977210c to your computer and use it in GitHub Desktop.
An answer to a reddit post that I gave here: https://www.reddit.com/r/bash/comments/vycqj8/can_someone_answer_this_please/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
table_max_width="${2:-80}" | |
# TO-DO: Add some smarts at this point to minimise issues caused by uneven column widths | |
# Subtract for our pipechars | |
# ---------|-----|------|-----|------|-----|------| | |
# ^ ^ ^ ^ These particular guys | |
columns_max_width="$(( table_max_width - 4 ))" | |
mapfile -t rawlines < "${1}" | |
# Calculate the width of the left 'A' column. | |
# ---------|------------|------------|------------| | |
# ^^^^^^^^^ this bit, excluding the pipe char | |
# In this example, we use the 'A1' cell as a proxy, and add 1 char of padding | |
# A fuller solution might look like "pull all lines that start with a char," | |
# "then sort them by size, selecting the longest" | |
# shellcheck disable=SC2086 | |
set -- ${rawlines[0]} | |
col_A_width="$(( "${#1}" + 1 ))" | |
# Subtract from our maximum width, then split in three for A2, A3 and A4 | |
columns_max_width=$(( columns_max_width - col_A_width )) | |
# This figures out this width | |
# ---------|-----|------|-----|------|-----|------| | |
# ^^^^^^^^^^^^ | |
col_BC_width=$(( columns_max_width / 3 )) | |
# The rest are the same, so don't recalc them, just copy them | |
col_DE_width="${col_BC_width}" | |
col_FG_width="${col_BC_width}" | |
# Now we split into our subcolumns | |
# Don't forget to subtract by 1 for a pipechar | |
# ---------|-----|------|-----|------|-----|------| | |
# ^ this guy | |
col_B_width="$(( (col_BC_width - 1) / 2 ))" | |
# Everything else should be the same width | |
col_C_width="${col_B_width}" | |
col_D_width="${col_B_width}" | |
col_E_width="${col_B_width}" | |
col_F_width="${col_B_width}" | |
col_G_width="${col_B_width}" | |
# shellcheck disable=SC2183 | |
print_top_border() { | |
printf -- '%*s+' "${col_A_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_BC_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_DE_width}" | tr ' ' "-" | |
printf -- '%*s+\n' "${col_FG_width}" | tr ' ' "-" | |
} | |
# shellcheck disable=SC2183 | |
print_mid_border() { | |
printf -- '%*s+' "${col_A_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_B_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_C_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_D_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_E_width}" | tr ' ' "-" | |
printf -- '%*s+' "${col_F_width}" | tr ' ' "-" | |
printf -- '%*s+\n' "${col_G_width}" | tr ' ' "-" | |
} | |
print_headers() { | |
# shellcheck disable=SC2086 | |
set -- ${rawlines[0]} | |
printf -- "%-${col_A_width}s|%${col_BC_width}s|%${col_DE_width}s|%${col_FG_width}s|\n" "${1}" "${2}" "${3}" "${4}" | |
} | |
# shellcheck disable=SC2059 | |
print_mid_line() { | |
print_fmt="%-${col_A_width}s|%${col_B_width}s|%${col_C_width}s|%${col_D_width}s|" | |
print_fmt="${print_fmt}%${col_E_width}s|%${col_F_width}s|%${col_G_width}s|\n" | |
printf -- "${print_fmt}" "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" | |
} | |
main() { | |
print_top_border | |
print_headers | |
print_mid_border | |
while read -r; do | |
case "${REPLY}" in | |
(Trial*) | |
print_mid_line ${REPLY} | |
;; | |
(*) | |
print_mid_line "" ${REPLY} | |
;; | |
esac | |
print_mid_border | |
done < <(printf -- '%s\n' "${rawlines[@]:1}") | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment