generated from justjavac/deno_starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcursor.ts
109 lines (91 loc) · 2.33 KB
/
cursor.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
import { ESC } from "./constants.ts";
const isTerminalApp = Deno.env.get("TERM_PROGRAM") === "Apple_Terminal";
/**
* moves cursor to line #, column #
* @param x column
* @param y line
*/
export function cursorTo(x: number, y: number): string {
return ESC + (y + 1) + ";" + (x + 1) + "H";
}
/**
* Set the position of the cursor relative to its current position.
* @param x
* @param y
*/
export function cursorMove(x: number, y: number): string {
let ret = "";
if (x < 0) {
ret += ESC + (-x) + "D";
} else if (x > 0) {
ret += ESC + x + "C";
}
if (y < 0) {
ret += ESC + (-y) + "A";
} else if (y > 0) {
ret += ESC + y + "B";
}
return ret;
}
/**
* moves cursor up # lines (default `1`).
* @param count
*/
export function cursorUp(count = 1): string {
return ESC + count + "A";
}
/**
* moves cursor down # lines (default `1`).
* @param count
*/
export function cursorDown(count = 1): string {
return ESC + count + "B";
}
/**
* moves cursor right # columns (default `1`).
* @param count
*/
export function cursorForward(count = 1): string {
return ESC + count + "C";
}
/**
* moves cursor left # columns (default `1`).
* @param count
*/
export function cursorBack(count = 1): string {
return ESC + count + "D";
} /**
* Moves the cursor to column `n` (default `1`).
* @param count
*/
export function cursorHorizontal(count = 1): string {
return ESC + count + "G";
}
/** saves the current cursor position */
export function cursorSavePosition(): string {
return isTerminalApp ? "\u001B7" : ESC + "s";
}
/** restores the cursor to the last saved position */
export function cursorRestorePosition(): string {
return isTerminalApp ? "\u001B8" : ESC + "u";
}
/** Reports the cursor position (CPR) to the application as (as though typed at the keyboard) */
export function cursorGetPosition(): string {
return ESC + "6n";
}
/** Moves cursor to beginning of the line n (default 1) lines down. */
export function cursorNextLine(): string {
return ESC + "E";
}
/** Moves cursor to beginning of the line n (default 1) lines up. */
export function cursorPrevLine(): string {
return ESC + "F";
}
/** DECTCEM Hides the cursor. */
export function cursorHide(): string {
return ESC + "?25l";
}
/** DECTCEM Shows the cursor, from the VT320. */
export function cursorShow(): string {
return ESC + "?25h";
}