Skip to content

Commit

Permalink
feat: add taoc 🫴 🐈
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Sep 5, 2023
1 parent b82d532 commit 6aae801
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/r

1. [c](docs/shell.md#-c)
原样命令行输出,并拷贝标准输出到系统剪贴板,省去`CTRL+C`操作,优化命令行与其它应用之间的操作流。
1. [coat](docs/shell.md#-coat)
彩色`cat`出文件行,方便人眼区分不同的行。
1. [coat and taoc](docs/shell.md#-coat)
彩色`cat`/`tac`出文件行,方便人眼区分不同的行。
1. [a2l](docs/shell.md#-a2l)
按行彩色输出参数,方便人眼查看。
1. [uq](docs/shell.md#-uq)
Expand Down
36 changes: 36 additions & 0 deletions bin/taoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
# @Function
# tac lines colorfully. taoc means *CO*lorful c*AT* in reverse(last line first).
#
# @Usage
# $ echo -e 'Hello\nWorld' | taoc
# $ taoc /path/to/file1
# $ taoc /path/to/file1 /path/to/file2
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/shell.md#-coat
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail

# if not in console, use tac directly
# check isatty in bash https://stackoverflow.com/questions/10022323
[ ! -t 1 ] && exec tac "$@"

readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorPrint() {
local message="$*"

# skip color for white space
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$message"
else
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
printf "\033[1;${color}m%s\033[0m\n" "$message"
fi
}

# Bash read line does not read leading spaces
# https://stackoverflow.com/questions/29689172
tac "$@" | while IFS= read -r line; do
rotateColorPrint "$line"
done
33 changes: 28 additions & 5 deletions docs/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- [🍺 c](#-c)
- [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B)
- [参考资料](#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99)
- [🍺 coat](#-coat)
- [🍺 coat and taoc](#-coat-and-taoc)
- [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-1)
- [🍺 a2l](#-a2l)
- [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-2)
Expand Down Expand Up @@ -115,13 +115,16 @@ Options:
- [拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.github.io/post/2012-12-23/command-output-to-clip),给出了不同系统可用命令。
- 关于文本文件最后的换行,参见[Why should text files end with a newline?](https://stackoverflow.com/questions/729692)
🍺 [coat](../bin/coat)
<a id="-coat"></a>
🍺 [coat](../bin/coat) and [taoc](../bin/taoc)
----------------------
彩色`cat`出文件行,方便人眼区分不同的行。
彩色`cat`/`tac`出文件行,方便人眼区分不同的行。
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
命令支持选项、功能和使用方式与[`cat`命令](https://linux.die.net/man/1/cat)完全一样(实际上文件操作的实现全部代理给了`cat`命令)。
命令支持选项、功能和使用方式与[`cat`](https://linux.die.net/man/1/cat)/[`tac`](https://linux.die.net/man/1/cat)命令完全一样。
文件操作的实现上代理给`cat`/`tac`命令。
命令名`coat`意思是`COlorful cAT`;当然单词`coat`的意思是外套,彩色的输出行就像件漂亮的外套~ 😆
Expand All @@ -133,6 +136,9 @@ Hello world
$ echo -e 'Hello\nWorld' | coat
Hello
World
$ echo -e 'Hello\nWorld' | taoc
World
Hello
$ echo -e 'Hello\nWorld' | nl | coat
1 Hello
2 World
Expand All @@ -145,7 +151,7 @@ line2 of file2
...

# 帮助信息
# 可以看到本人机器上实现代理的`cat`命令是GNU的实现。
# 可以看到本人机器上实现代理的`cat`/`tac`命令是GNU的实现。
$ coat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.
Expand All @@ -172,6 +178,23 @@ Examples:
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'

$ taoc --help
Usage: tac [OPTION]... [FILE]...
Write each FILE to standard output, last line first.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-b, --before attach the separator before instead of after
-r, --regex interpret the separator as a regular expression
-s, --separator=STRING use STRING as the separator instead of newline
--help display this help and exit
--version output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/tac>
or available locally via: info '(coreutils) tac invocation'
```
注:上面示例中,没有彩色;在控制台上运行可以看出彩色效果,如下:
Expand Down

0 comments on commit 6aae801

Please sign in to comment.