79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tui / input.zig
9.1 KB
1const std = @import("std"); 2const render = @import("render.zig"); 3 4pub const Key = union(enum) { 5 text: []const u8, 6 submit, 7 newline, 8 backspace, 9 delete, 10 word_back, 11 left, 12 right, 13 word_left, 14 word_right, 15 up, 16 down, 17 history_prev, 18 history_next, 19 home, 20 end, 21 doc_start, 22 doc_end, 23 tab, 24 escape, 25 interrupt, 26 eof, 27 reverse_search, 28}; 29 30const SHIFT: u32 = 1; 31const ALT: u32 = 2; 32const CTRL: u32 = 4; 33const MODS: u32 = SHIFT | ALT | CTRL; 34 35const CODE = struct { 36 const enter = 13; 37 const escape = 27; 38 const tab = 9; 39 const backspace = 127; 40 const delete = 57349; 41 const left = 57350; 42 const right = 57351; 43 const up = 57352; 44 const down = 57353; 45 const home = 57356; 46 const end = 57357; 47}; 48 49fn lookup(code: u32, mods: u32) ?Key { 50 const k = code * 8 + mods; 51 return switch (k) { 52 CODE.enter * 8 + 0 => .submit, 53 CODE.enter * 8 + ALT => .newline, 54 CODE.enter * 8 + SHIFT => .newline, 55 CODE.escape * 8 + 0 => .escape, 56 CODE.tab * 8 + 0 => .tab, 57 CODE.backspace * 8 + 0 => .backspace, 58 CODE.backspace * 8 + CTRL => .word_back, 59 CODE.delete * 8 + 0 => .delete, 60 CODE.left * 8 + 0 => .left, 61 CODE.left * 8 + CTRL => .word_left, 62 CODE.right * 8 + 0 => .right, 63 CODE.right * 8 + CTRL => .word_right, 64 CODE.up * 8 + 0 => .up, 65 CODE.down * 8 + 0 => .down, 66 CODE.up * 8 + SHIFT => .history_prev, 67 CODE.down * 8 + SHIFT => .history_next, 68 CODE.home * 8 + 0 => .home, 69 CODE.home * 8 + CTRL => .doc_start, 70 CODE.end * 8 + 0 => .end, 71 CODE.end * 8 + CTRL => .doc_end, 72 97 * 8 + CTRL => .home, 73 98 * 8 + ALT => .word_back, 74 99 * 8 + CTRL => .interrupt, 75 100 * 8 + CTRL => .eof, 76 101 * 8 + CTRL => .end, 77 106 * 8 + CTRL => .newline, 78 114 * 8 + CTRL => .reverse_search, 79 119 * 8 + CTRL => .word_back, 80 else => null, 81 }; 82} 83 84const PASTE_START = "\x1b[200~"; 85const PASTE_END = "\x1b[201~"; 86 87pub const Parser = struct { 88 buffer: std.ArrayList(u8) = .empty, 89 pasting: bool = false, 90 91 pub const Pending = enum { none, escape, sequence }; 92 93 pub fn pending(self: *Parser) Pending { 94 if (self.pasting) return .none; 95 if (self.buffer.items.len == 1 and self.buffer.items[0] == 0x1b) return .escape; 96 return if (self.buffer.items.len > 1) .sequence else .none; 97 } 98 99 pub fn flushEscape(self: *Parser, a: std.mem.Allocator, out: *std.ArrayList(Key)) void { 100 if (self.buffer.items.len != 1 or self.buffer.items[0] != 0x1b) return; 101 self.buffer.clearRetainingCapacity(); 102 out.append(a, .escape) catch {}; 103 } 104 105 pub fn flushSequence(self: *Parser) void { 106 self.buffer.clearRetainingCapacity(); 107 } 108 109 pub fn feed(self: *Parser, a: std.mem.Allocator, chunk: []const u8, out: *std.ArrayList(Key)) void { 110 self.buffer.appendSlice(a, chunk) catch {}; 111 const buf = self.buffer.items; 112 var i: usize = 0; 113 while (true) { 114 if (self.pasting) { 115 const rest = buf[i..]; 116 if (std.mem.indexOf(u8, rest, PASTE_END)) |end| { 117 if (end > 0) out.append(a, .{ .text = normalizePaste(a, rest[0..end]) }) catch {}; 118 i += end + PASTE_END.len; 119 self.pasting = false; 120 continue; 121 } 122 if (rest.len > PASTE_END.len) { 123 const keep = PASTE_END.len - 1; 124 const emit = rest[0 .. rest.len - keep]; 125 if (emit.len > 0) out.append(a, .{ .text = normalizePaste(a, emit) }) catch {}; 126 i += emit.len; 127 } 128 break; 129 } 130 if (i >= buf.len) break; 131 if (std.mem.startsWith(u8, buf[i..], PASTE_START)) { 132 i += PASTE_START.len; 133 self.pasting = true; 134 continue; 135 } 136 if (buf[i] == 0x1b) { 137 if (!self.parseEscape(a, buf, &i, out)) break; 138 continue; 139 } 140 const seq_len = std.unicode.utf8ByteSequenceLength(buf[i]) catch 1; 141 if (i + seq_len > buf.len) break; 142 const cp = std.unicode.utf8Decode(buf[i .. i + seq_len]) catch buf[i]; 143 if (cp < 0x20) { 144 if (cp == 0x08) { 145 if (lookup(CODE.backspace, 0)) |key| out.append(a, key) catch {}; 146 } else if (cp == 0x09) { 147 out.append(a, .tab) catch {}; 148 } else if (cp == 0x0d) { 149 out.append(a, .submit) catch {}; 150 } else { 151 if (lookup(@as(u32, cp) + 0x60, CTRL)) |key| out.append(a, key) catch {}; 152 } 153 } else if (cp == 0x7f) { 154 if (lookup(CODE.backspace, 0)) |key| out.append(a, key) catch {}; 155 } else { 156 out.append(a, .{ .text = a.dupe(u8, buf[i .. i + seq_len]) catch "" }) catch {}; 157 } 158 i += seq_len; 159 } 160 if (i > 0) { 161 const remaining = buf[i..]; 162 std.mem.copyForwards(u8, self.buffer.items[0..remaining.len], remaining); 163 self.buffer.items.len = remaining.len; 164 } 165 } 166 167 fn parseEscape(self: *Parser, a: std.mem.Allocator, buf: []const u8, i: *usize, out: *std.ArrayList(Key)) bool { 168 if (buf.len - i.* < 2) return false; 169 const next = buf[i.* + 1]; 170 if (next == '[' or next == 'O') return self.parseCsi(a, buf, i, out); 171 if (next == ']') return self.skipString(buf, i, true); 172 if (next == 'P' or next == 'X' or next == '^' or next == '_') return self.skipString(buf, i, false); 173 const cp = next; 174 i.* += 2; 175 if (lookup(cp, ALT)) |key| out.append(a, key) catch {}; 176 return true; 177 } 178 179 fn parseCsi(self: *Parser, a: std.mem.Allocator, buf: []const u8, i: *usize, out: *std.ArrayList(Key)) bool { 180 _ = self; 181 var j = i.* + 2; 182 while (j < buf.len) : (j += 1) { 183 const c = buf[j]; 184 if (c >= 0x40 and c <= 0x7e) break; 185 } 186 if (j >= buf.len) return false; 187 const final = buf[j]; 188 const params = buf[i.* + 2 .. j]; 189 i.* = j + 1; 190 var code: ?u32 = null; 191 var mods: u32 = 0; 192 if (final == 'u') { 193 var fields = std.mem.splitScalar(u8, params, ';'); 194 const first = fields.next() orelse ""; 195 const code_str = first[0 .. std.mem.indexOfScalar(u8, first, ':') orelse first.len]; 196 const c = std.fmt.parseInt(u32, code_str, 10) catch return true; 197 if (c >= 57344) return true; 198 code = c; 199 if (fields.next()) |m| { 200 const mstr = m[0 .. std.mem.indexOfScalar(u8, m, ':') orelse m.len]; 201 mods = modsOf(mstr); 202 } 203 } else { 204 if (final == '~') { 205 var fields = std.mem.splitScalar(u8, params, ';'); 206 const first = fields.next() orelse ""; 207 const n = std.fmt.parseInt(u32, first, 10) catch return true; 208 code = switch (n) { 209 1, 7 => CODE.home, 210 3 => CODE.delete, 211 4, 8 => CODE.end, 212 else => null, 213 }; 214 if (fields.next()) |m| mods = modsOf(m); 215 } else { 216 code = switch (final) { 217 'A' => CODE.up, 218 'B' => CODE.down, 219 'C' => CODE.right, 220 'D' => CODE.left, 221 'H' => CODE.home, 222 'F' => CODE.end, 223 else => null, 224 }; 225 if (std.mem.indexOfScalar(u8, params, ';')) |semi| mods = modsOf(params[semi + 1 ..]); 226 } 227 } 228 if (code) |c| { 229 if (lookup(c, mods)) |key| out.append(a, key) catch {}; 230 } 231 return true; 232 } 233 234 fn skipString(self: *Parser, buf: []const u8, i: *usize, bel: bool) bool { 235 _ = self; 236 const rest = buf[i.* + 2 ..]; 237 const st = std.mem.indexOf(u8, rest, "\x1b\\"); 238 const bell = if (bel) std.mem.indexOfScalar(u8, rest, 0x07) else null; 239 var end: ?usize = null; 240 if (bell) |b| { 241 if (st == null or b < st.?) end = i.* + 2 + b + 1; 242 } 243 if (end == null) { 244 if (st) |s| end = i.* + 2 + s + 2; 245 } 246 if (end) |e| { 247 i.* = e; 248 return true; 249 } 250 return false; 251 } 252}; 253 254fn modsOf(field: []const u8) u32 { 255 const raw = std.fmt.parseInt(u32, field, 10) catch return 0; 256 if (raw == 0) return 0; 257 return (raw - 1) & MODS; 258} 259 260fn normalizePaste(a: std.mem.Allocator, text: []const u8) []const u8 { 261 var out: std.ArrayList(u8) = .empty; 262 var i: usize = 0; 263 while (i < text.len) : (i += 1) { 264 if (text[i] == '\r') { 265 out.append(a, '\n') catch {}; 266 if (i + 1 < text.len and text[i + 1] == '\n') i += 1; 267 } else { 268 out.append(a, text[i]) catch {}; 269 } 270 } 271 return render.stripAnsi(a, render.sanitize(a, out.items)); 272}