Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 7c0bd2f

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Move constants to own file
Also rename globals to just global and add update function for terminal width
1 parent d31d716 commit 7c0bd2f

File tree

8 files changed

+133
-64
lines changed

8 files changed

+133
-64
lines changed

lib/psst/basic.inc.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ fi
2626
# shellcheck source=basic/esc.inc.sh
2727
. "$INCLUDE_PSST/basic/esc.inc.sh"
2828

29+
# shellcheck source=basic/const.inc.sh
30+
. "$INCLUDE_PSST/basic/const.inc.sh"
31+
2932
# shellcheck source=basic/conv.inc.sh
3033
. "$INCLUDE_PSST/basic/conv.inc.sh"
3134

35+
# shellcheck source=basic/global.inc.sh
36+
. "$INCLUDE_PSST/basic/global.inc.sh"
37+
3238
# shellcheck source=basic/ifs.inc.sh
3339
. "$INCLUDE_PSST/basic/ifs.inc.sh"
3440

lib/psst/basic/const.inc.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env sh
2+
3+
# Double include protection
4+
case "${INCLUDE_SEEN_PSST-}" in
5+
*_const.inc.sh_*)
6+
return
7+
;;
8+
esac
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _const.inc.sh_"
10+
11+
12+
##
13+
# CONSTANT
14+
# NL_CHAR_PSST
15+
#
16+
# SUMMARY
17+
# Newline (`\n`) character as string.
18+
#
19+
NL_CHAR_PSST=$( printf "\n_" )
20+
NL_CHAR_PSST=${NL_CHAR_PSST%_}
21+
# shellcheck disable=SC2034
22+
readonly NL_CHAR_PSST
23+
24+
25+
##
26+
# CONSTANT
27+
# FS_CHAR_PSST
28+
#
29+
# SUMMARY
30+
# FS (file separator) character (28/0x1c) as string.
31+
#
32+
# shellcheck disable=SC2034
33+
FS_CHAR_PSST=$( printf "\34" )
34+
# shellcheck disable=SC2034
35+
readonly FS_CHAR_PSST
36+
37+
38+
##
39+
# CONSTANT
40+
# GS_CHAR_PSST
41+
#
42+
# SUMMARY
43+
# GS (group separator) character (29/0x1D) as string.
44+
#
45+
# shellcheck disable=SC2034
46+
GS_CHAR_PSST=$( printf "\35" )
47+
# shellcheck disable=SC2034
48+
readonly GS_CHAR_PSST
49+
50+
51+
##
52+
# CONSTANT
53+
# RS_CHAR_PSST
54+
#
55+
# SUMMARY
56+
# RS (record separator) character (30/0x1E) as string.
57+
#
58+
# shellcheck disable=SC2034
59+
RS_CHAR_PSST=$( printf "\36" )
60+
# shellcheck disable=SC2034
61+
readonly RS_CHAR_PSST
62+
63+
64+
##
65+
# CONSTANT
66+
# US_CHAR_PSST
67+
#
68+
# SUMMARY
69+
# US (unit separator) character (31/0x1F) as string.
70+
#
71+
# shellcheck disable=SC2034
72+
US_CHAR_PSST=$( printf "\37" )
73+
# shellcheck disable=SC2034
74+
readonly US_CHAR_PSST

lib/psst/basic/global.inc.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env sh
2+
3+
# Double include protection
4+
case "${INCLUDE_SEEN_PSST-}" in
5+
*_global.inc.sh_*)
6+
return
7+
;;
8+
esac
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _global.inc.sh_"
10+
11+
12+
##
13+
# function
14+
# global_update_term_width_psst
15+
#
16+
# SUMMARY
17+
# Update the cached terminal width.
18+
#
19+
global_update_term_width_psst()
20+
{
21+
if [ -n "${COLUMNS-}" ]
22+
then
23+
TERM_WIDTH_PSST="$COLUMNS"
24+
elif tput cols >/dev/null 2>&1
25+
then
26+
# shellcheck disable=SC2034
27+
TERM_WIDTH_PSST=$( tput cols )
28+
fi
29+
}
30+
31+
32+
##
33+
# VARIABLE
34+
# TERM_WIDTH_PSST
35+
#
36+
# SUMMARY
37+
# Cached terminal width.
38+
# It's cached as it's unlikely to change while a script is executing.
39+
# Falls back to 80 in case real width cannot be determined.
40+
#
41+
# shellcheck disable=SC2034
42+
TERM_WIDTH_PSST=80
43+
global_update_term_width_psst

