|
| 1 | +import { cursor, write } from "./terminal" |
| 2 | + |
| 3 | +export interface Key { |
| 4 | + name: string |
| 5 | + char?: string |
| 6 | + ctrl?: boolean |
| 7 | + meta?: boolean |
| 8 | +} |
| 9 | + |
| 10 | +export function parseKey(data: Buffer): Key { |
| 11 | + const s = data.toString() |
| 12 | + |
| 13 | + // Control characters |
| 14 | + if (s === "\x03") return { name: "c", ctrl: true } |
| 15 | + if (s === "\x04") return { name: "d", ctrl: true } |
| 16 | + if (s === "\x0c") return { name: "l", ctrl: true } |
| 17 | + |
| 18 | + // Special keys |
| 19 | + if (s === "\r" || s === "\n") return { name: "return" } |
| 20 | + if (s === "\x7f" || s === "\b") return { name: "backspace" } |
| 21 | + if (s === "\t") return { name: "tab" } |
| 22 | + if (s === "\x1b") return { name: "escape" } |
| 23 | + |
| 24 | + // Arrow keys |
| 25 | + if (s === "\x1b[A") return { name: "up" } |
| 26 | + if (s === "\x1b[B") return { name: "down" } |
| 27 | + if (s === "\x1b[C") return { name: "right" } |
| 28 | + if (s === "\x1b[D") return { name: "left" } |
| 29 | + |
| 30 | + // Home/End |
| 31 | + if (s === "\x1b[H" || s === "\x1b[1~") return { name: "home" } |
| 32 | + if (s === "\x1b[F" || s === "\x1b[4~") return { name: "end" } |
| 33 | + |
| 34 | + // Delete |
| 35 | + if (s === "\x1b[3~") return { name: "delete" } |
| 36 | + |
| 37 | + // Regular character |
| 38 | + if (s.length === 1 && s >= " ") return { name: "char", char: s } |
| 39 | + |
| 40 | + return { name: "unknown", char: s } |
| 41 | +} |
| 42 | + |
| 43 | +export class LineEditor { |
| 44 | + line = "" |
| 45 | + cursor = 0 |
| 46 | + history: string[] = [] |
| 47 | + historyIndex = -1 |
| 48 | + |
| 49 | + render(prompt: string) { |
| 50 | + write(`\r${prompt}${this.line}${" ".repeat(10)}\r${prompt}`) |
| 51 | + if (this.cursor > 0) { |
| 52 | + write(cursor.forward(this.cursor)) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + handle(key: Key): string | null { |
| 57 | + if (key.name === "return") { |
| 58 | + const result = this.line |
| 59 | + if (result.trim()) { |
| 60 | + this.history.push(result) |
| 61 | + } |
| 62 | + this.line = "" |
| 63 | + this.cursor = 0 |
| 64 | + this.historyIndex = -1 |
| 65 | + return result |
| 66 | + } |
| 67 | + |
| 68 | + if (key.name === "backspace" && this.cursor > 0) { |
| 69 | + this.line = this.line.slice(0, this.cursor - 1) + this.line.slice(this.cursor) |
| 70 | + this.cursor-- |
| 71 | + } |
| 72 | + |
| 73 | + if (key.name === "delete" && this.cursor < this.line.length) { |
| 74 | + this.line = this.line.slice(0, this.cursor) + this.line.slice(this.cursor + 1) |
| 75 | + } |
| 76 | + |
| 77 | + if (key.name === "left" && this.cursor > 0) { |
| 78 | + this.cursor-- |
| 79 | + } |
| 80 | + |
| 81 | + if (key.name === "right" && this.cursor < this.line.length) { |
| 82 | + this.cursor++ |
| 83 | + } |
| 84 | + |
| 85 | + if (key.name === "home") { |
| 86 | + this.cursor = 0 |
| 87 | + } |
| 88 | + |
| 89 | + if (key.name === "end") { |
| 90 | + this.cursor = this.line.length |
| 91 | + } |
| 92 | + |
| 93 | + if (key.name === "up" && this.history.length > 0) { |
| 94 | + if (this.historyIndex < this.history.length - 1) { |
| 95 | + this.historyIndex++ |
| 96 | + this.line = this.history[this.history.length - 1 - this.historyIndex] |
| 97 | + this.cursor = this.line.length |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (key.name === "down") { |
| 102 | + if (this.historyIndex > 0) { |
| 103 | + this.historyIndex-- |
| 104 | + this.line = this.history[this.history.length - 1 - this.historyIndex] |
| 105 | + this.cursor = this.line.length |
| 106 | + } else if (this.historyIndex === 0) { |
| 107 | + this.historyIndex = -1 |
| 108 | + this.line = "" |
| 109 | + this.cursor = 0 |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (key.name === "char" && key.char) { |
| 114 | + this.line = this.line.slice(0, this.cursor) + key.char + this.line.slice(this.cursor) |
| 115 | + this.cursor++ |
| 116 | + } |
| 117 | + |
| 118 | + // Ctrl+L to clear |
| 119 | + if (key.ctrl && key.name === "l") { |
| 120 | + return "/clear" |
| 121 | + } |
| 122 | + |
| 123 | + return null |
| 124 | + } |
| 125 | +} |
0 commit comments