-
Notifications
You must be signed in to change notification settings - Fork 20
/
tty.elv
52 lines (41 loc) · 1.03 KB
/
tty.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
use re
use str
fn with-stty {|args cmd~|
var stty = (stty -g </dev/tty)
try { stty $@args </dev/tty >/dev/tty; cmd } finally { stty $stty </dev/tty >/dev/tty}
}
fn csi {|@cmd|
print "\e["(str:join '' [(
each {|e|
if (eq (kind-of $e) list) {
str:join ';' $e
} else {
put $e
}
} $cmd)]) >/dev/tty
}
fn csi-report {|delim @cmd|
with-stty [-echo raw] { csi $@cmd; read-upto $delim </dev/tty }
}
fn cursor-pos {
var res = (csi-report R 6 n)
put (re:find '\[(\d+);(\d+)R' $res)[groups][{1,2}][text]
}
# Short name alias according to https://en.wikipedia.org/wiki/ANSI_escape_code
var dsr~ = $cursor-pos~
fn set-cursor-pos {|row col|
csi [$row $col] H
}
# Short name alias according to https://en.wikipedia.org/wiki/ANSI_escape_code
var cur~ = $set-cursor-pos~
fn clear-line {|&mode=0|
csi $mode K
}
# Short name alias according to https://en.wikipedia.org/wiki/ANSI_escape_code
var el~ = $clear-line~
fn hide-cursor {
csi '?' 25 l
}
fn show-cursor {
csi '?' 25 h
}