a modern tui library written in zig

Add BorderLabels to Border widget (#214)

* vxfw(Border): add BorderLabels to Border widget

* - changed enum names to snake_case.
- used Grapheme Iterator instead of old range based loop.
- used `ctx.stringWidth()` instead of `text.len`.
- added empty label guard.

* modified loop to use stringWidth instead of i range

* - Added trailing comma at the end of BorderLabel alignment enum definition.
- Changed from `.width=1` to `.width=width`

* - Added @intCast for width

* fix zig fmt errors

authored by Cosmic Predator and committed by rockorager.dev 4d118689 5f6c128b

verified
Changed files
+42
src
vxfw
+42
src/vxfw/Border.zig
··· 5 5 6 6 const vxfw = @import("vxfw.zig"); 7 7 8 + pub const BorderLabel = struct { 9 + text: []const u8, 10 + alignment: enum { 11 + top_left, 12 + top_center, 13 + top_right, 14 + bottom_left, 15 + bottom_center, 16 + bottom_right, 17 + }, 18 + }; 19 + 8 20 const Border = @This(); 9 21 10 22 child: vxfw.Widget, 11 23 style: vaxis.Style = .{}, 24 + labels: []const BorderLabel = &[_]BorderLabel{}, 12 25 13 26 pub fn widget(self: *const Border) vxfw.Widget { 14 27 return .{ ··· 65 78 surf.writeCell(0, row, .{ .char = .{ .grapheme = "│", .width = 1 }, .style = self.style }); 66 79 surf.writeCell(right_edge, row, .{ .char = .{ .grapheme = "│", .width = 1 }, .style = self.style }); 67 80 } 81 + 82 + // Add border labels 83 + for (self.labels) |label| { 84 + const text_len: u16 = @intCast(ctx.stringWidth(label.text)); 85 + if (text_len == 0) continue; 86 + 87 + const text_row: u16 = switch (label.alignment) { 88 + .top_left, .top_center, .top_right => 0, 89 + .bottom_left, .bottom_center, .bottom_right => bottom_edge, 90 + }; 91 + 92 + var text_col: u16 = switch (label.alignment) { 93 + .top_left, .bottom_left => 1, 94 + .top_center, .bottom_center => @max((size.width - text_len) / 2, 1), 95 + .top_right, .bottom_right => @max(size.width - 1 - text_len, 1), 96 + }; 97 + 98 + var iter = ctx.graphemeIterator(label.text); 99 + while (iter.next()) |grapheme| { 100 + const text = grapheme.bytes(label.text); 101 + const width: u16 = @intCast(ctx.stringWidth(text)); 102 + surf.writeCell(text_col, text_row, .{ 103 + .char = .{ .grapheme = text, .width = @intCast(width) }, 104 + .style = self.style, 105 + }); 106 + text_col += width; 107 + } 108 + } 109 + 68 110 return surf; 69 111 } 70 112