-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-settitle.fish
executable file
·194 lines (164 loc) · 5.3 KB
/
copy-settitle.fish
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
184
185
186
187
188
189
190
191
192
193
194
function __settitle_debug
# echo "$argv" 1>&2
end
function __settitle_usage
echo "
settitle sets iTerm2 tab title and color
Usage examples:
settitle project1
just change the title
settitle project1 red
settitle red project1
settitle project1 1
settitle 1 project1
select ANSI 'red' color, with id 1 (argument order is guessed)
available colors: black red green yellow blue magenta cyan white
the colors are selected from the palette of 8
settitle project1 ffeea0
settitle project1 255 200 0
pick any color in RGB representation
settitle project1 work
the color depends on hashed value of 'work'
settitle
reset title & color
settitle defaultcolor
reset color only
See also: setpalette
"
end
function settitle -d "sets terminal title"
switch (count $argv)
case 0
__settitle_debug "no args given, reset title & color"
__settitle_set_title
__settitle_set_color "reset"
case 1
__settitle_debug "1 arg given"
if test "x$argv[1]" = "x-h" -o "x$argv[1]" = "x--help"
__settitle_usage
return
end
set color (__settitle_guess_color $argv[1])
if test -n "$color"
__settitle_set_color $color
else
__settitle_set_title $argv[1]
end
case 2
__settitle_debug "2 args given"
set color (__settitle_guess_color $argv[2])
if test -n "$color"
__settitle_set_title $argv[1]
__settitle_set_color $color
else
set color (__settitle_guess_color $argv[1])
if test -n "$color"
__settitle_set_title $argv[2]
__settitle_set_color $color
else
__settitle_set_title $argv[1]
__settitle_set_color (__settitle_hash_word $argv[2])
end
end
case 3
__settitle_debug "3 args given"
for arg in $argv
if test ! "(" "$arg" -ge 0 -a "$arg" -le 255 ")"
echo "error: RGB values must be within the range 0..255"
__settitle_usage
return 1
end
end
__settitle_set_color "$argv"
case 4
__settitle_debug "4 args given"
for arg in $argv[2] $argv[3]
if test ! "(" "$arg" -ge 0 -a "$arg" -le 255 ")"
echo "error: RGB values must be within the range 0..255"
__settitle_usage
return 1
end
end
if test "$argv[4]" -ge 0 -a "$argv[4]" -le 255
__settitle_set_title $argv[1]
__settitle_set_color "$argv[2] $argv[3] $argv[4]"
else if test "$argv[1]" -ge 0 -a "$argv[1]" -le 255
__settitle_set_title $argv[4]
__settitle_set_color "$argv[1] $argv[2] $argv[3]"
else
echo "error: RGB values must be within the range 0..255"
__settitle_usage
end
case '*'
__settitle_usage
return 1
end
end
function __settitle_guess_color -a word
switch $word
case "black"
echo (__settitle_get_color 1)
case "red"
echo (__settitle_get_color 2)
case "green"
echo (__settitle_get_color 3)
case "yellow"
echo (__settitle_get_color 4)
case "blue"
echo (__settitle_get_color 5)
case "magenta"
echo (__settitle_get_color 6)
case "cyan"
echo (__settitle_get_color 7)
case "white"
echo (__settitle_get_color 8)
case "defaultcolor"
echo "reset"
case '*'
if echo $word | grep -E -q '^[0-9a-fA-F]{6}$'
echo (__settitle_parse_hex $word)
else if test "$word" -gt 0 -a "$word" -le 8
echo (__settitle_get_color $word)
else
return 1
end
end
end
function __settitle_set_color -a rgb
if test "$rgb" = "reset"
__settitle_debug "resetting color to default"
echo -ne "\033]6;1;bg;*;default\a"
return
end
__settitle_debug "setting color to $rgb"
eval set rgb $rgb
echo -ne '\033]6;1;bg;red;brightness;'$rgb[1]'\a\033]6;1;bg;green;brightness;'$rgb[2]'\a\033]6;1;bg;blue;brightness;'$rgb[3]'\a'
end
function __settitle_set_title -a title
if test "$title" = "defaulttitle" -o -z "$title"
__settitle_debug "ressetting title to default"
set -e settitle_title
return
end
__settitle_debug "setting title to $title"
set -g settitle_title "$title"
end
function __settitle_parse_hex -a colorhex
set r (printf '%d' '0x'(echo $colorhex | cut -c1-2))
set g (printf '%d' '0x'(echo $colorhex | cut -c3-4))
set b (printf '%d' '0x'(echo $colorhex | cut -c5-6))
echo $r $g $b
end
function __settitle_hash_word -a word
__settitle_debug "setting color from the hash of \"$word\""
set num (printf '%d\n' '0x'(echo $word | shasum | cut -c1-6))
__settitle_debug "num: $num"
set colid (echo $num '% 8 + 1' | bc)
__settitle_debug "hashed colid: $colid"
echo (__settitle_get_color $colid)
end
function __settitle_get_color -a colid
# The variable generated automatically by `setpalette`
set __settitle_palette "0 0 0" "255 77 131" "31 140 59" "31 201 91" "29 211 238" "137 89 168" "62 153 159" "255 255 255"
echo $__settitle_palette[$colid]
end