Skip to content

Commit 59c17fd

Browse files
committed
feat: add taoc 🫴 🐈
1 parent b82d532 commit 59c17fd

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/r
7575

7676
1. [c](docs/shell.md#-c)
7777
原样命令行输出,并拷贝标准输出到系统剪贴板,省去`CTRL+C`操作,优化命令行与其它应用之间的操作流。
78-
1. [coat](docs/shell.md#-coat)
79-
彩色`cat`出文件行,方便人眼区分不同的行。
78+
1. [coat and taoc](docs/shell.md#-coat)
79+
彩色`cat`/`tac`出文件行,方便人眼区分不同的行。
8080
1. [a2l](docs/shell.md#-a2l)
8181
按行彩色输出参数,方便人眼查看。
8282
1. [uq](docs/shell.md#-uq)

bin/taoc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
# @Function
3+
# tac lines colorfully. taoc means *CO*lorful c*AT* in reverse(last line first).
4+
#
5+
# @Usage
6+
# $ echo -e 'Hello\nWorld' | taoc
7+
# $ taoc /path/to/file1
8+
# $ taoc /path/to/file1 /path/to/file2
9+
#
10+
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/shell.md#-coat
11+
# @author Jerry Lee (oldratlee at gmail dot com)
12+
set -eEuo pipefail
13+
14+
# if not in console, use tac directly
15+
# check isatty in bash https://stackoverflow.com/questions/10022323
16+
[ ! -t 1 ] && exec tac "$@"
17+
18+
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
19+
COUNT=0
20+
rotateColorPrint() {
21+
local message="$*"
22+
23+
# skip color for white space
24+
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
25+
printf '%s\n' "$message"
26+
else
27+
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
28+
printf "\033[1;${color}m%s\033[0m\n" "$message"
29+
fi
30+
}
31+
32+
# Bash read line does not read leading spaces
33+
# https://stackoverflow.com/questions/29689172
34+
tac "$@" | while IFS= read -r line; do
35+
rotateColorPrint "$line"
36+
done

docs/shell.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- [🍺 c](#-c)
99
- [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B)
1010
- [参考资料](#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99)
11-
- [🍺 coat](#-coat)
11+
- [🍺 coat and taoc](#-coat-and-taoc)
1212
- [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-1)
1313
- [🍺 a2l](#-a2l)
1414
- [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-2)
@@ -51,7 +51,7 @@
5151
原样命令行输出,并拷贝标准输出到系统剪贴板,省去`CTRL+C`操作,优化命令行与其它应用之间的操作流。
5252
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
5353

54-
命令名`c`意思是`Copy`,因为这个命令我平时非常常用,所以使用一个字符的命令名,方便快速键入。
54+
命令名`c`的意思是`Copy`,因为这个命令我平时非常常用,所以使用一个字符的命令名,方便快速键入。
5555

5656
更多说明参见[拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.github.io/post/2012-12-23/command-output-to-clip)
5757

@@ -115,15 +115,19 @@ Options:
115115
- [拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.github.io/post/2012-12-23/command-output-to-clip),给出了不同系统可用命令。
116116
- 关于文本文件最后的换行,参见[Why should text files end with a newline?](https://stackoverflow.com/questions/729692)
117117
118-
🍺 [coat](../bin/coat)
118+
<a id="-coat"></a>
119+
120+
🍺 [coat](../bin/coat) and [taoc](../bin/taoc)
119121
----------------------
120122
121-
彩色`cat`出文件行,方便人眼区分不同的行。
123+
彩色`cat`/`tac`出文件行,方便人眼区分不同的行。
122124
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
123125
124-
命令支持选项、功能和使用方式与[`cat`命令](https://linux.die.net/man/1/cat)完全一样(实际上文件操作的实现全部代理给了`cat`命令)。
126+
命令支持选项、功能和使用方式与[`cat`](https://linux.die.net/man/1/cat)/[`tac`](https://linux.die.net/man/1/cat)命令完全一样。
127+
文件操作在实现上完全代理给了`cat`/`tac`命令。
125128
126-
命令名`coat`意思是`COlorful cAT`;当然单词`coat`的意思是外套,彩色的输出行就像件漂亮的外套~ 😆
129+
- 命令名`coat`的意思是`COlorful cAT`;同时单词`coat`是外套,而彩色的输出行就像件漂亮的外套~ 🌈 😆
130+
- 命令名`taoc``coat`倒序拼写;命名方式就像`tac`之于`cat`。 🐈
127131
128132
### 用法/示例
129133
@@ -133,6 +137,9 @@ Hello world
133137
$ echo -e 'Hello\nWorld' | coat
134138
Hello
135139
World
140+
$ echo -e 'Hello\nWorld' | taoc
141+
World
142+
Hello
136143
$ echo -e 'Hello\nWorld' | nl | coat
137144
1 Hello
138145
2 World
@@ -145,7 +152,7 @@ line2 of file2
145152
...
146153

147154
# 帮助信息
148-
# 可以看到本人机器上实现代理的`cat`命令是GNU的实现。
155+
# 可以看到本人机器上实现代理的`cat`/`tac`命令是GNU的实现。
149156
$ coat --help
150157
Usage: cat [OPTION]... [FILE]...
151158
Concatenate FILE(s) to standard output.
@@ -172,6 +179,23 @@ Examples:
172179
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
173180
Full documentation at: <https://www.gnu.org/software/coreutils/cat>
174181
or available locally via: info '(coreutils) cat invocation'
182+
183+
$ taoc --help
184+
Usage: tac [OPTION]... [FILE]...
185+
Write each FILE to standard output, last line first.
186+
187+
With no FILE, or when FILE is -, read standard input.
188+
189+
Mandatory arguments to long options are mandatory for short options too.
190+
-b, --before attach the separator before instead of after
191+
-r, --regex interpret the separator as a regular expression
192+
-s, --separator=STRING use STRING as the separator instead of newline
193+
--help display this help and exit
194+
--version output version information and exit
195+
196+
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
197+
Full documentation <https://www.gnu.org/software/coreutils/tac>
198+
or available locally via: info '(coreutils) tac invocation'
175199
```
176200
177201
注:上面示例中,没有彩色;在控制台上运行可以看出彩色效果,如下:
@@ -183,7 +207,7 @@ or available locally via: info '(coreutils) cat invocation'
183207
按行彩色输出参数,方便人眼查看。
184208
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
185209
186-
命令名`a2l`意思是`Arguments to(2) Lines`
210+
命令名`a2l`的意思是`Arguments to(2) Lines`
187211
188212
### 用法/示例
189213
@@ -336,7 +360,7 @@ Options:
336360
批量转换文件路径为绝对路径/相对路径,会自动跟踪链接并规范化路径。
337361
支持`Linux``Mac``Windows``cygwin``MSSYS`)。
338362
339-
命令名`ap`意思是`Absolute Path``rp``Relative Path`
363+
命令名`ap`的意思是`Absolute Path``rp``Relative Path`
340364
341365
### 用法/示例
342366
@@ -445,7 +469,7 @@ SYN_SENT 7
445469
- `xpl`:在文件浏览器中打开指定的文件或文件夹。
446470
`xpl``explorer`的缩写。
447471
- `xpf`: 在文件浏览器中打开指定的文件或文件夹,并选中。
448-
`xpf``explorer and select file`的缩写。
472+
`xpf``EXplorer and select File`的缩写。
449473
450474
### 用法/示例
451475

0 commit comments

Comments
 (0)