| 6 | * and communicate with one another |
| 7 | */ |
| 8 | export default class WasmTTY { |
| 9 | private xterm: Terminal |
| 10 | |
| 11 | private inputCursorX: number |
| 12 | private inputCursorY: number |
| 13 | private inputStartCursorX: number |
| 14 | private inputStartCursorY: number |
| 15 | private input: string[] |
| 16 | private inputSplit: string[][] |
| 17 | |
| 18 | constructor(xterm: Terminal) { |
| 19 | this.xterm = xterm |
| 20 | this.input = [] |
| 21 | this.inputSplit = [] |
| 22 | this.inputCursorX = 0 |
| 23 | this.inputCursorY = 0 |
| 24 | } |
| 25 | |
| 26 | async read() { |
| 27 | this.input = [] |
| 28 | this.inputSplit = [] |
| 29 | |
| 30 | const promise = new Promise((resolve) => { |
| 31 | this.xterm.write('\x00', () => resolve(true)) |
| 32 | }).then(() => { |
| 33 | const buf = this.xterm.buffer.active |
| 34 | this.inputStartCursorX = buf.cursorX |
| 35 | this.inputStartCursorY = buf.cursorY + buf.baseY |
| 36 | this.inputCursorX = this.inputStartCursorX |
| 37 | this.inputCursorY = 0 |
| 38 | this.xterm.focus() |
| 39 | }) |
| 40 | |
| 41 | return promise |
| 42 | } |
| 43 | |
| 44 | reflowInput() { |
| 45 | const cols = this.xterm.cols |
| 46 | const lines: string[][] = [] |
| 47 | |
| 48 | const inputChars = Array.from(this.input) |
| 49 | |
| 50 | const firstLine = inputChars.slice(0, cols - this.inputStartCursorX) |
| 51 | lines.push(firstLine) |
| 52 | let index = firstLine.length |
| 53 | let lastLineFull = firstLine.length === cols - this.inputStartCursorX |
| 54 | while (index < inputChars.length) { |
| 55 | const line = inputChars.slice(index, index + cols) |
| 56 | lines.push(line) |
| 57 | index += line.length |
| 58 | lastLineFull = line.length == cols |
| 59 | } |
| 60 | if (lastLineFull) { |
| 61 | lines.push(['']) |
| 62 | } |
| 63 | |
| 64 | // TODO: Redraw relevant lines |
| 65 | this.inputSplit = lines |
nothing calls this directly
no test coverage detected