-
Notifications
You must be signed in to change notification settings - Fork 0
/
osc.elv
167 lines (142 loc) · 3.76 KB
/
osc.elv
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
# Copyright (c) 2018, 2020-2022, Cody Opel <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# https://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
use github.com/chlorm/elvish-stl/re
use ./ansi
use ./rgb
# Terminfo represents hexidecimal RGB as 00/00/00.
fn -dec-to-ti-hex {|decRgbMap|
var hexRgbMap = (rgb:dec-to-hex $decRgbMap)
printf '%s/%s/%s' $hexRgbMap['r'] $hexRgbMap['g'] $hexRgbMap['b']
}
# TODO: Maybe support rgba, not sure if anything common returns this, but it exists.
fn -ti-hex-to-dec {|tiHexStr|
var d = '([a-fA-F0-9]{2,4})'
var parsedRgbList = (re:finds 'rgb\:'$d'/'$d'/'$d $tiHexStr)
var decRgbMap = (
rgb:hex-to-dec [
&r=$parsedRgbList[0]
&g=$parsedRgbList[1]
&b=$parsedRgbList[2]
]
)
put $decRgbMap
}
fn -osc {|cmd|
# This uses C0 control codes for portability. Windows does not interpret
# C1 control codes.
printf "%s%s%s%s%s" ^
$ansi:ESC ']' ^
$cmd ^
$ansi:ESC '\'
}
fn -set-color {|cmd decRgbMap|
-osc (printf "%s;rgb:%s" $cmd (-dec-to-ti-hex $decRgbMap))
}
# FIXME: elvish doesn't seem to have a builtin way to capture an evaluated
# response from the terminal.
# Sends an escape sequence to a terminal and captures the terminals response.
fn -get-raw-terminal-osc-response {|osc|
var response = $nil
try {
var t = (
sh -c '
set -e
elvish_term_osc_get_response() {
elvishTermOscSttySettings="$(stty -g)"
# Response can only be captured in raw mode.
set +e
stty raw -echo
printf "\u001B]'$osc'\u001B\\" >/dev/tty
read -r -t 1 -d ''\'' elvishTermOscResponse
stty $elvishTermOscSttySettings
set -e
printf "$elvishTermOscResponse" | cat -v
}
elvish_term_osc_get_response
'
)
set response = $t
} catch _ {
fail
}
if (eq $response $nil) {
fail
}
put $response
}
# Parses an OSC color string and returns an RGB map.
fn -get-color {|cmd|
var raw = (-get-raw-terminal-osc-response (printf '%s;?' $cmd))
var decRgbMap = (-ti-hex-to-dec $raw)
put $decRgbMap
}
# 1
fn set-icon-name {|icon|
-osc (printf '%s;%s' 1 $icon)
}
# 2
fn set-window-title {|title|
-osc (printf '%s;%s' 2 $title)
}
# 4
fn set-base-color {|colorIndex decRgbMap|
-set-color (printf '%s;%s' 4 $colorIndex) $decRgbMap
}
fn get-base-color {|colorIndex|
-get-color (printf '%s;%s' 4 $colorIndex)
}
# 10
fn set-foreground-color {|decRgbMap|
-set-color 10 $decRgbMap
}
fn get-foreground-color {
-get-color 10
}
# 11
fn set-background-color {|decRgbMap|
-set-color 11 $decRgbMap
}
fn get-background-color {
-get-color 11
}
# 12
fn set-cursor-color {|decRgbMap|
-set-color 12 $decRgbMap
}
fn get-cursor-color {
-get-color 12
}
# 50
# FIXME: check correct syntax
fn set-font {|font|
-osc (printf '%s;%s' 50 $font)
}
# TODO: 52
# 104
fn reset-base-colors {
-osc 104
}
# 110
fn reset-forground-color {
-osc 110
}
# 111
fn reset-background-color {
-osc 111
}
# 112
fn reset-cursor-color {
-osc 112
}