|
| 1 | +// Import Node.js Dependencies |
| 2 | +import * as TTY from "node:tty"; |
| 3 | + |
| 4 | +// Import Internal Dependencies |
| 5 | +import { Cursor } from "./Cursor.class.js"; |
| 6 | + |
| 7 | +export interface ReplaceOptions { |
| 8 | + clearOutput?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +export class InputBuffer { |
| 12 | + cursor: Cursor; |
| 13 | + |
| 14 | + private output: TTY.WriteStream; |
| 15 | + private value: string = ""; |
| 16 | + |
| 17 | + constructor( |
| 18 | + output: TTY.WriteStream |
| 19 | + ) { |
| 20 | + this.output = output; |
| 21 | + this.cursor = new Cursor(this.output); |
| 22 | + } |
| 23 | + |
| 24 | + clear() { |
| 25 | + this.output.moveCursor(this.cursor.offset - this.cursor.position, 0); |
| 26 | + this.output.moveCursor(-this.value.length, 0); |
| 27 | + this.output.clearLine(1); |
| 28 | + } |
| 29 | + |
| 30 | + cursorIsAtEnd() { |
| 31 | + return this.cursor.position === this.value.length; |
| 32 | + } |
| 33 | + |
| 34 | + append( |
| 35 | + input: string |
| 36 | + ): string { |
| 37 | + this.output.write(input); |
| 38 | + |
| 39 | + return this.appendValue(input); |
| 40 | + } |
| 41 | + |
| 42 | + appendAtCursor( |
| 43 | + input: string |
| 44 | + ): string { |
| 45 | + const rightSideOfValue = this.value.slice( |
| 46 | + this.cursor.position |
| 47 | + ); |
| 48 | + |
| 49 | + this.replaceValue( |
| 50 | + `${this.value.slice(0, this.cursor.position)}${input}${rightSideOfValue}`, |
| 51 | + ++this.cursor.position |
| 52 | + ); |
| 53 | + |
| 54 | + this.output.write(`${input}${rightSideOfValue}`); |
| 55 | + this.output.moveCursor(-rightSideOfValue.length, 0); |
| 56 | + |
| 57 | + return this.value; |
| 58 | + } |
| 59 | + |
| 60 | + appendValue( |
| 61 | + input: string, |
| 62 | + cursorLength: number = input.length |
| 63 | + ): string { |
| 64 | + this.value += input; |
| 65 | + this.cursor.position += cursorLength; |
| 66 | + |
| 67 | + return this.value; |
| 68 | + } |
| 69 | + |
| 70 | + appendChar( |
| 71 | + char: string |
| 72 | + ): { autocomplete: boolean; } { |
| 73 | + if (this.cursorIsAtEnd()) { |
| 74 | + this.appendValue(char); |
| 75 | + |
| 76 | + return { autocomplete: true }; |
| 77 | + } |
| 78 | + |
| 79 | + this.appendAtCursor(char); |
| 80 | + |
| 81 | + return { autocomplete: false }; |
| 82 | + } |
| 83 | + |
| 84 | + replace( |
| 85 | + input: string, |
| 86 | + options: ReplaceOptions = {} |
| 87 | + ): string { |
| 88 | + const { clearOutput = false } = options; |
| 89 | + |
| 90 | + if (clearOutput) { |
| 91 | + this.clear(); |
| 92 | + } |
| 93 | + this.output.write(input); |
| 94 | + |
| 95 | + return this.replaceValue(input); |
| 96 | + } |
| 97 | + |
| 98 | + replaceValue( |
| 99 | + input: string, |
| 100 | + cursorLength: number = input.length |
| 101 | + ): string { |
| 102 | + this.value = input; |
| 103 | + this.cursor.position = cursorLength; |
| 104 | + |
| 105 | + return this.value; |
| 106 | + } |
| 107 | + |
| 108 | + toString() { |
| 109 | + return this.value; |
| 110 | + } |
| 111 | +} |
0 commit comments