-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
126 lines (93 loc) · 3.38 KB
/
mod.ts
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
// Copyright 2021 hex3928 (https://github.com/hex3928). All rights reserved. MIT license.
const ESC = "\u001B["
const OSC = "\u001B]"
const BEL = "\u0007"
const SEP = ";"
const isTermApp = Deno.env.get("TERM_PROGRAM")
interface Pos {
x: number
y?: number
}
interface ITermOpts {
pos: { x?: number, y?: number }
length?: number
isHidden?: boolean
}
interface ImageOpts {
width?: any
height?: any
preserveAspectRatio?: any
}
export const curTo = (pos: Pos) => {
return pos.y
? ESC + (pos.y + 1) + ";" + (pos.x + 1) + "H"
: ESC + (pos.x + 1) + "G"
}
export const curMove = (pos: Pos) => {
let result = ""
if (pos.x < 0) result += ESC + (-pos.x) + "D"
else
if (pos.x > 0) result += ESC + pos.x + "C"
if (pos.y)
if (pos.y < 0) result += ESC + (-pos.y) + "A"
else
if (pos.y > 0) result += ESC + pos.y + "B"
return result
}
export const curUp = (count = 1) => ESC + count + "A"
export const curDown = (count = 1) => ESC + count + "B"
export const curForward = (count = 1) => ESC + count + "C"
export const curBackward = (count = 1) => ESC + count + "D"
export const curLeft = () => ESC + "G"
export const curSavePosition = () => isTermApp ? "\u001B7" : ESC + "s"
export const curRestorePosition = () => isTermApp ? "\u001B8" : ESC + "u"
export const curGetPosition = () => ESC + "6n"
export const curNextLine = () => ESC + "E"
export const curPrevLine = () => ESC + "F"
export const curHide = () => ESC + "?25l"
export const curShow = () => ESC + "?25h"
export const eraseLines = (count: any) => {
let clear = ""
for (let i = 0; i < count; i++) clear += eraseLine() + (i < count - 1 ? curUp() : "")
if (count) clear += curLeft()
return clear
}
export const eraseEndLine = () => ESC + "K"
export const eraseStartLine = () => ESC + "1K"
export const eraseLine = () => ESC + "2K"
export const eraseDown = () => ESC + "J"
export const eraseUp = () => ESC + "1J"
export const eraseScreen = () => ESC + "2J"
export const scrollUp = () => ESC + "S"
export const scrollDown = () => ESC + "T"
export const clearScreen = () => "\u001Bc"
export const clearTerm = () => Deno.build.os === "windows" ?
`${eraseScreen()}${ESC}0f` :
`${eraseScreen()}${ESC}3J${ESC}H`
export const beep = () => BEL
export const link = (url: string | URL, text?: string) => `${OSC}8${SEP}${SEP}${url.toString()}${BEL}${text ? text : url.toString()}${OSC}8${SEP}${SEP}${BEL}`
export const image = (buffer: any, opts: ImageOpts) => {
let result = `${OSC}1337;File=inline=1`
if (opts.width) result += `;width=${opts.width}`
if (opts.height) result += `;height=${opts.height}`
if (opts.preserveAspectRatio === false) result += ";preserveAspectRatio=0"
return result + ":" + btoa(`${buffer}`) + BEL
}
export const iTerm = {
setCwd: (cwd = new URL(".", import.meta.url).pathname) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
annotation: (msg: any, opts: ITermOpts) => {
let result = `${OSC}1337;`
const hasX = typeof opts.pos.x !== "undefined"
const hasY = typeof opts.pos.y !== "undefined"
if ((hasX || hasY) && !(hasX && hasY && typeof opts.length !== "undefined")) throw new Error("`length` required when a position is provied")
msg = msg.replace(/\|/g, "")
result += opts.isHidden ? "AddHiddenAnnotation=" : "AddAnnotation="
if (opts.length && opts.length > 0)
result +=
(hasX
? [msg, opts.length, opts.pos.x, opts.pos.y]
: [opts.length, msg]).join("|")
else result += msg
return result + BEL
}
}