Skip to content

Commit b5d7b84

Browse files
committed
use cat instead of self implementation
1 parent 7c9b93e commit b5d7b84

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

colines

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
# @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#beer-colines
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212

13+
set -e
14+
set -o pipefail
15+
1316
# NOTE: $'foo' is the escape sequence syntax of bash
1417
readonly ec=$'\033' # escape char
1518
readonly eend=$'\033[0m' # escape end
@@ -23,22 +26,7 @@ colorEcho() {
2326
[ -t 1 ] && echo "$ec[1;${color}m$@$eend" || echo "$@"
2427
}
2528

26-
readonly seperator="================================================================================"
27-
28-
if [ $# -eq 0 ]; then
29-
# Bash read line does not read leading spaces https://stackoverflow.com/questions/29689172
30-
while IFS= read -r line; do
31-
colorEcho "$line"
32-
done
33-
else
34-
for a; do
35-
[ $# -gt 1 ] && {
36-
echo "$seperator"
37-
echo "$a"
38-
echo "$seperator"
39-
}
40-
while IFS= read -r line; do
41-
colorEcho "$line"
42-
done < "$a"
43-
done
44-
fi
29+
# Bash read line does not read leading spaces https://stackoverflow.com/questions/29689172
30+
cat "$@" | while IFS= read -r line; do
31+
colorEcho "$line"
32+
done

docs/shell.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAz+ETZEgoLeIiC0rjWewdDs0sbo8c...== [email protected]
8383
:beer: [colines](../colines)
8484
----------------------
8585

86-
彩色`cat`出文件行,方便人眼区分不同的行。
87-
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
86+
彩色`cat`出文件行,方便人眼区分不同的行。
87+
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
88+
命令支持选项、功能和使用方式与[`cat`命令](https://linux.die.net/man/1/cat)完全一样(实际上在实现上全部代码给`cat`命令)。
8889

8990
命令名`colines`意思是`COLorful LINES`
9091

@@ -100,15 +101,9 @@ $ echo -e 'a\nb' | nl | colines
100101
1 a
101102
2 b
102103
$ colines file1 file2.txt
103-
================================================================================
104-
file1
105-
================================================================================
106104
line1 of file1
107105
line2 of file1
108106
...
109-
================================================================================
110-
file2.txt
111-
================================================================================
112107
line1 of file2
113108
line2 of file2
114109
...

0 commit comments

Comments
 (0)