Skip to content

Commit 5b25e67

Browse files
committed
fix(coat/taoc) šŸž: missing last line if there’s no newline at the end of the file
1 parent 68b38e7 commit 5b25e67

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

ā€Žbin/coat

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,42 @@
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212
set -eEuo pipefail
1313

14-
# if stdout is a terminal, use `cat` directly.
14+
# if stdout is not a terminal, use `cat` directly.
1515
# '-t' check: is a terminal?
1616
# check isatty in bash https://stackoverflow.com/questions/10022323
17-
[ ! -t 1 ] && exec cat "$@"
17+
[ -t 1 ] || exec cat "$@"
1818

1919
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
2020
COUNT=0
21+
# CAUTION: print content WITHOUT new line
2122
rotateColorPrint() {
2223
local content=$*
23-
2424
# skip color for white space
2525
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
26-
printf '%s\n' "$content"
26+
printf %s "$content"
2727
else
2828
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
29-
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
29+
printf '\e[1;%sm%s\e[0m' "$color" "$content"
3030
fi
3131
}
3232

33+
rotateColorPrintln() {
34+
rotateColorPrint "$*"$'\n'
35+
}
36+
3337
colorLines() {
38+
local line
3439
# Bash read line does not read leading spaces
3540
# https://stackoverflow.com/questions/29689172
3641
while IFS= read -r line; do
37-
rotateColorPrint "$line"
42+
rotateColorPrintln "$line"
3843
done
44+
# How to use `while read` (Bash) to read the last line in a file
45+
# if there’s no newline at the end of the file?
46+
# https://stackoverflow.com/questions/4165135
47+
if [ -n "$line" ]; then
48+
rotateColorPrint "$line"
49+
fi
3950
}
4051

4152
if [ $# == 0 ]; then

ā€Žbin/taoc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,42 @@
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212
set -eEuo pipefail
1313

14-
# if stdout is a terminal, use `tac` directly.
14+
# if stdout is not a terminal, use `tac` directly.
1515
# '-t' check: is a terminal?
1616
# check isatty in bash https://stackoverflow.com/questions/10022323
17-
[ ! -t 1 ] && exec tac "$@"
17+
[ -t 1 ] || exec tac "$@"
1818

1919
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
2020
COUNT=0
21+
# CAUTION: print content WITHOUT new line
2122
rotateColorPrint() {
2223
local content=$*
23-
2424
# skip color for white space
2525
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
26-
printf '%s\n' "$content"
26+
printf %s "$content"
2727
else
2828
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
29-
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
29+
printf '\e[1;%sm%s\e[0m' "$color" "$content"
3030
fi
3131
}
3232

33+
rotateColorPrintln() {
34+
rotateColorPrint "$*"$'\n'
35+
}
36+
3337
colorLines() {
38+
local line
3439
# Bash read line does not read leading spaces
3540
# https://stackoverflow.com/questions/29689172
3641
while IFS= read -r line; do
37-
rotateColorPrint "$line"
42+
rotateColorPrintln "$line"
3843
done
44+
# How to use `while read` (Bash) to read the last line in a file
45+
# if there’s no newline at the end of the file?
46+
# https://stackoverflow.com/questions/4165135
47+
if [ -n "$line" ]; then
48+
rotateColorPrint "$line"
49+
fi
3950
}
4051

4152
tac "$@" | colorLines

0 commit comments

Comments
Ā (0)