Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125//! The prompt text area: the wrapped editor rows that fit between the prompt//! borders, with the cursor placed on its row.
const std = @import("std");const vaxis = @import("vaxis");
const vxfw = vaxis.vxfw;
const editors = @import("../editor.zig");
const testing = std.testing;
pub const Prompt = @This();
editor: *editors.Editor,rows: u16,
pub fn widget(self: *const Prompt) vxfw.Widget { return .{ .userdata = @constCast(self), .drawFn = typeErasedDrawFn, };}
fn typeErasedDrawFn(ptr: *anyopaque, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const self: *const Prompt = @ptrCast(@alignCast(ptr)); return self.draw(ctx);}
pub fn draw(self: *const Prompt, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const width = ctx.max.width orelse return vxfw.Surface.empty(self.widget()); var surface = try vxfw.Surface.init(ctx.arena, self.widget(), .{ .width = width, .height = self.rows });
const rows = try self.editor.rows(ctx.arena, width); const text = self.editor.slice(); var row: u16 = 0; while (row < self.rows) : (row += 1) { const index = self.editor.scroll + row; if (index >= rows.len) { break; } const line = text[rows[index].start..rows[index].end]; var col: u16 = 0; var iter = ctx.graphemeIterator(line); while (iter.next()) |grapheme| { const bytes = grapheme.bytes(line); const glyph_width: u8 = @intCast(ctx.stringWidth(bytes)); surface.writeCell(col, row, .{ .char = .{ .grapheme = bytes, .width = glyph_width } }); col += glyph_width; } }
const cursor_row = editors.rowOf(rows, self.editor.cursor); if (cursor_row >= self.editor.scroll and cursor_row < self.editor.scroll + self.rows) { const line = rows[cursor_row]; surface.cursor = .{ .row = @intCast(cursor_row - self.editor.scroll), .col = @intCast(ctx.stringWidth(text[line.start..self.editor.cursor])), }; } return surface;}
test "the prompt draws the visible rows and places the cursor" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth);
var editor = editors.Editor.init(testing.allocator); defer editor.deinit(); for (0..20) |line| { try editor.insert("l"); try editor.insert(try std.fmt.allocPrint(arena, "{d}", .{line})); if (line < 19) { try editor.newline(); } }
const width: u16 = 20; const rows = try editor.height(arena, width); try testing.expectEqual(@as(usize, 8), rows);
const prompt: Prompt = .{ .editor = &editor, .rows = @intCast(rows) }; const surface = try prompt.widget().draw(.{ .arena = arena, .min = .{ .width = width, .height = @intCast(rows) }, .max = .{ .width = width, .height = @intCast(rows) }, .cell_size = .{}, });
try testing.expectEqual(@as(u16, width), surface.size.width); try testing.expectEqual(@as(u16, 8), surface.size.height); try testing.expectEqualStrings("l", surface.readCell(0, 0).char.grapheme); try testing.expectEqualStrings("2", surface.readCell(2, 0).char.grapheme); try testing.expectEqualStrings("9", surface.readCell(2, 7).char.grapheme); try testing.expectEqual(@as(u16, 7), surface.cursor.?.row); try testing.expectEqual(@as(u16, 3), surface.cursor.?.col);}
test "a row that fills the width at the end of the text moves the cursor to a new row" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth);
var editor = editors.Editor.init(testing.allocator); defer editor.deinit(); try editor.insert("abcd");
const width: u16 = 4; try testing.expectEqual(@as(usize, 2), try editor.height(arena, width)); const prompt: Prompt = .{ .editor = &editor, .rows = 2 }; const surface = try prompt.widget().draw(.{ .arena = arena, .min = .{ .width = width, .height = 2 }, .max = .{ .width = width, .height = 2 }, .cell_size = .{}, });
try testing.expectEqualStrings("d", surface.readCell(3, 0).char.grapheme); try testing.expectEqual(@as(u16, 1), surface.cursor.?.row); try testing.expectEqual(@as(u16, 0), surface.cursor.?.col);}