-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdout.ts
More file actions
217 lines (201 loc) · 4.13 KB
/
stdout.ts
File metadata and controls
217 lines (201 loc) · 4.13 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
/**
* @fileoverview Standard output stream utilities.
* Provides utilities for writing to stdout with formatting and control.
*/
import process from 'node:process'
import { WriteStream } from 'node:tty'
import {
clearLineOn,
cursorToOn,
getColumnsOf,
getRowsOf,
isTTYOf,
} from './_stream'
// Get the actual stdout stream
const stdout: NodeJS.WriteStream = process.stdout
// Module-level flag for ensureCursorOnExit idempotency.
let _cursorExitRegistered = false
/**
* Clear the current line on stdout.
* Only works in TTY environments.
*
* @example
* ```ts
* write('Processing...')
* clearLine()
* write('Done!')
* ```
*/
export function clearLine(): void {
clearLineOn(stdout)
}
/**
* Clear screen from cursor position down to bottom.
* Only works in TTY environments.
*
* @example
* ```ts
* cursorTo(0, 5)
* clearScreenDown() // Clear from row 5 to bottom
* ```
*/
export function clearScreenDown(): void {
if (stdout.isTTY) {
stdout.clearScreenDown()
}
}
/**
* Move cursor to specific position on stdout.
* Only works in TTY environments.
*
* @param x - Column position (0-based)
* @param y - Row position (0-based, optional)
*
* @example
* ```ts
* cursorTo(0) // Move to start of line
* cursorTo(10, 5) // Move to column 10, row 5
* ```
*/
export function cursorTo(x: number, y?: number | undefined): void {
cursorToOn(stdout, x, y)
}
/**
* Register handlers to ensure cursor is shown on process exit.
* Prevents hidden cursor after abnormal termination.
* Handles SIGINT (Ctrl+C) and SIGTERM signals.
*
* @example
* ```ts
* ensureCursorOnExit()
* hideCursor()
* // Even if process crashes, cursor will be restored
* ```
*/
export function ensureCursorOnExit(): void {
if (_cursorExitRegistered) {
return
}
_cursorExitRegistered = true
process.on('exit', showCursor)
process.on('SIGINT', () => {
showCursor()
// eslint-disable-next-line n/no-process-exit
process.exit(130)
})
process.on('SIGTERM', () => {
showCursor()
// eslint-disable-next-line n/no-process-exit
process.exit(143)
})
}
/**
* Get the number of columns (width) in the terminal.
*
* @returns Terminal width in characters
* @default 80
*
* @example
* ```ts
* const width = getColumns()
* console.log(`Terminal is ${width} characters wide`)
* ```
*/
export function getColumns(): number {
return getColumnsOf(stdout)
}
/**
* Get the number of rows (height) in the terminal.
*
* @returns Terminal height in lines
* @default 24
*
* @example
* ```ts
* const height = getRows()
* console.log(`Terminal is ${height} lines tall`)
* ```
*/
export function getRows(): number {
return getRowsOf(stdout)
}
/**
* Hide the cursor on stdout.
* Useful for cleaner output during animations.
*
* @example
* ```ts
* hideCursor()
* // Show animation
* showCursor()
* ```
*/
export function hideCursor(): void {
if (stdout.isTTY && stdout instanceof WriteStream) {
stdout.write('\u001B[?25l')
}
}
/**
* Check if stdout is connected to a TTY (terminal).
*
* @returns `true` if stdout is a TTY, `false` if piped/redirected
*
* @example
* ```ts
* if (isTTY()) {
* // Show interactive UI
* } else {
* // Use simple text output
* }
* ```
*/
export function isTTY(): boolean {
return isTTYOf(stdout)
}
/**
* Show the cursor on stdout.
* Should be called after `hideCursor()`.
*
* @example
* ```ts
* hideCursor()
* // Show animation
* showCursor()
* ```
*/
export function showCursor(): void {
if (stdout.isTTY && stdout instanceof WriteStream) {
stdout.write('\u001B[?25h')
}
}
/**
* Write text to stdout without adding a newline.
*
* @param text - Text to write
*
* @example
* ```ts
* write('Loading...')
* // Later: clear and update
* ```
*/
export function write(text: string): void {
stdout.write(text)
}
/**
* Write a line to stdout with trailing newline.
*
* @param text - Text to write
* @default text ''
*
* @example
* ```ts
* writeLine('Hello, world!')
* writeLine() // Write empty line
* ```
*/
export function writeLine(text: string = ''): void {
stdout.write(`${text}\n`)
}
// Export the raw stream for advanced usage
export { stdout }