Skip to content

Commit

Permalink
use cat instead of self implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Oct 23, 2017
1 parent 7c9b93e commit b5d7b84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
26 changes: 7 additions & 19 deletions colines
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#beer-colines
# @author Jerry Lee (oldratlee at gmail dot com)

set -e
set -o pipefail

# NOTE: $'foo' is the escape sequence syntax of bash
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end
Expand All @@ -23,22 +26,7 @@ colorEcho() {
[ -t 1 ] && echo "$ec[1;${color}m$@$eend" || echo "$@"
}

readonly seperator="================================================================================"

if [ $# -eq 0 ]; then
# Bash read line does not read leading spaces https://stackoverflow.com/questions/29689172
while IFS= read -r line; do
colorEcho "$line"
done
else
for a; do
[ $# -gt 1 ] && {
echo "$seperator"
echo "$a"
echo "$seperator"
}
while IFS= read -r line; do
colorEcho "$line"
done < "$a"
done
fi
# Bash read line does not read leading spaces https://stackoverflow.com/questions/29689172
cat "$@" | while IFS= read -r line; do
colorEcho "$line"
done
11 changes: 3 additions & 8 deletions docs/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAz+ETZEgoLeIiC0rjWewdDs0sbo8c...== [email protected]
:beer: [colines](../colines)
----------------------

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

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

Expand All @@ -100,15 +101,9 @@ $ echo -e 'a\nb' | nl | colines
1 a
2 b
$ colines file1 file2.txt
================================================================================
file1
================================================================================
line1 of file1
line2 of file1
...
================================================================================
file2.txt
================================================================================
line1 of file2
line2 of file2
...
Expand Down

0 comments on commit b5d7b84

Please sign in to comment.