From 8d496ffcdd2029dcafec5962b971d3669032f975 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 24 Jan 2024 09:54:22 -0600 Subject: [PATCH] widgets(textinput): move key handling to ifelse block Signed-off-by: Tim Culverhouse --- src/widgets/TextInput.zig | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/widgets/TextInput.zig b/src/widgets/TextInput.zig index b1f15c6..f21b371 100644 --- a/src/widgets/TextInput.zig +++ b/src/widgets/TextInput.zig @@ -35,24 +35,20 @@ pub fn deinit(self: *TextInput) void { pub fn update(self: *TextInput, event: Event) !void { switch (event) { .key_press => |key| { - if (key.text) |text| { - try self.buf.insertSlice(self.byteOffsetToCursor(), text); - self.cursor_idx += 1; - self.grapheme_count += 1; - } if (key.matches(Key.backspace, .{})) { if (self.cursor_idx == 0) return; self.deleteBeforeCursor(); - } - if (key.matches(Key.delete, .{})) { + } else if (key.matches(Key.delete, .{})) { if (self.cursor_idx == self.grapheme_count) return; self.deleteAtCursor(); - } - if (key.matches(Key.left, .{})) { + } else if (key.matches(Key.left, .{})) { if (self.cursor_idx > 0) self.cursor_idx -= 1; - } - if (key.matches(Key.right, .{})) { + } else if (key.matches(Key.right, .{})) { if (self.cursor_idx < self.grapheme_count) self.cursor_idx += 1; + } else if (key.text) |text| { + try self.buf.insertSlice(self.byteOffsetToCursor(), text); + self.cursor_idx += 1; + self.grapheme_count += 1; } // TODO: readline bindings },