Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769const std = @import("std");const uucode = @import("uucode");const assert = std.debug.assert;const Key = @import("../Key.zig");const Cell = @import("../Cell.zig");const Window = @import("../Window.zig");const unicode = @import("../unicode.zig");
const TextInput = @This();
/// The events that this widget handlesconst Event = union(enum) { key_press: Key,};
const ellipsis: Cell.Character = .{ .grapheme = "…", .width = 1 };
// Index of our cursorbuf: Buffer,
/// the number of graphemes to skip when drawing. Used for horizontal scrollingdraw_offset: u16 = 0,/// the column we placed the cursor the last time we drewprev_cursor_col: u16 = 0,/// the grapheme index of the cursor the last time we drewprev_cursor_idx: u16 = 0,/// approximate distance from an edge before we scrollscroll_offset: u16 = 4,
pub fn init(alloc: std.mem.Allocator) TextInput { return TextInput{ .buf = Buffer.init(alloc), };}
pub fn deinit(self: *TextInput) void { self.buf.deinit();}
pub fn update(self: *TextInput, event: Event) !void { switch (event) { .key_press => |key| { if (key.matches(Key.backspace, .{})) { self.deleteBeforeCursor(); } else if (key.matches(Key.delete, .{}) or key.matches('d', .{ .ctrl = true })) { self.deleteAfterCursor(); } else if (key.matches(Key.left, .{}) or key.matches('b', .{ .ctrl = true })) { self.cursorLeft(); } else if (key.matches(Key.right, .{}) or key.matches('f', .{ .ctrl = true })) { self.cursorRight(); } else if (key.matches('a', .{ .ctrl = true }) or key.matches(Key.home, .{})) { self.buf.moveGapLeft(self.buf.firstHalf().len); } else if (key.matches('e', .{ .ctrl = true }) or key.matches(Key.end, .{})) { self.buf.moveGapRight(self.buf.secondHalf().len); } else if (key.matches('k', .{ .ctrl = true })) { self.deleteToEnd(); } else if (key.matches('u', .{ .ctrl = true })) { self.deleteToStart(); } else if (key.matches('b', .{ .alt = true }) or key.matches(Key.left, .{ .alt = true })) { self.moveBackwardWordwise(); } else if (key.matches('f', .{ .alt = true }) or key.matches(Key.right, .{ .alt = true })) { self.moveForwardWordwise(); } else if (key.matches(Key.backspace, .{ .alt = true })) { self.deleteWordBefore(); } else if (key.matches('w', .{ .ctrl = true })) { self.deleteWordBeforeWhitespace(); } else if (key.matches('d', .{ .alt = true })) { self.deleteWordAfter(); } else if (key.text) |text| { try self.insertSliceAtCursor(text); } }, }}
/// insert text at the cursor positionpub fn insertSliceAtCursor(self: *TextInput, data: []const u8) std.mem.Allocator.Error!void { var iter = unicode.graphemeIterator(data); while (iter.next()) |text| { try self.buf.insertSliceAtCursor(text.bytes(data)); }}
pub fn sliceToCursor(self: *TextInput, buf: []u8) []const u8 { assert(buf.len >= self.buf.cursor); @memcpy(buf[0..self.buf.cursor], self.buf.firstHalf()); return buf[0..self.buf.cursor];}
/// Copies the complete logical contents without exposing the gap-buffer layout./// The caller owns the returned slice.pub fn toOwnedContents(self: *const TextInput, alloc: std.mem.Allocator) std.mem.Allocator.Error![]u8 { const first = self.buf.firstHalf(); const second = self.buf.secondHalf(); const out = try alloc.alloc(u8, first.len + second.len); @memcpy(out[0..first.len], first); @memcpy(out[first.len..], second); return out;}
/// calculates the display width from the draw_offset to the cursorpub fn widthToCursor(self: *TextInput, win: Window) u16 { var width: u16 = 0; const first_half = self.buf.firstHalf(); var first_iter = unicode.graphemeIterator(first_half); var i: usize = 0; while (first_iter.next()) |grapheme| { defer i += 1; if (i < self.draw_offset) { continue; } const g = grapheme.bytes(first_half); width += win.gwidth(g); } return width;}
pub fn cursorLeft(self: *TextInput) void { // We need to find the size of the last grapheme in the first half var iter = unicode.graphemeIterator(self.buf.firstHalf()); var len: usize = 0; while (iter.next()) |grapheme| { len = grapheme.len; } self.buf.moveGapLeft(len);}
pub fn cursorRight(self: *TextInput) void { var iter = unicode.graphemeIterator(self.buf.secondHalf()); const grapheme = iter.next() orelse return; self.buf.moveGapRight(grapheme.len);}
pub fn graphemesBeforeCursor(self: *const TextInput) u16 { const first_half = self.buf.firstHalf(); var first_iter = unicode.graphemeIterator(first_half); var i: u16 = 0; while (first_iter.next()) |_| { i += 1; } return i;}
pub fn draw(self: *TextInput, win: Window) void { self.drawWithStyle(win, .{});}
pub fn drawWithStyle(self: *TextInput, win: Window, style: Cell.Style) void { const cursor_idx = self.graphemesBeforeCursor(); if (cursor_idx < self.draw_offset) self.draw_offset = cursor_idx; if (win.width == 0) return; while (true) { const width = self.widthToCursor(win); if (width >= win.width) { self.draw_offset +|= width - win.width + 1; continue; } else break; }
self.prev_cursor_idx = cursor_idx; self.prev_cursor_col = 0;
// assumption!! the gap is never within a grapheme // one way to _ensure_ this is to move the gap... but that's a cost we probably don't want to pay. const first_half = self.buf.firstHalf(); var first_iter = unicode.graphemeIterator(first_half); var col: u16 = 0; var i: u16 = 0; while (first_iter.next()) |grapheme| { if (i < self.draw_offset) { i += 1; continue; } const g = grapheme.bytes(first_half); const w = win.gwidth(g); if (col + w >= win.width) { win.writeCell(win.width - 1, 0, .{ .char = ellipsis, .style = style, }); break; } win.writeCell(col, 0, .{ .char = .{ .grapheme = g, .width = @intCast(w), }, .style = style, }); col += w; i += 1; if (i == cursor_idx) self.prev_cursor_col = col; } const second_half = self.buf.secondHalf(); var second_iter = unicode.graphemeIterator(second_half); while (second_iter.next()) |grapheme| { if (i < self.draw_offset) { i += 1; continue; } const g = grapheme.bytes(second_half); const w = win.gwidth(g); if (col + w > win.width) { win.writeCell(win.width - 1, 0, .{ .char = ellipsis, .style = style, }); break; } win.writeCell(col, 0, .{ .char = .{ .grapheme = g, .width = @intCast(w), }, .style = style, }); col += w; i += 1; if (i == cursor_idx) self.prev_cursor_col = col; } if (self.draw_offset > 0) { win.writeCell(0, 0, .{ .char = ellipsis, .style = style, }); } win.showCursor(self.prev_cursor_col, 0);}
pub fn clearAndFree(self: *TextInput) void { self.buf.clearAndFree(); self.reset();}
pub fn clearRetainingCapacity(self: *TextInput) void { self.buf.clearRetainingCapacity(); self.reset();}
pub fn toOwnedSlice(self: *TextInput) ![]const u8 { defer self.reset(); return self.buf.toOwnedSlice();}
pub fn reset(self: *TextInput) void { self.draw_offset = 0; self.prev_cursor_col = 0; self.prev_cursor_idx = 0;}
// returns the number of bytes before the cursorpub fn byteOffsetToCursor(self: TextInput) usize { return self.buf.cursor;}
pub fn deleteToEnd(self: *TextInput) void { self.buf.growGapRight(self.buf.secondHalf().len);}
pub fn deleteToStart(self: *TextInput) void { self.buf.growGapLeft(self.buf.cursor);}
pub fn deleteBeforeCursor(self: *TextInput) void { // We need to find the size of the last grapheme in the first half var iter = unicode.graphemeIterator(self.buf.firstHalf()); var len: usize = 0; while (iter.next()) |grapheme| { len = grapheme.len; } self.buf.growGapLeft(len);}
pub fn deleteAfterCursor(self: *TextInput) void { var iter = unicode.graphemeIterator(self.buf.secondHalf()); const grapheme = iter.next() orelse return; self.buf.growGapRight(grapheme.len);}
const DecodedCodepoint = struct { cp: u21, start: usize, len: usize,};
fn decodeCodepointAt(bytes: []const u8, start: usize) DecodedCodepoint { const first = bytes[start]; const len = std.unicode.utf8ByteSequenceLength(first) catch 1; const capped_len = @min(len, bytes.len - start); const slice = bytes[start .. start + capped_len]; const cp = std.unicode.utf8Decode(slice) catch { return .{ .cp = first, .start = start, .len = 1 }; }; return .{ .cp = cp, .start = start, .len = capped_len };}
fn isUtf8ContinuationByte(c: u8) bool { return (c & 0b1100_0000) == 0b1000_0000;}
fn decodeCodepointBefore(bytes: []const u8, end: usize) DecodedCodepoint { var start = end - 1; while (start > 0 and isUtf8ContinuationByte(bytes[start])) : (start -= 1) {} const slice = bytes[start..end]; const cp = std.unicode.utf8Decode(slice) catch { return .{ .cp = bytes[end - 1], .start = end - 1, .len = 1 }; }; return .{ .cp = cp, .start = start, .len = end - start };}
/// Returns true if the codepoint is a readline-style word constituent.fn isWordCodepoint(cp: u21) bool { if (cp == '_') return true; return switch (uucode.get(.general_category, cp)) { .letter_uppercase, .letter_lowercase, .letter_titlecase, .letter_modifier, .letter_other, .number_decimal_digit, .number_letter, .number_other, .mark_nonspacing, .mark_spacing_combining, .mark_enclosing, .punctuation_connector, => true, else => false, };}
fn isWhitespaceCodepoint(cp: u21) bool { return switch (cp) { ' ', '\t', '\n', '\r', 0x0b, 0x0c, 0x85 => true, else => switch (uucode.get(.general_category, cp)) { .separator_space, .separator_line, .separator_paragraph, => true, else => false, }, };}
/// Moves the cursor backward by one word using character-class boundaries./// Skips non-word characters, then skips word characters (matching readline backward-word).pub fn moveBackwardWordwise(self: *TextInput) void { const first_half = self.buf.firstHalf(); var i: usize = first_half.len; // Skip non-word characters while (i > 0) { const decoded = decodeCodepointBefore(first_half, i); if (isWordCodepoint(decoded.cp)) break; i = decoded.start; } // Skip word characters while (i > 0) { const decoded = decodeCodepointBefore(first_half, i); if (!isWordCodepoint(decoded.cp)) break; i = decoded.start; } self.buf.moveGapLeft(self.buf.cursor - i);}
/// Moves the cursor forward by one word using character-class boundaries./// Skips non-word characters, then skips word characters — landing at the end of the next word/// (matching readline forward-word).pub fn moveForwardWordwise(self: *TextInput) void { const second_half = self.buf.secondHalf(); var i: usize = 0; // Skip non-word characters while (i < second_half.len) { const decoded = decodeCodepointAt(second_half, i); if (isWordCodepoint(decoded.cp)) break; i += decoded.len; } // Skip word characters while (i < second_half.len) { const decoded = decodeCodepointAt(second_half, i); if (!isWordCodepoint(decoded.cp)) break; i += decoded.len; } self.buf.moveGapRight(i);}
/// Deletes the word before the cursor using character-class boundaries/// (matching readline backward-kill-word / Alt+Backspace).pub fn deleteWordBefore(self: *TextInput) void { const pre = self.buf.cursor; self.moveBackwardWordwise(); self.buf.growGapRight(pre - self.buf.cursor);}
/// Deletes the word before the cursor using whitespace boundaries/// (matching readline unix-word-rubout / Ctrl+W).pub fn deleteWordBeforeWhitespace(self: *TextInput) void { const first_half = self.buf.firstHalf(); var i: usize = first_half.len; // Skip trailing whitespace while (i > 0) { const decoded = decodeCodepointBefore(first_half, i); if (!isWhitespaceCodepoint(decoded.cp)) break; i = decoded.start; } // Skip non-whitespace while (i > 0) { const decoded = decodeCodepointBefore(first_half, i); if (isWhitespaceCodepoint(decoded.cp)) break; i = decoded.start; } const to_delete = self.buf.cursor - i; self.buf.moveGapLeft(to_delete); self.buf.growGapRight(to_delete);}
/// Deletes the word after the cursor using character-class boundaries/// (matching readline kill-word / Alt+D).pub fn deleteWordAfter(self: *TextInput) void { const second_half = self.buf.secondHalf(); var i: usize = 0; // Skip non-word characters while (i < second_half.len) { const decoded = decodeCodepointAt(second_half, i); if (isWordCodepoint(decoded.cp)) break; i += decoded.len; } // Skip word characters while (i < second_half.len) { const decoded = decodeCodepointAt(second_half, i); if (!isWordCodepoint(decoded.cp)) break; i += decoded.len; } self.buf.growGapRight(i);}
test "assertion" { const astronaut = "👩🚀"; const astronaut_emoji: Key = .{ .text = astronaut, .codepoint = try std.unicode.utf8Decode(astronaut[0..4]), }; var input = TextInput.init(std.testing.allocator); defer input.deinit(); for (0..6) |_| { try input.update(.{ .key_press = astronaut_emoji }); }}
test "sliceToCursor" { var input = init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello, world"); input.cursorLeft(); input.cursorLeft(); input.cursorLeft(); var buf: [32]u8 = undefined; try std.testing.expectEqualStrings("hello, wo", input.sliceToCursor(&buf)); input.cursorRight(); try std.testing.expectEqualStrings("hello, wor", input.sliceToCursor(&buf));}
pub const Buffer = struct { allocator: std.mem.Allocator, buffer: []u8, cursor: usize, gap_size: usize,
pub fn init(allocator: std.mem.Allocator) Buffer { return .{ .allocator = allocator, .buffer = &.{}, .cursor = 0, .gap_size = 0, }; }
pub fn deinit(self: *Buffer) void { self.allocator.free(self.buffer); }
pub fn firstHalf(self: Buffer) []const u8 { return self.buffer[0..self.cursor]; }
pub fn secondHalf(self: Buffer) []const u8 { return self.buffer[self.cursor + self.gap_size ..]; }
pub fn grow(self: *Buffer, n: usize) std.mem.Allocator.Error!void { // Always grow by 512 bytes const new_size = self.buffer.len + n + 512; // Allocate the new memory const new_memory = try self.allocator.alloc(u8, new_size); // Copy the first half @memcpy(new_memory[0..self.cursor], self.firstHalf()); // Copy the second half const second_half = self.secondHalf(); @memcpy(new_memory[new_size - second_half.len ..], second_half); self.allocator.free(self.buffer); self.buffer = new_memory; self.gap_size = new_size - second_half.len - self.cursor; }
pub fn insertSliceAtCursor(self: *Buffer, slice: []const u8) std.mem.Allocator.Error!void { if (slice.len == 0) return; if (self.gap_size <= slice.len) try self.grow(slice.len); @memcpy(self.buffer[self.cursor .. self.cursor + slice.len], slice); self.cursor += slice.len; self.gap_size -= slice.len; }
/// Move the gap n bytes to the left pub fn moveGapLeft(self: *Buffer, n: usize) void { const new_idx = self.cursor -| n; const dst = self.buffer[new_idx + self.gap_size ..]; const src = self.buffer[new_idx..self.cursor]; std.mem.copyForwards(u8, dst, src); self.cursor = new_idx; }
pub fn moveGapRight(self: *Buffer, n: usize) void { const new_idx = self.cursor + n; const dst = self.buffer[self.cursor..]; const src = self.buffer[self.cursor + self.gap_size .. new_idx + self.gap_size]; std.mem.copyForwards(u8, dst, src); self.cursor = new_idx; }
/// grow the gap by moving the cursor n bytes to the left pub fn growGapLeft(self: *Buffer, n: usize) void { // gap grows by the delta self.gap_size += n; self.cursor -|= n; }
/// grow the gap by removing n bytes after the cursor pub fn growGapRight(self: *Buffer, n: usize) void { self.gap_size = @min(self.gap_size + n, self.buffer.len - self.cursor); }
pub fn clearAndFree(self: *Buffer) void { self.cursor = 0; self.allocator.free(self.buffer); self.buffer = &.{}; self.gap_size = 0; }
pub fn clearRetainingCapacity(self: *Buffer) void { self.cursor = 0; self.gap_size = self.buffer.len; }
pub fn toOwnedSlice(self: *Buffer) std.mem.Allocator.Error![]const u8 { const first_half = self.firstHalf(); const second_half = self.secondHalf(); const buf = try self.allocator.alloc(u8, first_half.len + second_half.len); @memcpy(buf[0..first_half.len], first_half); @memcpy(buf[first_half.len..], second_half); self.clearAndFree(); return buf; }
pub fn realLength(self: *const Buffer) usize { return self.firstHalf().len + self.secondHalf().len; }};
test "moveBackwardWordwise stops at word boundary" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello-world"); // Cursor is at end: "hello-world|" input.moveBackwardWordwise(); // Should stop at start of "world": "hello-|world" try std.testing.expectEqualStrings("hello-", input.buf.firstHalf()); try std.testing.expectEqualStrings("world", input.buf.secondHalf()); input.moveBackwardWordwise(); // Should skip "-" and stop at start of "hello": "|hello-world" try std.testing.expectEqualStrings("", input.buf.firstHalf()); try std.testing.expectEqualStrings("hello-world", input.buf.secondHalf());}
test "moveForwardWordwise stops at end of word" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello-world"); // Move cursor to start input.buf.moveGapLeft(input.buf.firstHalf().len); // Cursor at start: "|hello-world" input.moveForwardWordwise(); // Should stop at end of "hello": "hello|-world" try std.testing.expectEqualStrings("hello", input.buf.firstHalf()); try std.testing.expectEqualStrings("-world", input.buf.secondHalf()); input.moveForwardWordwise(); // Should skip "-" then stop at end of "world": "hello-world|" try std.testing.expectEqualStrings("hello-world", input.buf.firstHalf()); try std.testing.expectEqualStrings("", input.buf.secondHalf());}
test "moveBackwardWordwise with path separators" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("/usr/local/bin"); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("/usr/local/", input.buf.firstHalf()); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("/usr/", input.buf.firstHalf()); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("/", input.buf.firstHalf()); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("", input.buf.firstHalf());}
test "moveForwardWordwise with dots" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("foo.bar.baz"); input.buf.moveGapLeft(input.buf.firstHalf().len); input.moveForwardWordwise(); // Stops at end of "foo": "foo|.bar.baz" try std.testing.expectEqualStrings("foo", input.buf.firstHalf()); input.moveForwardWordwise(); // Skips "." then stops at end of "bar": "foo.bar|.baz" try std.testing.expectEqualStrings("foo.bar", input.buf.firstHalf()); input.moveForwardWordwise(); // Skips "." then stops at end of "baz": "foo.bar.baz|" try std.testing.expectEqualStrings("foo.bar.baz", input.buf.firstHalf());}
test "deleteWordBefore with hyphens" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello-world"); input.deleteWordBefore(); // Should delete "world" only: "hello-|" try std.testing.expectEqualStrings("hello-", input.buf.firstHalf()); try std.testing.expectEqualStrings("", input.buf.secondHalf()); input.deleteWordBefore(); // Should skip "-" and delete "hello": "|" try std.testing.expectEqualStrings("", input.buf.firstHalf());}
test "deleteWordBeforeWhitespace deletes to whitespace" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello-world foo.bar"); input.deleteWordBeforeWhitespace(); // Should delete "foo.bar" (entire whitespace-delimited word) try std.testing.expectEqualStrings("hello-world ", input.buf.firstHalf()); input.deleteWordBeforeWhitespace(); // Should delete " hello-world" try std.testing.expectEqualStrings("", input.buf.firstHalf());}
test "deleteWordAfter with mixed punctuation" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("foo.bar baz"); input.buf.moveGapLeft(input.buf.firstHalf().len); input.deleteWordAfter(); // kill-word: skip non-word (none), skip word "foo" → delete "foo" try std.testing.expectEqualStrings("", input.buf.firstHalf()); try std.testing.expectEqualStrings(".bar baz", input.buf.secondHalf()); input.deleteWordAfter(); // kill-word: skip "." (non-word), skip "bar" (word) → delete ".bar" try std.testing.expectEqualStrings("", input.buf.firstHalf()); try std.testing.expectEqualStrings(" baz", input.buf.secondHalf());}
test "word motion with underscores treats them as word chars" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello_world-test"); input.moveBackwardWordwise(); // "test" is a word, should stop before it: "hello_world-|test" try std.testing.expectEqualStrings("hello_world-", input.buf.firstHalf()); input.moveBackwardWordwise(); // "hello_world" is one word (underscore is word char): "|hello_world-test" try std.testing.expectEqualStrings("", input.buf.firstHalf());}
test "word motion with non-ASCII text" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); // "café-latte" — the é is multi-byte UTF-8, should not split inside it try input.insertSliceAtCursor("café-latte"); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("café-", input.buf.firstHalf()); try std.testing.expectEqualStrings("latte", input.buf.secondHalf()); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("", input.buf.firstHalf());
// Forward from start input.moveForwardWordwise(); // Should stop at end of "café" try std.testing.expectEqualStrings("caf\xc3\xa9", input.buf.firstHalf()); try std.testing.expectEqualStrings("-latte", input.buf.secondHalf());}
test "non-ASCII punctuation acts as a separator" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello\u{2014}world"); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("hello\u{2014}", input.buf.firstHalf()); try std.testing.expectEqualStrings("world", input.buf.secondHalf());
input.buf.moveGapLeft(input.buf.firstHalf().len); input.moveForwardWordwise(); try std.testing.expectEqualStrings("hello", input.buf.firstHalf()); try std.testing.expectEqualStrings("\u{2014}world", input.buf.secondHalf());}
test "deleteWordBeforeWhitespace handles unicode whitespace" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello\u{3000}world"); input.deleteWordBeforeWhitespace(); try std.testing.expectEqualStrings("hello\u{3000}", input.buf.firstHalf()); try std.testing.expectEqualStrings("", input.buf.secondHalf());}
test "deleteWordBefore with non-ASCII text" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("über-cool"); input.deleteWordBefore(); try std.testing.expectEqualStrings("über-", input.buf.firstHalf()); input.deleteWordBefore(); try std.testing.expectEqualStrings("", input.buf.firstHalf());}
test "word motion with spaces" { var input = TextInput.init(std.testing.allocator); defer input.deinit(); try input.insertSliceAtCursor("hello world"); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("hello ", input.buf.firstHalf()); input.moveBackwardWordwise(); try std.testing.expectEqualStrings("", input.buf.firstHalf());}
test "TextInput.zig: Buffer" { var gap_buf = Buffer.init(std.testing.allocator); defer gap_buf.deinit();
try gap_buf.insertSliceAtCursor("abc"); try std.testing.expectEqualStrings("abc", gap_buf.firstHalf()); try std.testing.expectEqualStrings("", gap_buf.secondHalf());
gap_buf.moveGapLeft(1); try std.testing.expectEqualStrings("ab", gap_buf.firstHalf()); try std.testing.expectEqualStrings("c", gap_buf.secondHalf());
try gap_buf.insertSliceAtCursor(" "); try std.testing.expectEqualStrings("ab ", gap_buf.firstHalf()); try std.testing.expectEqualStrings("c", gap_buf.secondHalf());
gap_buf.growGapLeft(1); try std.testing.expectEqualStrings("ab", gap_buf.firstHalf()); try std.testing.expectEqualStrings("c", gap_buf.secondHalf()); try std.testing.expectEqual(2, gap_buf.cursor);
gap_buf.growGapRight(1); try std.testing.expectEqualStrings("ab", gap_buf.firstHalf()); try std.testing.expectEqualStrings("", gap_buf.secondHalf()); try std.testing.expectEqual(2, gap_buf.cursor);}