clean up handleInputKey

This commit is contained in:
Emil Lerch 2026-03-19 11:46:29 -07:00
parent b66b9391a5
commit ff87505771
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -477,13 +477,17 @@ pub const App = struct {
} }
} }
/// Handles keypresses in symbol_input mode (activated by `/`).
/// Mini text input for typing a ticker symbol (e.g. AAPL, BRK.B, ^GSPC).
fn handleInputKey(self: *App, ctx: *vaxis.vxfw.EventContext, key: vaxis.Key) void { fn handleInputKey(self: *App, ctx: *vaxis.vxfw.EventContext, key: vaxis.Key) void {
// Cancel input, return to normal mode
if (key.codepoint == vaxis.Key.escape) { if (key.codepoint == vaxis.Key.escape) {
self.mode = .normal; self.mode = .normal;
self.input_len = 0; self.input_len = 0;
self.setStatus("Cancelled"); self.setStatus("Cancelled");
return ctx.consumeAndRedraw(); return ctx.consumeAndRedraw();
} }
// Commit: uppercase the input, set as active symbol, switch to quote tab
if (key.codepoint == vaxis.Key.enter) { if (key.codepoint == vaxis.Key.enter) {
if (self.input_len > 0) { if (self.input_len > 0) {
for (self.input_buf[0..self.input_len]) |*ch| ch.* = std.ascii.toUpper(ch.*); for (self.input_buf[0..self.input_len]) |*ch| ch.* = std.ascii.toUpper(ch.*);
@ -499,17 +503,19 @@ pub const App = struct {
self.input_len = 0; self.input_len = 0;
return ctx.consumeAndRedraw(); return ctx.consumeAndRedraw();
} }
// Delete last character
if (key.codepoint == vaxis.Key.backspace) { if (key.codepoint == vaxis.Key.backspace) {
if (self.input_len > 0) self.input_len -= 1; if (self.input_len > 0) self.input_len -= 1;
return ctx.consumeAndRedraw(); return ctx.consumeAndRedraw();
} }
// Ctrl+U: clear entire input (readline convention)
if (key.matches('u', .{ .ctrl = true })) { if (key.matches('u', .{ .ctrl = true })) {
self.input_len = 0; self.input_len = 0;
return ctx.consumeAndRedraw(); return ctx.consumeAndRedraw();
} }
if (key.codepoint >= 0x20 and key.codepoint < 0x7f and self.input_len < self.input_buf.len) { // Accept printable ASCII (letters, digits, dots, hyphens, carets for tickers)
const c: u8 = @intCast(key.codepoint); if (key.codepoint < std.math.maxInt(u7) and std.ascii.isPrint(@intCast(key.codepoint)) and self.input_len < self.input_buf.len) {
self.input_buf[self.input_len] = std.ascii.toUpper(c); self.input_buf[self.input_len] = @intCast(key.codepoint);
self.input_len += 1; self.input_len += 1;
return ctx.consumeAndRedraw(); return ctx.consumeAndRedraw();
} }