-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear.ts
More file actions
218 lines (208 loc) · 4.92 KB
/
clear.ts
File metadata and controls
218 lines (208 loc) · 4.92 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @fileoverview Terminal clearing and cursor utilities.
* Provides functions for clearing lines, screens, and managing cursor position.
*/
import process from 'node:process'
/**
* Clear the current line in the terminal.
* Uses native TTY methods when available, falls back to ANSI escape codes.
* Uses native TTY methods when available and falls back to `\r\x1b[K` ANSI
* escapes on non-TTY streams.
*
* ANSI Sequences:
* - `\r`: Carriage return (move to line start)
* - `\x1b[K`: Clear from cursor to end of line
*
* @param stream - Output stream to clear (defaults to `process.stdout`)
* @default stream process.stdout
*
* @example
* ```ts
* clearLine() // Clear current line on stdout
* clearLine(process.stderr) // Clear on stderr
* ```
*/
export function clearLine(stream: NodeJS.WriteStream = process.stdout): void {
if (stream.isTTY) {
// TTY: Use cursor control
stream.cursorTo(0)
stream.clearLine(0)
} else {
// Non-TTY: Use ANSI escape codes
stream.write('\r\x1b[K')
}
}
/**
* Clear multiple lines above the current cursor position.
* Useful for clearing multi-line output like progress bars or status messages.
*
* ANSI Sequences:
* - `\x1b[1A`: Move cursor up one line
* - `\x1b[2K`: Erase entire line
*
* @param count - Number of lines to clear
* @param stream - Output stream to clear
* @default stream process.stdout
*
* @example
* ```ts
* console.log('Line 1')
* console.log('Line 2')
* console.log('Line 3')
* clearLines(2) // Clears lines 2 and 3
* ```
*/
export function clearLines(
count: number,
stream: NodeJS.WriteStream = process.stdout,
): void {
for (let i = 0; i < count; i++) {
// Move up and clear line
stream.write('\x1b[1A\x1b[2K')
}
}
/**
* Clear the entire screen and reset cursor to top-left.
* Only works in TTY environments.
*
* ANSI Sequence:
* - `\x1bc`: Full reset (clear screen and move cursor home)
*
* @param stream - Output stream to clear
* @default stream process.stdout
*
* @example
* ```ts
* clearScreen() // Clear entire terminal
* ```
*/
export function clearScreen(stream: NodeJS.WriteStream = process.stdout): void {
if (stream.isTTY) {
// Clear screen and move cursor to top-left
stream.write('\x1bc')
}
}
/**
* Clear the visible terminal screen.
* Alias for `clearScreen()`.
*
* @param stream - Output stream to clear
* @default stream process.stdout
*
* @example
* ```ts
* clearVisible() // Same as clearScreen()
* ```
*/
export function clearVisible(
stream: NodeJS.WriteStream = process.stdout,
): void {
clearScreen(stream)
}
/**
* Move cursor to the beginning of the current line.
* Uses native TTY methods when available, falls back to carriage return.
*
* @param stream - Output stream to manipulate
* @default stream process.stdout
*
* @example
* ```ts
* process.stdout.write('Some text...')
* cursorToStart()
* process.stdout.write('New text') // Overwrites from start
* ```
*/
export function cursorToStart(
stream: NodeJS.WriteStream = process.stdout,
): void {
if (stream.isTTY) {
stream.cursorTo(0)
} else {
stream.write('\r')
}
}
/**
* Hide the terminal cursor.
* Useful for cleaner output during animations or progress indicators.
*
* ANSI Sequence:
* - `\x1b[?25l`: DECTCEM hide cursor
*
* @param stream - Output stream to manipulate
* @default stream process.stdout
*
* @example
* ```ts
* hideCursor()
* // ... show animation
* showCursor()
* ```
*/
export function hideCursor(stream: NodeJS.WriteStream = process.stdout): void {
stream.write('\x1b[?25l')
}
/**
* Restore cursor to previously saved position.
* Must be called after `saveCursor()`.
*
* ANSI Sequence:
* - `\x1b8`: DECRC restore cursor
*
* @param stream - Output stream to manipulate
* @default stream process.stdout
*
* @example
* ```ts
* saveCursor()
* console.log('Temporary text')
* restoreCursor()
* console.log('Back at saved position')
* ```
*/
export function restoreCursor(
stream: NodeJS.WriteStream = process.stdout,
): void {
stream.write('\x1b8')
}
/**
* Save the current cursor position.
* Can be restored later with `restoreCursor()`.
*
* ANSI Sequence:
* - `\x1b7`: DECSC save cursor
*
* @param stream - Output stream to manipulate
* @default stream process.stdout
*
* @example
* ```ts
* saveCursor()
* console.log('Temporary text')
* restoreCursor()
* console.log('Back at saved position')
* ```
*/
export function saveCursor(stream: NodeJS.WriteStream = process.stdout): void {
stream.write('\x1b7')
}
/**
* Show the terminal cursor.
* Should be called after `hideCursor()` to restore normal cursor visibility.
*
* ANSI Sequence:
* - `\x1b[?25h`: DECTCEM show cursor
*
* @param stream - Output stream to manipulate
* @default stream process.stdout
*
* @example
* ```ts
* hideCursor()
* // ... show animation
* showCursor()
* ```
*/
export function showCursor(stream: NodeJS.WriteStream = process.stdout): void {
stream.write('\x1b[?25h')
}