lib/psst/basic/globals.inc.sh

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

lib/psst/basic/print.inc.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ esac
99
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _print.inc.sh_"
1010

1111

12-
# shellcheck source=globals.inc.sh
13-
. "$INCLUDE_PSST/basic/globals.inc.sh"
12+
# shellcheck source=global.inc.sh
13+
. "$INCLUDE_PSST/basic/global.inc.sh"
1414

1515

1616
##
@@ -40,7 +40,7 @@ print_psst()
4040
string="$*"
4141

4242
lines=$( printf "%s\n" "$string" \
43-
| fold -w "$TERMINAL_WIDTH_PSST" -s \
43+
| fold -w "$TERM_WIDTH_PSST" -s \
4444
| sed "s/ $//"
4545
)
4646
printf "%s\n" "$lines"
@@ -79,7 +79,7 @@ print_i_psst()
7979
IFS=""
8080
string="$*"
8181

82-
cols=$(( TERMINAL_WIDTH_PSST - indent ))
82+
cols=$(( TERM_WIDTH_PSST - indent ))
8383
lines=$( printf "%s\n" "$string" \
8484
| fold -w "$cols" -s \
8585
| sed "s/ $//"

lib/psst/file/glob.inc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _glob.inc.sh_"
1212
# shellcheck source=../basic/assert.inc.sh
1313
. "$INCLUDE_PSST/basic/assert.inc.sh"
1414

15-
# shellcheck source=../basic/globals.inc.sh
16-
. "$INCLUDE_PSST/basic/globals.inc.sh"
15+
# shellcheck source=../basic/global.inc.sh
16+
. "$INCLUDE_PSST/basic/global.inc.sh"
1717

1818
# shellcheck source=../basic/ifs.inc.sh
1919
. "$INCLUDE_PSST/basic/ifs.inc.sh"

test/basic/bin/test_perror.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fi
4242
# shellcheck source=../../../lib/psst/basic.inc.sh
4343
. "$INCLUDE_PSST/basic.inc.sh"
4444

45-
[ "$TERMINAL_WIDTH_PSST" = "30" ] || test_fail_psst $LINENO
45+
[ "$TERM_WIDTH_PSST" = "30" ] || test_fail_psst $LINENO
4646
)
4747

4848

@@ -53,7 +53,7 @@ then
5353
# shellcheck source=../../../lib/psst/basic.inc.sh
5454
. "$INCLUDE_PSST/basic.inc.sh"
5555

56-
[ "$TERMINAL_WIDTH_PSST" = "$( tput cols )" ] || test_fail_psst $LINENO
56+
[ "$TERM_WIDTH_PSST" = "$( tput cols )" ] || test_fail_psst $LINENO
5757
)
5858
fi
5959

test/basic/bin/test_print.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fi
4343
# shellcheck source=../../../lib/psst/basic.inc.sh
4444
. "$INCLUDE_PSST/basic.inc.sh"
4545

46-
[ "$TERMINAL_WIDTH_PSST" = "30" ] || test_fail_psst $LINENO
46+
[ "$TERM_WIDTH_PSST" = "30" ] || test_fail_psst $LINENO
4747
)
4848

4949

@@ -54,7 +54,7 @@ then
5454
# shellcheck source=../../../lib/psst/basic.inc.sh
5555
. "$INCLUDE_PSST/basic.inc.sh"
5656

57-
[ "$TERMINAL_WIDTH_PSST" = "$( tput cols )" ] || test_fail_psst $LINENO
57+
[ "$TERM_WIDTH_PSST" = "$( tput cols )" ] || test_fail_psst $LINENO
5858
)
5959
fi
6060

0 commit comments

Comments
 (0)