Skip to content

Commit 33d2fdd

Browse files
V1.2 - Added margin and better input trimming.
Updated and added examples and documentation.
1 parent 6ecbfbd commit 33d2fdd

File tree

7 files changed

+199
-93
lines changed

7 files changed

+199
-93
lines changed

display-box.sh

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# USAGE:
1414
#
1515
# As a script:
16-
# /path/to-display-box.sh "$headers" "$body" -s 2 -p 2 -d ,
16+
# /path/to-display-box.sh "$headers" "$body" -s 2 -p 2 -d , -m 1
1717
#
1818
# As a function:
1919
# . /path/to/display-box.sh
20-
# display-box "$headers" "$body" -s 2 -p 2 -d ,
20+
# display-box "$headers" "$body" -s 2 -p 2 -d , -m 1
2121
#
2222
# Or via a pipe:
23-
# echo "$multi_line_string" | /path/to/display-box.sh -s 2 -p 2 -d ,
23+
# echo "$multi_line_string" | /path/to/display-box.sh -s 2 -p 2 -d , -m 1
2424
#
2525
# To make it more convenient, you may want to create a symlink in the
2626
# global path, such as: /usr/local/bin/display-box
@@ -30,24 +30,40 @@
3030

3131
# Main function to display box
3232
display-box() {
33+
# Clean up input strings
34+
clean_string() {
35+
r=$(($required_spaces - 1))
36+
test "$r" -lt 1 && r=1
37+
echo "$1" | sed -E \
38+
-e '/^\s*$/d' `# Delete blank lines` \
39+
-e 's/([ ]{'$r'}[ ]+)/\t/g' `# Detect n spaces and replace with tab` \
40+
-e 's/^\s*//g' `# Trim leading spaces` \
41+
-e 's/\s*$//g' `# Trim trailing spaces`
42+
}
43+
3344
headers=""
3445
# Process options ($headers is always defined before $body)
3546
while [[ $# != 0 ]]; do
3647
case "$1" in
37-
-s) [[ ! -z "$2" ]] && { style="$2"; shift 2; } || shift;;
38-
-p) [[ ! -z "$2" ]] && { padding="$2"; shift 2; } || shift;;
39-
-d) [[ ! -z "$2" ]] && { delimiter="$2"; shift 2; } || shift;;
48+
-s) [[ ! -z "$2" ]] && { style=$2; shift 2; } || shift;;
49+
-p) [[ ! -z "$2" ]] && { padding=$2; shift 2; } || shift;;
50+
-d) [[ ! -z "$2" ]] && { delimiter=$2; shift 2; } || shift;;
51+
-m) [[ ! -z "$2" ]] && { margin=$2; shift 2; } || shift;;
4052
*)
53+
#i="$(echo "$1" | sed ':a;N;$!ba;s/^[[:space:]\n]*//;s/[[:space:]\n]*$//')" # Trim leading and trailing whitespace
54+
i="$(clean_string "$1")"
4155
if [[ -z "$headers" ]]; then
42-
headers="$1"
56+
headers="$i"
4357
elif [[ -z "$body" ]]; then
44-
body="$1"
45-
elif [[ -z "$style" ]] && [[ $1 =~ ^[1-6]$ ]]; then
46-
style="$1"
47-
elif [[ -z "$padding" ]] && [[ $1 =~ ^[1-6]$ ]]; then
48-
padding="$1"
49-
elif [[ -z "$delimiter" ]] && [[ $1 =~ ^.\ *$ ]]; then
50-
delimiter="$1"
58+
body="$i"
59+
elif [[ -z "$style" ]] && [[ $i =~ ^[1-6]$ ]]; then
60+
style=$i
61+
elif [[ -z "$padding" ]] && [[ $i =~ ^[1-6]$ ]]; then
62+
padding=$i
63+
elif [[ -z "$delimiter" ]] && [[ $i =~ ^.\ *$ ]]; then # 1 Character plus any number of spaces are optional
64+
delimiter=$i
65+
elif [[ -z "$margin" ]] && [[ $i =~ ^[0-9]+$ ]]; then
66+
margin=$i
5167
fi
5268
shift
5369
;;
@@ -58,6 +74,7 @@ display-box() {
5874
[[ -z "$style" ]] && style=1
5975
[[ -z "$padding" ]] && padding=1
6076
[[ -z "$delimiter" ]] && delimiter=$'\t'
77+
[[ -z "$margin" ]] && margin=0
6178

6279
# Catch missing inputs
6380
error=0
@@ -78,7 +95,11 @@ display-box() {
7895
error=1
7996
fi
8097
if [[ ! -z "$delimiter" ]] && [[ ! "$delimiter" =~ ^.\ *$ ]]; then
81-
echo "Invalid delimiter. delimiter must be a single character or 2+ spaces."
98+
echo "Invalid delimiter. Delimiter must be a single character or 2+ spaces."
99+
error=1
100+
fi
101+
if [[ ! -z "$margin" ]] && [[ ! "$margin" =~ ^.\ *$ ]]; then
102+
echo "Invalid margin number. Must be an integer."
82103
error=1
83104
fi
84105
if [[ "$error" -eq 1 ]]; then
@@ -93,17 +114,6 @@ display-box() {
93114
printf "%0.s$1" $(seq $2)
94115
}
95116

96-
# Clean up input strings
97-
clean_string() {
98-
r=$(($required_spaces - 1))
99-
test "$r" -lt 1 && r=1
100-
echo "$1" | sed -E \
101-
-e '/^\s*$/d' `# Delete blank lines` \
102-
-e 's/([ ]{'$r'}[ ]+)/\t/g' `# Detect n spaces and replace with tab` \
103-
-e 's/^\s*//g' `# Trim leading spaces` \
104-
-e 's/\s*$//g' `# Trim trailing spaces`
105-
}
106-
107117
# Set some default vars
108118
declare -a column_length
109119
required_spaces=2 # Spaces required as column separator
@@ -195,6 +205,15 @@ display-box() {
195205
done
196206
}
197207

208+
# Set margin above and below table
209+
margin() {
210+
if [[ $margin -gt 0 ]]; then
211+
for in in $(seq 1 $margin); do
212+
output+=$'\n'
213+
done
214+
fi
215+
}
216+
198217
# Display the top line above the header
199218
header_top_line() {
200219
for column in ${!column_length[@]}; do
@@ -259,6 +278,9 @@ display-box() {
259278
get_max_depth "$headers"
260279
get_max_depth "$body"
261280

281+
# Add top margin if any
282+
margin
283+
262284
# Add padding to the max depths
263285
pad_each_column
264286

@@ -271,8 +293,11 @@ display-box() {
271293
process_data "$body"
272294
[[ "$show_bottom" -eq 1 ]] && footer
273295

296+
# Add bottom margin if any
297+
margin
298+
274299
# Display the final output
275-
printf "$output"
300+
printf "%b" "$output"
276301
}
277302

278303

@@ -282,7 +307,7 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
282307
# Get headers/body/flags
283308
if [[ -z "$1" ]] || [[ "$1" =~ ^\-.*$ ]]; then
284309
# Capture stdin and separate header from body
285-
input="$(timeout 1 cat)"
310+
input="$(timeout 1 cat | sed ':a;N;$!ba;s/^[[:space:]\n]*//;s/[[:space:]\n]*$//')" # Trim leading and trailing whitespace
286311
headers="${input%%$'\n'*}"
287312
body="${input#*$'\n'}"
288313

@@ -292,10 +317,11 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
292317
-s) [[ ! -z "$2" ]] && { style="-s $2"; shift 2; } || shift;;
293318
-p) [[ ! -z "$2" ]] && { padding="-p $2"; shift 2; } || shift;;
294319
-d) [[ ! -z "$2" ]] && { delimiter="-d $2"; shift 2; } || shift;;
320+
-m) [[ ! -z "$2" ]] && { margin="-m $2"; shift 2; } || shift;;
295321
*) shift;;
296322
esac
297323
done
298-
display-box "$headers" "$body" $style $padding $delimiter
324+
display-box "$headers" "$body" $style $padding $delimiter $margin
299325
else
300326
# Try just running it with the same input
301327
display-box "$@"

