-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathimage-renderer.ts
More file actions
228 lines (200 loc) · 5.64 KB
/
image-renderer.ts
File metadata and controls
228 lines (200 loc) · 5.64 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
219
220
221
222
223
224
225
226
227
228
import {
ENTER_ALT_BUFFER,
EXIT_ALT_BUFFER,
HIDE_CURSOR,
moveCursor,
SHOW_CURSOR,
} from './ansi'
import {
diffImageCommands,
fullImageCommands,
type GraphemeImage,
} from './grapheme-image'
export type TerminalFrame = {
frame: GraphemeImage
cursor?: {
row: number
column: number
visible?: boolean
}
}
type Stdout = {
write: (data: string) => unknown
columns: number
rows: number
on: (event: 'resize', listener: () => void) => unknown
removeListener: (event: 'resize', listener: () => void) => unknown
isTTY: boolean
}
export type GetFrame = (numRows: number, numColumns: number) => TerminalFrame
export class Renderer {
stdout: Stdout
fps: number
refreshAllFps: number
getFrame: GetFrame
private lastFrame: GraphemeImage = [[]]
private lastRefreshTime: number
private lastFullRefreshTime: number
private onResize: () => void = () => {}
private timer: NodeJS.Timeout | null = null
private interval: NodeJS.Timeout | null = null
private inProgress: boolean = false
private lastCursorPosition: { row: number; column: number } | null = null
private cursorVisible: boolean = true
constructor({
stdout,
fps = 30,
refreshAllFps = 1,
getFrame,
}: {
stdout: Stdout
fps?: number
refreshAllFps?: number
getFrame: GetFrame
}) {
if (!stdout.isTTY) {
throw new Error('Must be interactive terminal.')
}
if (fps <= 0) {
throw new Error('FPS must be greater than 0')
}
if (refreshAllFps <= 0) {
throw new Error('Refresh all FPS must be greater than 0')
}
if (refreshAllFps > fps) {
throw new Error('Refresh all FPS must be FPS or less')
}
this.stdout = stdout
this.fps = fps
this.refreshAllFps = refreshAllFps
this.getFrame = (nRows, nCols) => {
const frame = getFrame(nRows, nCols)
if (frame.frame.length !== nRows) {
throw new Error(`Expected ${nRows} rows: got ${frame.frame.length}`)
}
for (const [i, row] of frame.frame.entries()) {
if (row.length !== nCols) {
throw new Error(
`Expected all rows to have ${nCols} columns: got ${row.length} in row ${i}`,
)
}
}
if (!frame.cursor) {
return frame
}
if (frame.cursor.row >= nRows || frame.cursor.row < 0) {
throw new Error(`Invalid cursor row: ${frame.cursor.row}`)
}
if (frame.cursor.column >= nCols || frame.cursor.column < 0) {
throw new Error(`Invalid cursor column: ${frame.cursor.column}`)
}
return frame
}
this.lastRefreshTime = 0
this.lastFullRefreshTime = 0
this.inProgress = false
this.lastCursorPosition = null
this.cursorVisible = true
}
public start() {
this.inProgress = true
this.lastCursorPosition = null
this.cursorVisible = true
this.stdout.write(ENTER_ALT_BUFFER)
this.onResize = () => {
this.refreshScreen(false, true)
}
this.onResize()
this.stdout.on('resize', this.onResize)
this.interval = setInterval(() => {
this.refreshScreen(false, true)
}, 1000 / this.refreshAllFps)
}
private forceRenderFrame(renderAll: boolean = false) {
if (this.timer) {
clearTimeout(this.timer)
this.timer = null
}
const frame = this.getFrame(this.stdout.rows, this.stdout.columns)
const now = Date.now()
if ((now - this.lastFullRefreshTime) * this.refreshAllFps > 1000) {
renderAll = true
}
const frameCommands = renderAll
? fullImageCommands(frame.frame)
: diffImageCommands(this.lastFrame, frame.frame)
const commands: string[] = []
if (frameCommands.length > 0) {
commands.push(frameCommands)
}
const cursor = frame.cursor
if (cursor) {
const desiredVisible = cursor.visible ?? true
const cursorMoved =
!this.lastCursorPosition ||
this.lastCursorPosition.row !== cursor.row ||
this.lastCursorPosition.column !== cursor.column
if (commands.length > 0 || cursorMoved) {
commands.push(moveCursor(cursor.row, cursor.column))
}
if (this.cursorVisible !== desiredVisible) {
commands.push(desiredVisible ? SHOW_CURSOR : HIDE_CURSOR)
this.cursorVisible = desiredVisible
}
this.lastCursorPosition = { row: cursor.row, column: cursor.column }
} else {
this.lastCursorPosition = null
if (this.cursorVisible) {
commands.push(HIDE_CURSOR)
this.cursorVisible = false
}
}
this.lastRefreshTime = now
if (renderAll) {
this.lastFullRefreshTime = now
}
this.lastFrame = frame.frame
if (commands.length === 0) {
return
}
this.stdout.write(commands.join(''))
}
public refreshScreen(
waitForNextFrame: boolean = true,
renderAll: boolean = false,
) {
if (!this.inProgress) {
throw new Error(
'Cannot refresh screen while not in progress. Call start() first.',
)
}
if (!waitForNextFrame) {
this.forceRenderFrame(renderAll)
return
}
const now = Date.now()
if (this.timer) {
return
}
// dt / 1000 < 1 / fps
const timeToWait = Math.max(this.lastRefreshTime + 1000 / this.fps - now, 0)
this.timer = setTimeout(() => {
this.forceRenderFrame(renderAll)
}, timeToWait)
}
public exit() {
if (this.interval) {
clearInterval(this.interval)
this.interval = null
}
if (this.timer) {
clearTimeout(this.timer)
this.timer = null
}
this.stdout.removeListener('resize', this.onResize)
this.stdout.write(EXIT_ALT_BUFFER)
this.lastCursorPosition = null
this.cursorVisible = true
this.inProgress = false
}
}