-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.sh
More file actions
executable file
·104 lines (89 loc) · 2.06 KB
/
highlight.sh
File metadata and controls
executable file
·104 lines (89 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -o nounset
set -o errexit
# 终端文本高亮输出
# 可以设置终端文本的文字颜色,背景颜色,粗体,下划线
#
# Date: 2023-05-18
# Author: fdipzone
# Ver: 1.0
function highlight(){
# 正常样式
local normal=$(tput sgr0)
# 设置默认值
local str=""
local color=""
local bgcolor=""
local bold=0
local underline=0
# 获取配置参数
local LONGOPTS="str:,color:,bgcolor:,bold:,underline:"
local ARGS=$(getopt -o nothing --long $LONGOPTS -- "$@")
# 检查是否有错误
if [ $? -ne 0 ]; then
exit 1
fi
# 将参数转换为数组
eval set -- "$ARGS"
# 遍历参数数组
while true; do
case "$1" in
--str)
str="$2"
shift 2
;;
--color)
color="$2"
shift 2
;;
--bgcolor)
bgcolor="$2"
shift 2
;;
--bold)
bold="$2"
shift 2
;;
--underline)
underline="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "unknow option: $1"
exit 1
;;
esac
done
# 设置文本颜色
case "$color" in
0|1|2|3|4|5|6|7)
set_color=$(tput setaf $color;) ;;
*)
set_color="" ;;
esac
# 设置背景颜色
case "$bgcolor" in
0|1|2|3|4|5|6|7)
set_bgcolor=$(tput setab $bgcolor;) ;;
*)
set_bgcolor="" ;;
esac
# 设置粗体
if [ "$bold" = "1" ]; then
set_bold=$(tput bold;)
else
set_bold=""
fi
# 设置下划线
if [ "$underline" = "1" ]; then
set_underline=$(tput smul;)
else
set_underline=""
fi
# 打印文本
printf "${set_color}${set_bgcolor}${set_bold}${set_underline}${str}${normal}\n"
}