-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle.sh
More file actions
187 lines (163 loc) · 4.69 KB
/
Copy pathstyle.sh
File metadata and controls
187 lines (163 loc) · 4.69 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
CLEAR_FORMAT="\x1B[0m"
SYMBOL_MODIFIERS=("blink" "reversed" "underlined" "crossed" "fg_color" "bg_color", "reset")
declare -gA _style_obj
# $1 - style name
function style.new() {
_style_obj["$1"]=1
_style_obj["$1:style"]=""
# Every style property set to 0
_style_obj["$1:clean"]=1
}
# $1 - style name
# $2, $3, ... - names of properties to enable, see SYMBOL_MODIFIERS for supported modifiers and their value param
# If property needs specific value you should pass it after = symbol, for example color=red
# Else pass 1 or 0 to enable or disable (if = unpassed then default will be 1)
# For example blink=1 or blink enables blink, blink=0 disables it
# See coresponding functions below for each property
function style.set_style() {
local i
declare -A style_with_value
for i in "${@:2}"
do
IFS="=" read key value <<< "$i"
style_with_value[$key]=$value
done
for i in "${!style_with_value[@]}"
do
local value=${style_with_value[$i]}
case $i in
blink)
style.set_blink $1 ${value:-1}
;;
reversed)
style.set_reverse $1 ${value:-1}
;;
underlined)
style.set_underline $1 ${value:-1}
;;
crossed)
style.set_cross $1 ${value:-1}
;;
bg_color)
style.set_background_color $1 ${value:-"255;255;255"}
;;
fg_color)
style.set_foreground_color $1 ${value:-"255;255;255"}
;;
reset)
style.set_reset $1 ${value:-1}
;;
*)
echo "Unrecognized $i property, exiting"
exit
;;
esac
done
}
# $1 style name
# $2 - enable(1) or disable(0)
# Makes text blink
function style.set_blink() {
style._set_property $1 $2 blink "\x1B[5m"
}
# $1 style name
# $2 - enable(1) or disable(0)
# Reverses foreground and background color
function style.set_reverse() {
style._set_property $1 $2 reversed "\x1B[7m"
}
# $1 style name
# $2 - enable(1) or disable(0)
# Underlines text
function style.set_underline() {
style._set_property $1 $2 underlined "\x1B[4m"
}
# $1 style name
# $2 - enable(1) or disable(0)
# Crosses text
function style.set_cross() {
style._set_property $1 $2 crossed "\x1B[9m"
}
# $1 style name
# $2 - enable(1) or disable(0)
# Clears all styles, applied before that style
# Not guaranteed that clear will not also remove current style, or half of it
# Better to use as separate option
function style.set_reset() {
style._set_property $1 $2 reset "\x1B[0m"
}
# $1 style name
# $2 - arg in r;g;b format, none or 0 for disabling
# Set foreground color (text, symbols, etc)
function style.set_foreground_color() {
local enable=1
local color
if [ "$2" = "none" ] || [ "$2" = "0" ]; then
enable=0
else
color_check $2
color="\x1B[38;2;${2}m"
fi
style._set_property $1 $enable fg_color $color
}
# $1 style name
# $2 - arg in r;g;b format, none or 0 for disabling
# Set background color (background, etc)
function style.set_background_color() {
local enable=1
local color
if [ "$2" = "none" ] || [ "$2" = "0" ]; then
enable=0
else
color_check $2
color="\x1B[48;2;${2}m"
fi
style._set_property $1 $enable bg_color $color
}
# $1 - style name
# $2 - x pos
# $3 - y pos
# Adds selected style to buffer symbol,
# !!! If you want to apply big amount of characters(for example background),
# then please use style.apply_rect, its way more faster
function style.apply() {
if [ ${_style_obj["$1:clean"]} -eq 0 ]; then
buffer.insert_modifier "${_style_obj["$1:style"]}" $2 $3
fi
}
# $1 - style name
# $2 - start x pos
# $3 - start y pos
# $4 - end x pos
# $5 - end y pos
function style.apply_rect() {
if [ ${_style_obj["$1:clean"]} -eq 0 ]; then
buffer.insert_modifier_rect "${_style_obj["$1:style"]}" $2 $3 $4 $5
fi
}
# $1 style name
# $2 - condition (1 or 0 for diasbling)
# $3 - key name
# $4 - code
# Sets any style property for currenty style obj
function style._set_property() {
_style_obj["$1:style"]=${_style_obj["$1:style"]/"${_style_obj["$1:style:$3"]}"/""}
if [ -z "$4" ] || [ "$2" = 0 ]; then
unset _style_obj["$1:style:$3"]
else
_style_obj["$1:style"]+="$4"
_style_obj["$1:style:$3"]="$4"
_style_obj["$1:clean"]=0
fi
}
# $1 style name
# Deletes style
function style.delete() {
unset _style_obj["$1"]
unset _style_obj["$1:style"]
unset _style_obj["$1:clean"]
local i
for i in "${STYLE_MODIFIERS[@]}"; do
unset _style_obj["$1:style:$i"]
done
}