examples/example-all.sh renamed to examples/example-all-styles.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ restore git stash pop Restore latest stashed changes
2525
wip git commit -am "WIP" Make a "Work in Progress" commit'
2626

2727
for i in $(seq 6); do
28-
printf "\n=== Style $i ===\n\n"
29-
display-box "$headers" "$body" -s $i
28+
printf "\n=== Style $i ===\n"
29+
display-box "$headers" "$body" -s $i -m 1
3030
done
3131

3232
echo

examples/example-inline.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/example-tabs.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
# This example shows how you can source the script initially, and have the display-box function
4+
# available to your environment. This is helpful if you want to execute it multiple times quickly.
5+
# Another option is to symlink the display-box.sh to location in your path.
6+
7+
script_path="$(cd "${0%/*}" && pwd)"
8+
. "$script_path/../display-box.sh"
9+
10+
headers='Git Alias Runs Description'
11+
12+
# Note the columns are using a tab character for separation. Other delimiter options are available.
13+
body='g git Quick shortcut to git command
14+
gh git help Display Git help
15+
g. git add . && git status Stage all changes and do a git status
16+
ga git add Stage files for commit
17+
gac git add . && git commit && git push Stage all files, commit (prompt for message) and push
18+
gb git branch List all branches
19+
gc git commit Commit staged files
20+
gca git commit -a --amend -C HEAD Commit the latest changes as an ammendment to the last commit
21+
gd git diff Show changes since last commit
22+
gf git fetch Fetch new changes from remote repo
23+
gl git log Show log of commits
24+
gp git pull Pull latest changes from remote repo
25+
gps git push Push commit(s) to remote repo
26+
gs git status Git Status
27+
branch git checkout -b Create a new branch or checkout existing branch
28+
stash git stash Stash current changes
29+
restore git stash pop Restore latest stashed changes
30+
wip git commit -am "WIP" Make a "Work in Progress" commit'
31+
32+
display-box "$headers" "$body" -s 1 -p 1 -m 1

examples/example-whitespace.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# This example uses multiple spaces to define the column delimiter.
4+
# This allows for easy visual editing. Just be careful to not accidentally include two spaces in the content.
5+
# This example also shows the simplicity of calling it in a single command. No need to include it first.
6+
7+
script_path="$(cd "${0%/*}" && pwd)"
8+
9+
echo '
10+
Git Alias Runs Description
11+
12+
g git Quick shortcut to git command
13+
gh git help Display Git help
14+
g. git add . && git status Stage all changes and do a git status
15+
ga git add Stage files for commit
16+
gac git add . && git commit && git push Stage all files, commit (prompt for message) and push
17+
gb git branch List all branches
18+
gc git commit Commit staged files
19+
gca git commit -a --amend -C HEAD Commit the latest changes as an ammendment to the last commit
20+
gd git diff Show changes since last commit
21+
gf git fetch Fetch new changes from remote repo
22+
gl git log Show log of commits
23+
gp git pull Pull latest changes from remote repo
24+
gps git push Push commit(s) to remote repo
25+
gs git status Git Status
26+
branch git checkout -b Create a new branch or checkout existing branch
27+
stash git stash Stash current changes
28+
restore git stash pop Restore latest stashed changes
29+
wip git commit -am "WIP" Make a "Work in Progress" commit
30+
' | $script_path/../display-box.sh -s 5 -p 2 -m 1
31+

examples/example.sh

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
#!/bin/bash
22

3+
# This example shows a comma-separated column delimiter with the -d , option.
4+
# Note that if the content includes a comma, you can simply escape it with: \,
5+
36
script_path="$(cd "${0%/*}" && pwd)"
4-
. "$script_path/../display-box.sh"
57

6-
headers='Git Alias Runs Description'
8+
content='
9+
Git Alias,Runs,Description
710
8-
body='g git Quick shortcut to git command
9-
gh git help Display Git help
10-
g. git add . && git status Stage all changes and do a git status
11-
ga git add Stage files for commit
12-
gac git add . && git commit && git push Stage all files, commit (prompt for message) and push
13-
gb git branch List all branches
14-
gc git commit Commit staged files
15-
gca git commit -a --amend -C HEAD Commit the latest changes as an ammendment to the last commit
16-
gd git diff Show changes since last commit
17-
gf git fetch Fetch new changes from remote repo
18-
gl git log Show log of commits
19-
gp git pull Pull latest changes from remote repo
20-
gps git push Push commit(s) to remote repo
21-
gs git status Git Status
22-
branch git checkout -b Create a new branch or checkout existing branch
23-
stash git stash Stash current changes
24-
restore git stash pop Restore latest stashed changes
25-
wip git commit -am "WIP" Make a "Work in Progress" commit'
11+
g,git,Quick shortcut to git command
12+
gh,git help,Display Git help
13+
g.,git add . && git status,Stage all changes and do a git status
14+
ga,git add,Stage files for commit
15+
gac,git add . && git commit && git push,Stage all files\, commit (prompt for message) and push
16+
gb,git branch,List all branches
17+
gc,git commit,Commit staged files
18+
gca,git commit -a --amend -C HEAD,Commit the latest changes as an ammendment to the last commit
19+
gd,git diff,Show changes since last commit
20+
gf,git fetch,Fetch new changes from remote repo
21+
gl,git log,Show log of commits
22+
gp,git pull,Pull latest changes from remote repo
23+
gps,git push,Push commit(s) to remote repo
24+
gs,git status,Git Status
25+
branch,git checkout -b,Create a new branch or checkout existing branch
26+
stash,git stash,Stash current changes
27+
restore,git stash pop,Restore latest stashed changes
28+
wip,git commit -am "WIP",Make a "Work in Progress" commit'
2629

27-
display-box "$headers" "$body" -s 1 -p 1
30+
echo "$content" | "$script_path/../display-box.sh" -s 4 -p 2 -d , -m 1

0 commit comments

Comments
 (0)