//! The rows around the prompt: the border above it, the border below it and the //! label that the top border shows while the agent works. const std = @import("std"); const vaxis = @import("vaxis"); const vxfw = vaxis.vxfw; const testing = std.testing; /// One row of `─` across the width. While the agent works the row also holds /// the spinner and its label. pub const Border = struct { spinner: ?*vxfw.Spinner = null, const working_label = " Working "; pub fn widget(self: *const Border) vxfw.Widget { return .{ .userdata = @constCast(self), .drawFn = typeErasedDrawFn, }; } fn typeErasedDrawFn(ptr: *anyopaque, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const self: *const Border = @ptrCast(@alignCast(ptr)); return self.draw(ctx); } pub fn draw(self: *const Border, 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 = 1 }); @memset(surface.buffer, .{ .char = .{ .grapheme = "─", .width = 1 } }); const spinner = self.spinner orelse return surface; // The spinner glyph and the label replace the line from column 2 on. surface.writeCell(1, 0, .{ .char = .{ .grapheme = " ", .width = 1 } }); var children: std.ArrayList(vxfw.SubSurface) = .empty; const spin = try spinner.draw(ctx.withConstraints( .{ .width = 1, .height = 1 }, .{ .width = 1, .height = 1 }, )); try children.append(ctx.arena, .{ .origin = .{ .row = 0, .col = 2 }, .surface = spin }); const label: *vxfw.Text = try ctx.arena.create(vxfw.Text); label.* = .{ .text = working_label, .softwrap = false, .width_basis = .longest_line }; const text = try label.draw(ctx.withConstraints( .{ .width = 0, .height = 1 }, .{ .width = width -| 3, .height = 1 }, )); try children.append(ctx.arena, .{ .origin = .{ .row = 0, .col = 3 }, .surface = text }); surface.children = children.items; return surface; } }; test "the border is a full row of box drawing with the spinner and label" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth); var spinner: vxfw.Spinner = .{ .io = testing.io }; _ = spinner.start(); const border: Border = .{ .spinner = &spinner }; const surface = try border.widget().draw(.{ .arena = arena, .min = .{ .width = 20, .height = 1 }, .max = .{ .width = 20, .height = 1 }, .cell_size = .{}, }); try testing.expectEqual(@as(u16, 1), surface.size.height); try testing.expectEqualStrings("─", surface.readCell(0, 0).char.grapheme); try testing.expectEqualStrings(" ", surface.readCell(1, 0).char.grapheme); try testing.expectEqualStrings("─", surface.readCell(19, 0).char.grapheme); // The spinner sits at column 2 and the label follows it. try testing.expectEqual(@as(usize, 2), surface.children.len); try testing.expectEqual(@as(u16, 2), @as(u16, @intCast(surface.children[0].origin.col))); try testing.expectEqualStrings("⣶", surface.children[0].surface.readCell(0, 0).char.grapheme); try testing.expectEqual(@as(u16, 3), @as(u16, @intCast(surface.children[1].origin.col))); try testing.expectEqualStrings("W", surface.children[1].surface.readCell(1, 0).char.grapheme); } test "a border without a spinner is a plain line" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth); const border: Border = .{}; const surface = try border.widget().draw(.{ .arena = arena, .min = .{ .width = 5, .height = 1 }, .max = .{ .width = 5, .height = 1 }, .cell_size = .{}, }); try testing.expectEqual(@as(usize, 0), surface.children.len); try testing.expectEqualStrings("─", surface.readCell(1, 0).char.grapheme); }