this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

all!: reduce usage of usize

+193 -189
+1 -1
examples/cli.zig
··· 92 92 .text = opt, 93 93 .style = if (j == i) .{ .reverse = true } else .{}, 94 94 }}; 95 - _ = win.print(&seg, .{ .row_offset = j + 1 }); 95 + _ = win.print(&seg, .{ .row_offset = @intCast(j + 1) }); 96 96 } 97 97 } 98 98 try vx.render(tty.anyWriter());
+1 -1
examples/image.zig
··· 47 47 48 48 var n: usize = 0; 49 49 50 - var clip_y: usize = 0; 50 + var clip_y: u16 = 0; 51 51 52 52 while (true) { 53 53 const event = loop.nextEvent();
+3 -2
examples/main.zig
··· 67 67 // the old and only updated cells will be drawn 68 68 win.clear(); 69 69 70 + const msg_len: u16 = @intCast(msg.len); 70 71 // Create some child window. .expand means the height and width will 71 72 // fill the remaining space of the parent. Child windows do not store a 72 73 // reference to their parent: this is true immediate mode. Do not store 73 74 // windows, always create new windows each render cycle 74 75 const child = win.child( 75 - .{ .x_off = win.width / 2 - msg.len / 2, .y_off = win.height / 2 }, 76 + .{ .x_off = win.width / 2 - msg_len / 2, .y_off = win.height / 2 }, 76 77 ); 77 78 // Loop through the message and print the cells to the screen 78 79 for (msg, 0..) |_, i| { ··· 86 87 .fg = .{ .index = color_idx }, 87 88 }, 88 89 }; 89 - child.writeCell(i, 0, cell); 90 + child.writeCell(@intCast(i), 0, cell); 90 91 } 91 92 // Render the screen 92 93 try vx.render(tty.anyWriter());
+5 -5
examples/table.zig
··· 155 155 // Select/Unselect Row 156 156 if (key.matches(vaxis.Key.space, .{})) { 157 157 const rows = demo_tbl.sel_rows orelse createRows: { 158 - demo_tbl.sel_rows = try alloc.alloc(usize, 1); 158 + demo_tbl.sel_rows = try alloc.alloc(u16, 1); 159 159 break :createRows demo_tbl.sel_rows.?; 160 160 }; 161 - var rows_list = std.ArrayList(usize).fromOwnedSlice(alloc, rows); 161 + var rows_list = std.ArrayList(u16).fromOwnedSlice(alloc, rows); 162 162 for (rows_list.items, 0..) |row, idx| { 163 163 if (row != demo_tbl.row) continue; 164 164 _ = rows_list.orderedRemove(idx); ··· 179 179 mem.eql(u8, ":quit", cmd) or 180 180 mem.eql(u8, ":exit", cmd)) return; 181 181 if (mem.eql(u8, "G", cmd)) { 182 - demo_tbl.row = user_list.items.len - 1; 182 + demo_tbl.row = @intCast(user_list.items.len - 1); 183 183 active = .mid; 184 184 } 185 185 if (cmd.len >= 2 and mem.eql(u8, "gg", cmd[0..2])) { 186 - const goto_row = fmt.parseInt(usize, cmd[2..], 0) catch 0; 186 + const goto_row = fmt.parseInt(u16, cmd[2..], 0) catch 0; 187 187 demo_tbl.row = goto_row; 188 188 active = .mid; 189 189 } ··· 213 213 }; 214 214 demo_tbl.active_ctx = &row_ctx; 215 215 demo_tbl.active_content_fn = struct { 216 - fn see(win: *vaxis.Window, ctx_raw: *const anyopaque) !usize { 216 + fn see(win: *vaxis.Window, ctx_raw: *const anyopaque) !u16 { 217 217 const ctx: *const RowContext = @alignCast(@ptrCast(ctx_raw)); 218 218 win.height = 5; 219 219 const see_win = win.child(.{
+8 -8
examples/view.zig
··· 29 29 var use_sm_map = false; 30 30 var use_mini_view = false; 31 31 32 - var x: usize = 0; 33 - var y: usize = 0; 34 - var h: usize = 0; 35 - var w: usize = 0; 32 + var x: u16 = 0; 33 + var y: u16 = 0; 34 + var h: u16 = 0; 35 + var w: u16 = 0; 36 36 defer log.info( 37 37 \\Results: 38 38 \\ - Map Size: {d}x{d} ··· 351 351 \\ +SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS+ 352 352 \\ 353 353 ; 354 - const lg_map_width = mapWidth: { 354 + const lg_map_width: u16 = mapWidth: { 355 355 @setEvalBranchQuota(100_000); 356 - break :mapWidth mem.indexOfScalar(u8, lg_world_map, '\n').?; 356 + break :mapWidth @intCast(mem.indexOfScalar(u8, lg_world_map, '\n').?); 357 357 }; 358 - const lg_map_height = mapHeight: { 358 + const lg_map_height: u16 = mapHeight: { 359 359 @setEvalBranchQuota(100_000); 360 - break :mapHeight mem.count(u8, lg_world_map, "\n"); 360 + break :mapHeight @intCast(mem.count(u8, lg_world_map, "\n")); 361 361 };
+16 -16
src/Image.zig
··· 33 33 }; 34 34 35 35 pub const CellSize = struct { 36 - rows: usize, 37 - cols: usize, 36 + rows: u16, 37 + cols: u16, 38 38 }; 39 39 40 40 pub const DrawOptions = struct { ··· 42 42 /// origin of the image. These must be less than the pixel size of a single 43 43 /// cell 44 44 pixel_offset: ?struct { 45 - x: usize, 46 - y: usize, 45 + x: u16, 46 + y: u16, 47 47 } = null, 48 48 /// the vertical stacking order 49 49 /// < 0: Drawn beneath text ··· 51 51 z_index: ?i32 = null, 52 52 /// A clip region of the source image to draw. 53 53 clip_region: ?struct { 54 - x: ?usize = null, 55 - y: ?usize = null, 56 - width: ?usize = null, 57 - height: ?usize = null, 54 + x: ?u16 = null, 55 + y: ?u16 = null, 56 + width: ?u16 = null, 57 + height: ?u16 = null, 58 58 } = null, 59 59 /// Scaling to apply to the Image 60 60 scale: enum { ··· 71 71 /// field, and should prefer to use scale. `draw` will fill in this field with 72 72 /// the correct values if a scale method is applied. 73 73 size: ?struct { 74 - rows: ?usize = null, 75 - cols: ?usize = null, 74 + rows: ?u16 = null, 75 + cols: ?u16 = null, 76 76 } = null, 77 77 }; 78 78 ··· 80 80 id: u32, 81 81 82 82 /// width in pixels 83 - width: usize, 83 + width: u16, 84 84 /// height in pixels 85 - height: usize, 85 + height: u16, 86 86 87 87 pub fn draw(self: Image, win: Window, opts: DrawOptions) !void { 88 88 var p_opts = opts; ··· 176 176 const w = win.screen.width; 177 177 const h = win.screen.height; 178 178 179 - const pix_per_col = try std.math.divCeil(usize, x_pix, w); 180 - const pix_per_row = try std.math.divCeil(usize, y_pix, h); 179 + const pix_per_col = try std.math.divCeil(u16, x_pix, w); 180 + const pix_per_row = try std.math.divCeil(u16, y_pix, h); 181 181 182 - const cell_width = std.math.divCeil(usize, self.width, pix_per_col) catch 0; 183 - const cell_height = std.math.divCeil(usize, self.height, pix_per_row) catch 0; 182 + const cell_width = std.math.divCeil(u16, self.width, pix_per_col) catch 0; 183 + const cell_height = std.math.divCeil(u16, self.height, pix_per_row) catch 0; 184 184 return .{ 185 185 .rows = cell_height, 186 186 .cols = cell_width,
+8 -8
src/InternalScreen.zig
··· 32 32 } 33 33 }; 34 34 35 - width: usize = 0, 36 - height: usize = 0, 35 + width: u16 = 0, 36 + height: u16 = 0, 37 37 38 38 buf: []InternalCell = undefined, 39 39 40 - cursor_row: usize = 0, 41 - cursor_col: usize = 0, 40 + cursor_row: u16 = 0, 41 + cursor_col: u16 = 0, 42 42 cursor_vis: bool = false, 43 43 cursor_shape: CursorShape = .default, 44 44 45 45 mouse_shape: MouseShape = .default, 46 46 47 47 /// sets each cell to the default cell 48 - pub fn init(alloc: std.mem.Allocator, w: usize, h: usize) !InternalScreen { 48 + pub fn init(alloc: std.mem.Allocator, w: u16, h: u16) !InternalScreen { 49 49 var screen = InternalScreen{ 50 50 .buf = try alloc.alloc(InternalCell, w * h), 51 51 }; ··· 75 75 /// writes a cell to a location. 0 indexed 76 76 pub fn writeCell( 77 77 self: *InternalScreen, 78 - col: usize, 79 - row: usize, 78 + col: u16, 79 + row: u16, 80 80 cell: Cell, 81 81 ) void { 82 82 if (self.width < col) { ··· 105 105 self.buf[i].default = cell.default; 106 106 } 107 107 108 - pub fn readCell(self: *InternalScreen, col: usize, row: usize) ?Cell { 108 + pub fn readCell(self: *InternalScreen, col: u16, row: u16) ?Cell { 109 109 if (self.width < col) { 110 110 // column out of bounds 111 111 return null;
+4 -4
src/Mouse.zig
··· 41 41 drag, 42 42 }; 43 43 44 - col: usize, 45 - row: usize, 46 - xoffset: usize = 0, 47 - yoffset: usize = 0, 44 + col: u16, 45 + row: u16, 46 + xoffset: u16 = 0, 47 + yoffset: u16 = 0, 48 48 button: Button, 49 49 mods: Modifiers, 50 50 type: Type,
+4 -4
src/Parser.zig
··· 522 522 const width_pix = iter.next() orelse "0"; 523 523 524 524 const winsize: Winsize = .{ 525 - .rows = std.fmt.parseUnsigned(usize, height_char, 10) catch return null_event, 526 - .cols = std.fmt.parseUnsigned(usize, width_char, 10) catch return null_event, 527 - .x_pixel = std.fmt.parseUnsigned(usize, width_pix, 10) catch return null_event, 528 - .y_pixel = std.fmt.parseUnsigned(usize, height_pix, 10) catch return null_event, 525 + .rows = std.fmt.parseUnsigned(u16, height_char, 10) catch return null_event, 526 + .cols = std.fmt.parseUnsigned(u16, width_char, 10) catch return null_event, 527 + .x_pixel = std.fmt.parseUnsigned(u16, width_pix, 10) catch return null_event, 528 + .y_pixel = std.fmt.parseUnsigned(u16, height_pix, 10) catch return null_event, 529 529 }; 530 530 return .{ 531 531 .event = .{ .winsize = winsize },
+8 -8
src/Screen.zig
··· 10 10 11 11 const Screen = @This(); 12 12 13 - width: usize = 0, 14 - height: usize = 0, 13 + width: u16 = 0, 14 + height: u16 = 0, 15 15 16 - width_pix: usize = 0, 17 - height_pix: usize = 0, 16 + width_pix: u16 = 0, 17 + height_pix: u16 = 0, 18 18 19 19 buf: []Cell = undefined, 20 20 21 - cursor_row: usize = 0, 22 - cursor_col: usize = 0, 21 + cursor_row: u16 = 0, 22 + cursor_col: u16 = 0, 23 23 cursor_vis: bool = false, 24 24 25 25 unicode: *const Unicode = undefined, ··· 49 49 } 50 50 51 51 /// writes a cell to a location. 0 indexed 52 - pub fn writeCell(self: *Screen, col: usize, row: usize, cell: Cell) void { 52 + pub fn writeCell(self: *Screen, col: u16, row: u16, cell: Cell) void { 53 53 if (self.width <= col) { 54 54 // column out of bounds 55 55 return; ··· 63 63 self.buf[i] = cell; 64 64 } 65 65 66 - pub fn readCell(self: *const Screen, col: usize, row: usize) ?Cell { 66 + pub fn readCell(self: *const Screen, col: u16, row: u16) ?Cell { 67 67 if (self.width <= col) { 68 68 // column out of bounds 69 69 return null;
+24 -20
src/Vaxis.zig
··· 23 23 const ctlseqs = @import("ctlseqs.zig"); 24 24 const gwidth = @import("gwidth.zig"); 25 25 26 + const assert = std.debug.assert; 27 + 26 28 const Vaxis = @This(); 27 29 28 30 const log = std.log.scoped(.vaxis); ··· 84 86 changed_default_fg: bool = false, 85 87 changed_default_bg: bool = false, 86 88 cursor: struct { 87 - row: usize = 0, 88 - col: usize = 0, 89 + row: u16 = 0, 90 + col: u16 = 0, 89 91 } = .{}, 90 92 } = .{}, 91 93 ··· 139 141 try self.exitAltScreen(tty); 140 142 } else { 141 143 try tty.writeByte('\r'); 142 - var i: usize = 0; 144 + var i: u16 = 0; 143 145 while (i < self.state.cursor.row) : (i += 1) { 144 146 try tty.writeAll(ctlseqs.ri); 145 147 } ··· 308 310 /// draws the screen to the terminal 309 311 pub fn render(self: *Vaxis, tty: AnyWriter) !void { 310 312 defer self.refresh = false; 313 + assert(self.screen.buf.len == self.screen.width * self.screen.height); // correct size 314 + assert(self.screen.buf.len == self.screen_last.buf.len); // same size 311 315 312 316 // Set up sync before we write anything 313 317 // TODO: optimize sync so we only sync _when we have changes_. This ··· 331 335 332 336 // initialize some variables 333 337 var reposition: bool = false; 334 - var row: usize = 0; 335 - var col: usize = 0; 338 + var row: u16 = 0; 339 + var col: u16 = 0; 336 340 var cursor: Style = .{}; 337 341 var link: Hyperlink = .{}; 338 342 var cursor_pos: struct { 339 - row: usize = 0, 340 - col: usize = 0, 343 + row: u16 = 0, 344 + col: u16 = 0, 341 345 } = .{}; 342 346 343 347 // Clear all images 344 348 if (self.caps.kitty_graphics) 345 349 try tty.writeAll(ctlseqs.kitty_graphics_clear); 346 350 347 - var i: usize = 0; 351 + var i: u16 = 0; 348 352 while (i < self.screen.buf.len) { 349 353 const cell = self.screen.buf[i]; 350 - const w = blk: { 354 + const w: u16 = blk: { 351 355 if (cell.char.width != 0) break :blk cell.char.width; 352 356 353 357 const method: gwidth.Method = self.caps.unicode; 354 - const width = gwidth.gwidth(cell.char.grapheme, method, &self.unicode.width_data); 358 + const width: u16 = @intCast(gwidth.gwidth(cell.char.grapheme, method, &self.unicode.width_data)); 355 359 break :blk @max(1, width); 356 360 }; 357 361 defer { ··· 726 730 allocator: std.mem.Allocator, 727 731 tty: AnyWriter, 728 732 payload: []const u8, 729 - width: usize, 730 - height: usize, 733 + width: u16, 734 + height: u16, 731 735 medium: Image.TransmitMedium, 732 736 format: Image.TransmitFormat, 733 737 ) !Image { ··· 780 784 self: *Vaxis, 781 785 tty: AnyWriter, 782 786 bytes: []const u8, 783 - width: usize, 784 - height: usize, 787 + width: u16, 788 + height: u16, 785 789 format: Image.TransmitFormat, 786 790 ) !Image { 787 791 defer self.next_img_id += 1; ··· 861 865 const b64_buf = try arena.allocator().alloc(u8, base64Encoder.calcSize(buf.len)); 862 866 const encoded = base64Encoder.encode(b64_buf, buf); 863 867 864 - return self.transmitPreEncodedImage(tty, encoded, img.width, img.height, format); 868 + return self.transmitPreEncodedImage(tty, encoded, @intCast(img.width), @intCast(img.height), format); 865 869 } 866 870 867 871 pub fn loadImage( ··· 961 965 defer tty.writeAll(ctlseqs.sgr_reset) catch {}; 962 966 963 967 var reposition: bool = false; 964 - var row: usize = 0; 965 - var col: usize = 0; 968 + var row: u16 = 0; 969 + var col: u16 = 0; 966 970 var cursor: Style = .{}; 967 971 var link: Hyperlink = .{}; 968 972 var cursor_pos: struct { 969 - row: usize = 0, 970 - col: usize = 0, 973 + row: u16 = 0, 974 + col: u16 = 0, 971 975 } = .{}; 972 976 973 - var i: usize = 0; 977 + var i: u16 = 0; 974 978 while (i < self.screen.buf.len) { 975 979 const cell = self.screen.buf[i]; 976 980 const w = blk: {
+34 -34
src/Window.zig
··· 11 11 12 12 pub const Size = union(enum) { 13 13 expand, 14 - limit: usize, 14 + limit: u16, 15 15 }; 16 16 17 17 /// horizontal offset from the screen 18 - x_off: usize, 18 + x_off: u16, 19 19 /// vertical offset from the screen 20 - y_off: usize, 20 + y_off: u16, 21 21 /// width of the window. This can't be larger than the terminal screen 22 - width: usize, 22 + width: u16, 23 23 /// height of the window. This can't be larger than the terminal screen 24 - height: usize, 24 + height: u16, 25 25 26 26 screen: *Screen, 27 27 ··· 30 30 /// unaware of resizes. 31 31 fn initChild( 32 32 self: Window, 33 - x_off: usize, 34 - y_off: usize, 33 + x_off: u16, 34 + y_off: u16, 35 35 width: Size, 36 36 height: Size, 37 37 ) Window { 38 - const resolved_width = switch (width) { 38 + const resolved_width: u16 = switch (width) { 39 39 .expand => self.width -| x_off, 40 40 .limit => |w| blk: { 41 41 if (w + x_off > self.width) { ··· 44 44 break :blk w; 45 45 }, 46 46 }; 47 - const resolved_height = switch (height) { 47 + const resolved_height: u16 = switch (height) { 48 48 .expand => self.height -| y_off, 49 49 .limit => |h| blk: { 50 50 if (h + y_off > self.height) { ··· 63 63 } 64 64 65 65 pub const ChildOptions = struct { 66 - x_off: usize = 0, 67 - y_off: usize = 0, 66 + x_off: u16 = 0, 67 + y_off: u16 = 0, 68 68 /// the width of the resulting child, including any borders 69 69 width: Size = .expand, 70 70 /// the height of the resulting child, including any borders ··· 141 141 .other => |loc| loc, 142 142 }; 143 143 if (loc.top) { 144 - var i: usize = 0; 144 + var i: u16 = 0; 145 145 while (i < w) : (i += 1) { 146 146 result.writeCell(i, 0, .{ .char = horizontal, .style = style }); 147 147 } 148 148 } 149 149 if (loc.bottom) { 150 - var i: usize = 0; 150 + var i: u16 = 0; 151 151 while (i < w) : (i += 1) { 152 152 result.writeCell(i, h -| 1, .{ .char = horizontal, .style = style }); 153 153 } 154 154 } 155 155 if (loc.left) { 156 - var i: usize = 0; 156 + var i: u16 = 0; 157 157 while (i < h) : (i += 1) { 158 158 result.writeCell(0, i, .{ .char = vertical, .style = style }); 159 159 } 160 160 } 161 161 if (loc.right) { 162 - var i: usize = 0; 162 + var i: u16 = 0; 163 163 while (i < h) : (i += 1) { 164 164 result.writeCell(w -| 1, i, .{ .char = vertical, .style = style }); 165 165 } ··· 174 174 if (loc.bottom and loc.right) 175 175 result.writeCell(w -| 1, h -| 1, .{ .char = bottom_right, .style = style }); 176 176 177 - const x_off: usize = if (loc.left) 1 else 0; 178 - const y_off: usize = if (loc.top) 1 else 0; 179 - const h_delt: usize = if (loc.bottom) 1 else 0; 180 - const w_delt: usize = if (loc.right) 1 else 0; 181 - const h_ch: usize = h -| y_off -| h_delt; 182 - const w_ch: usize = w -| x_off -| w_delt; 177 + const x_off: u16 = if (loc.left) 1 else 0; 178 + const y_off: u16 = if (loc.top) 1 else 0; 179 + const h_delt: u16 = if (loc.bottom) 1 else 0; 180 + const w_delt: u16 = if (loc.right) 1 else 0; 181 + const h_ch: u16 = h -| y_off -| h_delt; 182 + const w_ch: u16 = w -| x_off -| w_delt; 183 183 return result.initChild(x_off, y_off, .{ .limit = w_ch }, .{ .limit = h_ch }); 184 184 } 185 185 186 186 /// writes a cell to the location in the window 187 - pub fn writeCell(self: Window, col: usize, row: usize, cell: Cell) void { 187 + pub fn writeCell(self: Window, col: u16, row: u16, cell: Cell) void { 188 188 if (self.height == 0 or self.width == 0) return; 189 189 if (self.height <= row or self.width <= col) return; 190 190 self.screen.writeCell(col + self.x_off, row + self.y_off, cell); 191 191 } 192 192 193 193 /// reads a cell at the location in the window 194 - pub fn readCell(self: Window, col: usize, row: usize) ?Cell { 194 + pub fn readCell(self: Window, col: u16, row: u16) ?Cell { 195 195 if (self.height == 0 or self.width == 0) return null; 196 196 if (self.height <= row or self.width <= col) return null; 197 197 return self.screen.readCell(col + self.x_off, row + self.y_off); ··· 203 203 } 204 204 205 205 /// returns the width of the grapheme. This depends on the terminal capabilities 206 - pub fn gwidth(self: Window, str: []const u8) usize { 206 + pub fn gwidth(self: Window, str: []const u8) u16 { 207 207 return gw.gwidth(str, self.screen.width_method, &self.screen.unicode.width_data); 208 208 } 209 209 ··· 220 220 @memset(self.screen.buf[start..end], cell); 221 221 } else { 222 222 // Non-contiguous. Iterate over rows an memset 223 - var row: usize = self.y_off; 223 + var row: u16 = self.y_off; 224 224 const last_row = @min(self.height + self.y_off, self.screen.height); 225 225 while (row < last_row) : (row += 1) { 226 226 const start = @min(self.x_off + (row * self.screen.width), self.screen.buf.len); ··· 237 237 } 238 238 239 239 /// show the cursor at the given coordinates, 0 indexed 240 - pub fn showCursor(self: Window, col: usize, row: usize) void { 240 + pub fn showCursor(self: Window, col: u16, row: u16) void { 241 241 if (self.height == 0 or self.width == 0) return; 242 242 if (self.height <= row or self.width <= col) return; 243 243 self.screen.cursor_vis = true; ··· 252 252 /// Options to use when printing Segments to a window 253 253 pub const PrintOptions = struct { 254 254 /// vertical offset to start printing at 255 - row_offset: usize = 0, 255 + row_offset: u16 = 0, 256 256 /// horizontal offset to start printing at 257 - col_offset: usize = 0, 257 + col_offset: u16 = 0, 258 258 259 259 /// wrap behavior for printing 260 260 wrap: enum { ··· 273 273 }; 274 274 275 275 pub const PrintResult = struct { 276 - col: usize, 277 - row: usize, 276 + col: u16, 277 + row: u16, 278 278 overflow: bool, 279 279 }; 280 280 ··· 284 284 var row = opts.row_offset; 285 285 switch (opts.wrap) { 286 286 .grapheme => { 287 - var col: usize = opts.col_offset; 287 + var col: u16 = opts.col_offset; 288 288 const overflow: bool = blk: for (segments) |segment| { 289 289 var iter = self.screen.unicode.graphemeIterator(segment.text); 290 290 while (iter.next()) |grapheme| { ··· 324 324 }; 325 325 }, 326 326 .word => { 327 - var col: usize = opts.col_offset; 327 + var col: u16 = opts.col_offset; 328 328 var overflow: bool = false; 329 329 var soft_wrapped: bool = false; 330 330 outer: for (segments) |segment| { ··· 406 406 }; 407 407 }, 408 408 .none => { 409 - var col: usize = opts.col_offset; 409 + var col: u16 = opts.col_offset; 410 410 const overflow: bool = blk: for (segments) |segment| { 411 411 var iter = self.screen.unicode.graphemeIterator(segment.text); 412 412 while (iter.next()) |grapheme| { ··· 443 443 444 444 /// scrolls the window down one row (IE inserts a blank row at the bottom of the 445 445 /// screen and shifts all rows up one) 446 - pub fn scroll(self: Window, n: usize) void { 446 + pub fn scroll(self: Window, n: u16) void { 447 447 if (n > self.height) return; 448 448 var row = self.y_off; 449 449 while (row < self.height - n) : (row += 1) {
+7 -8
src/gwidth.zig
··· 12 12 }; 13 13 14 14 /// returns the width of the provided string, as measured by the method chosen 15 - pub fn gwidth(str: []const u8, method: Method, data: *const DisplayWidth.DisplayWidthData) usize { 15 + pub fn gwidth(str: []const u8, method: Method, data: *const DisplayWidth.DisplayWidthData) u16 { 16 16 switch (method) { 17 17 .unicode => { 18 18 const dw: DisplayWidth = .{ .data = data }; 19 - return dw.strWidth(str); 19 + return @intCast(dw.strWidth(str)); 20 20 }, 21 21 .wcwidth => { 22 - var total: usize = 0; 22 + var total: u16 = 0; 23 23 var iter: code_point.Iterator = .{ .bytes = str }; 24 24 while (iter.next()) |cp| { 25 - const w = switch (cp.code) { 25 + const w: u16 = switch (cp.code) { 26 26 // undo an override in zg for emoji skintone selectors 27 27 0x1f3fb...0x1f3ff, 28 28 => 2, 29 - else => data.codePointWidth(cp.code), 29 + else => @max(0, data.codePointWidth(cp.code)), 30 30 }; 31 - if (w < 0) continue; 32 - total += @intCast(w); 31 + total += w; 33 32 } 34 33 return total; 35 34 }, 36 35 .no_zwj => { 37 36 var iter = std.mem.split(u8, str, "\u{200D}"); 38 - var result: usize = 0; 37 + var result: u16 = 0; 39 38 while (iter.next()) |s| { 40 39 result += gwidth(s, .unicode, data); 41 40 }
+4 -4
src/main.zig
··· 39 39 40 40 /// The size of the terminal screen 41 41 pub const Winsize = struct { 42 - rows: usize, 43 - cols: usize, 44 - x_pixel: usize, 45 - y_pixel: usize, 42 + rows: u16, 43 + cols: u16, 44 + x_pixel: u16, 45 + y_pixel: u16, 46 46 }; 47 47 48 48 /// Initialize a Vaxis application.
+2 -2
src/widgets/CodeView.zig
··· 4 4 const LineNumbers = vaxis.widgets.LineNumbers; 5 5 6 6 pub const DrawOptions = struct { 7 - highlighted_line: usize = 0, 7 + highlighted_line: u16 = 0, 8 8 draw_line_numbers: bool = true, 9 - indentation: usize = 0, 9 + indentation: u16 = 0, 10 10 }; 11 11 12 12 pub const Buffer = vaxis.widgets.TextView.Buffer;
+1 -1
src/widgets/LineNumbers.zig
··· 12 12 return (v / (std.math.powi(usize, 10, n) catch unreachable)) % 10; 13 13 } 14 14 15 - pub fn numDigits(v: usize) usize { 15 + pub fn numDigits(v: usize) u8 { 16 16 return switch (v) { 17 17 0...9 => 1, 18 18 10...99 => 2,
+25 -25
src/widgets/Table.zig
··· 9 9 /// Table Context for maintaining state and drawing Tables with `drawTable()`. 10 10 pub const TableContext = struct { 11 11 /// Current active Row of the Table. 12 - row: usize = 0, 12 + row: u16 = 0, 13 13 /// Current active Column of the Table. 14 - col: usize = 0, 14 + col: u16 = 0, 15 15 /// Starting point within the Data List. 16 - start: usize = 0, 16 + start: u16 = 0, 17 17 /// Selected Rows. 18 - sel_rows: ?[]usize = null, 18 + sel_rows: ?[]u16 = null, 19 19 20 20 /// Active status of the Table. 21 21 active: bool = false, 22 22 /// Active Content Callback Function. 23 23 /// If available, this will be called to vertically expand the active row with additional info. 24 - active_content_fn: ?*const fn (*vaxis.Window, *const anyopaque) anyerror!usize = null, 24 + active_content_fn: ?*const fn (*vaxis.Window, *const anyopaque) anyerror!u16 = null, 25 25 /// Active Content Context 26 26 /// This will be provided to the `active_content` callback when called. 27 27 active_ctx: *const anyopaque = &{}, 28 28 /// Y Offset for rows beyond the Active Content. 29 29 /// (This will be calculated automatically) 30 - active_y_off: usize = 0, 30 + active_y_off: u16 = 0, 31 31 32 32 /// The Background Color for Selected Rows. 33 33 selected_bg: vaxis.Cell.Color, ··· 47 47 row_bg_2: vaxis.Cell.Color = .{ .rgb = [_]u8{ 8, 8, 8 } }, 48 48 49 49 /// Y Offset for drawing to the parent Window. 50 - y_off: usize = 0, 50 + y_off: u16 = 0, 51 51 /// X Offset for printing each Cell/Item. 52 - cell_x_off: usize = 1, 52 + cell_x_off: u16 = 1, 53 53 54 54 /// Column Width 55 55 /// Note, if this is left `null` the Column Width will be dynamically calculated during `drawTable()`. ··· 78 78 /// Dynamically calculate Column Widths such that the entire (or most) of the screen is filled horizontally. 79 79 dynamic_fill, 80 80 /// Dynamically calculate the Column Width for each Column based on its Header Length and the provided Padding length. 81 - dynamic_header_len: usize, 81 + dynamic_header_len: u16, 82 82 /// Statically set all Column Widths to the same value. 83 - static_all: usize, 83 + static_all: u16, 84 84 /// Statically set individual Column Widths to specific values. 85 - static_individual: []const usize, 85 + static_individual: []const u16, 86 86 }; 87 87 88 88 /// Column Indexes ··· 216 216 }); 217 217 218 218 // Headers 219 - if (table_ctx.col > headers.len - 1) table_ctx.col = headers.len - 1; 220 - var col_start: usize = 0; 219 + if (table_ctx.col > headers.len - 1) table_ctx.col = @intCast(headers.len - 1); 220 + var col_start: u16 = 0; 221 221 for (headers[0..], 0..) |hdr_txt, idx| { 222 222 const col_width = try calcColWidth( 223 - idx, 223 + @intCast(idx), 224 224 headers, 225 225 table_ctx.col_width, 226 226 table_win, ··· 260 260 261 261 // Rows 262 262 if (table_ctx.active_content_fn == null) table_ctx.active_y_off = 0; 263 - const max_items = 264 - if (data_items.len > table_win.height -| 1) table_win.height -| 1 else data_items.len; 263 + const max_items: u16 = 264 + if (data_items.len > table_win.height -| 1) table_win.height -| 1 else @intCast(data_items.len); 265 265 var end = table_ctx.start + max_items; 266 266 if (table_ctx.row + table_ctx.active_y_off >= win.height -| 2) 267 267 end -|= table_ctx.active_y_off; 268 - if (end > data_items.len) end = data_items.len; 268 + if (end > data_items.len) end = @intCast(data_items.len); 269 269 table_ctx.start = tableStart: { 270 270 if (table_ctx.row == 0) 271 271 break :tableStart 0; 272 272 if (table_ctx.row < table_ctx.start) 273 273 break :tableStart table_ctx.start - (table_ctx.start - table_ctx.row); 274 274 if (table_ctx.row >= data_items.len - 1) 275 - table_ctx.row = data_items.len - 1; 275 + table_ctx.row = @intCast(data_items.len - 1); 276 276 if (table_ctx.row >= end) 277 277 break :tableStart table_ctx.start + (table_ctx.row - end + 1); 278 278 break :tableStart table_ctx.start; ··· 280 280 end = table_ctx.start + max_items; 281 281 if (table_ctx.row + table_ctx.active_y_off >= win.height -| 2) 282 282 end -|= table_ctx.active_y_off; 283 - if (end > data_items.len) end = data_items.len; 283 + if (end > data_items.len) end = @intCast(data_items.len); 284 284 table_ctx.start = @min(table_ctx.start, end); 285 285 table_ctx.active_y_off = 0; 286 286 for (data_items[table_ctx.start..end], 0..) |data, row| { ··· 288 288 if (table_ctx.active and table_ctx.start + row == table_ctx.row) 289 289 break :rowColors .{ table_ctx.active_fg, table_ctx.active_bg }; 290 290 if (table_ctx.sel_rows) |rows| { 291 - if (mem.indexOfScalar(usize, rows, table_ctx.start + row) != null) 291 + if (mem.indexOfScalar(u16, rows, @intCast(table_ctx.start + row)) != null) 292 292 break :rowColors .{ table_ctx.selected_fg, table_ctx.selected_bg }; 293 293 } 294 294 if (row % 2 == 0) break :rowColors .{ .default, table_ctx.row_bg_1 }; ··· 296 296 }; 297 297 var row_win = table_win.child(.{ 298 298 .x_off = 0, 299 - .y_off = 1 + row + table_ctx.active_y_off, 299 + .y_off = @intCast(1 + row + table_ctx.active_y_off), 300 300 .width = .{ .limit = table_win.width }, 301 301 .height = .{ .limit = 1 }, 302 302 //.border = .{ .where = if (table_ctx.row_borders) .top else .none }, ··· 386 386 387 387 /// Calculate the Column Width of `col` using the provided Number of Headers (`num_hdrs`), Width Style (`style`), and Table Window (`table_win`). 388 388 pub fn calcColWidth( 389 - col: usize, 389 + col: u16, 390 390 headers: []const []const u8, 391 391 style: WidthStyle, 392 392 table_win: vaxis.Window, 393 - ) !usize { 393 + ) !u16 { 394 394 return switch (style) { 395 395 .dynamic_fill => dynFill: { 396 - var cw = table_win.width / headers.len; 396 + var cw: u16 = table_win.width / @as(u16, @intCast(headers.len)); 397 397 if (cw % 2 != 0) cw +|= 1; 398 398 while (cw * headers.len < table_win.width - 1) cw +|= 1; 399 399 break :dynFill cw; 400 400 }, 401 401 .dynamic_header_len => dynHdrs: { 402 402 if (col >= headers.len) break :dynHdrs error.NotEnoughStaticWidthsProvided; 403 - break :dynHdrs headers[col].len + (style.dynamic_header_len * 2); 403 + break :dynHdrs @as(u16, @intCast(headers[col].len)) + (style.dynamic_header_len * 2); 404 404 }, 405 405 .static_all => style.static_all, 406 406 .static_individual => statInd: {
+10 -10
src/widgets/TextInput.zig
··· 18 18 buf: Buffer, 19 19 20 20 /// the number of graphemes to skip when drawing. Used for horizontal scrolling 21 - draw_offset: usize = 0, 21 + draw_offset: u16 = 0, 22 22 /// the column we placed the cursor the last time we drew 23 - prev_cursor_col: usize = 0, 23 + prev_cursor_col: u16 = 0, 24 24 /// the grapheme index of the cursor the last time we drew 25 - prev_cursor_idx: usize = 0, 25 + prev_cursor_idx: u16 = 0, 26 26 /// approximate distance from an edge before we scroll 27 - scroll_offset: usize = 4, 27 + scroll_offset: u16 = 4, 28 28 29 29 unicode: *const Unicode, 30 30 ··· 88 88 } 89 89 90 90 /// calculates the display width from the draw_offset to the cursor 91 - pub fn widthToCursor(self: *TextInput, win: Window) usize { 92 - var width: usize = 0; 91 + pub fn widthToCursor(self: *TextInput, win: Window) u16 { 92 + var width: u16 = 0; 93 93 const first_half = self.buf.firstHalf(); 94 94 var first_iter = self.unicode.graphemeIterator(first_half); 95 95 var i: usize = 0; ··· 120 120 self.buf.moveGapRight(grapheme.len); 121 121 } 122 122 123 - pub fn graphemesBeforeCursor(self: *const TextInput) usize { 123 + pub fn graphemesBeforeCursor(self: *const TextInput) u16 { 124 124 const first_half = self.buf.firstHalf(); 125 125 var first_iter = self.unicode.graphemeIterator(first_half); 126 - var i: usize = 0; 126 + var i: u16 = 0; 127 127 while (first_iter.next()) |_| { 128 128 i += 1; 129 129 } ··· 153 153 // one way to _ensure_ this is to move the gap... but that's a cost we probably don't want to pay. 154 154 const first_half = self.buf.firstHalf(); 155 155 var first_iter = self.unicode.graphemeIterator(first_half); 156 - var col: usize = 0; 157 - var i: usize = 0; 156 + var col: u16 = 0; 157 + var i: u16 = 0; 158 158 while (first_iter.next()) |grapheme| { 159 159 if (i < self.draw_offset) { 160 160 i += 1;
+11 -11
src/widgets/View.zig
··· 20 20 21 21 /// View Initialization Config 22 22 pub const Config = struct { 23 - width: usize, 24 - height: usize, 23 + width: u16, 24 + height: u16, 25 25 }; 26 26 27 27 /// Initialize a new View ··· 58 58 } 59 59 60 60 pub const DrawOptions = struct { 61 - x_off: usize = 0, 62 - y_off: usize = 0, 61 + x_off: u16 = 0, 62 + y_off: u16 = 0, 63 63 }; 64 64 65 65 pub fn draw(self: *View, win: Window, opts: DrawOptions) void { ··· 80 80 81 81 /// Render Config for `toWin()` 82 82 pub const RenderConfig = struct { 83 - x: usize = 0, 84 - y: usize = 0, 83 + x: u16 = 0, 84 + y: u16 = 0, 85 85 width: Extent = .fit, 86 86 height: Extent = .fit, 87 87 88 88 pub const Extent = union(enum) { 89 89 fit, 90 - max: usize, 90 + max: u16, 91 91 }; 92 92 }; 93 93 94 94 /// Render a portion of this View to the provided Window (`win`). 95 95 /// This will return the bounded X (col), Y (row) coordinates based on the rendering. 96 - pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { usize, usize } { 96 + pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { u16, u16 } { 97 97 var x = @min(self.screen.width - 1, config.x); 98 98 var y = @min(self.screen.height - 1, config.y); 99 99 const width = width: { ··· 123 123 } 124 124 125 125 /// Writes a cell to the location in the View 126 - pub fn writeCell(self: *View, col: usize, row: usize, cell: Cell) void { 126 + pub fn writeCell(self: *View, col: u16, row: u16, cell: Cell) void { 127 127 self.screen.writeCell(col, row, cell); 128 128 } 129 129 130 130 /// Reads a cell at the location in the View 131 - pub fn readCell(self: *const View, col: usize, row: usize) ?Cell { 131 + pub fn readCell(self: *const View, col: u16, row: u16) ?Cell { 132 132 return self.screen.readCell(col, row); 133 133 } 134 134 ··· 138 138 } 139 139 140 140 /// Returns the width of the grapheme. This depends on the terminal capabilities 141 - pub fn gwidth(self: View, str: []const u8) usize { 141 + pub fn gwidth(self: View, str: []const u8) u16 { 142 142 return gw.gwidth(str, self.screen.width_method, &self.screen.unicode.width_data); 143 143 } 144 144
+1 -1
src/widgets/alignment.zig
··· 1 1 const Window = @import("../Window.zig"); 2 2 3 - pub fn center(parent: Window, cols: usize, rows: usize) Window { 3 + pub fn center(parent: Window, cols: u16, rows: u16) Window { 4 4 const y_off = (parent.height / 2) -| (rows / 2); 5 5 const x_off = (parent.width / 2) -| (cols / 2); 6 6 return parent.child(.{
+12 -12
src/widgets/terminal/Screen.zig
··· 49 49 style: vaxis.Style = .{}, 50 50 uri: std.ArrayList(u8) = undefined, 51 51 uri_id: std.ArrayList(u8) = undefined, 52 - col: usize = 0, 53 - row: usize = 0, 52 + col: u16 = 0, 53 + row: u16 = 0, 54 54 pending_wrap: bool = false, 55 55 shape: vaxis.Cell.CursorShape = .default, 56 56 visible: bool = true, ··· 68 68 }; 69 69 70 70 pub const ScrollingRegion = struct { 71 - top: usize, 72 - bottom: usize, 73 - left: usize, 74 - right: usize, 71 + top: u16, 72 + bottom: u16, 73 + left: u16, 74 + right: u16, 75 75 76 76 pub fn contains(self: ScrollingRegion, col: usize, row: usize) bool { 77 77 return col >= self.left and ··· 81 81 } 82 82 }; 83 83 84 - width: usize = 0, 85 - height: usize = 0, 84 + width: u16 = 0, 85 + height: u16 = 0, 86 86 87 87 scrolling_region: ScrollingRegion, 88 88 ··· 93 93 csi_u_flags: vaxis.Key.KittyFlags = @bitCast(@as(u5, 0)), 94 94 95 95 /// sets each cell to the default cell 96 - pub fn init(alloc: std.mem.Allocator, w: usize, h: usize) !Screen { 96 + pub fn init(alloc: std.mem.Allocator, w: u16, h: u16) !Screen { 97 97 var screen = Screen{ 98 98 .buf = try alloc.alloc(Cell, w * h), 99 99 .scrolling_region = .{ ··· 313 313 } 314 314 } 315 315 316 - pub fn cursorUp(self: *Screen, n: usize) void { 316 + pub fn cursorUp(self: *Screen, n: u16) void { 317 317 self.cursor.pending_wrap = false; 318 318 if (self.withinScrollingRegion()) 319 319 self.cursor.row = @max( ··· 324 324 self.cursor.row -|= n; 325 325 } 326 326 327 - pub fn cursorLeft(self: *Screen, n: usize) void { 327 + pub fn cursorLeft(self: *Screen, n: u16) void { 328 328 self.cursor.pending_wrap = false; 329 329 if (self.withinScrollingRegion()) 330 330 self.cursor.col = @max( ··· 335 335 self.cursor.col = self.cursor.col -| n; 336 336 } 337 337 338 - pub fn cursorRight(self: *Screen, n: usize) void { 338 + pub fn cursorRight(self: *Screen, n: u16) void { 339 339 self.cursor.pending_wrap = false; 340 340 if (self.withinScrollingRegion()) 341 341 self.cursor.col = @min(
+4 -4
src/widgets/terminal/Terminal.zig
··· 31 31 const log = std.log.scoped(.terminal); 32 32 33 33 pub const Options = struct { 34 - scrollback_size: usize = 500, 34 + scrollback_size: u16 = 500, 35 35 winsize: Winsize = .{ .rows = 24, .cols = 80, .x_pixel = 0, .y_pixel = 0 }, 36 36 initial_working_directory: ?[]const u8 = null, 37 37 }; ··· 52 52 pub var global_sigchild_installed: bool = false; 53 53 54 54 allocator: std.mem.Allocator, 55 - scrollback_size: usize, 55 + scrollback_size: u16, 56 56 57 57 pty: Pty, 58 58 cmd: Command, ··· 221 221 } 222 222 } 223 223 224 - var row: usize = 0; 224 + var row: u16 = 0; 225 225 while (row < self.front_screen.height) : (row += 1) { 226 - var col: usize = 0; 226 + var col: u16 = 0; 227 227 while (col < self.front_screen.width) { 228 228 const cell = self.front_screen.readCell(col, row) orelse continue; 229 229 win.writeCell(col, row, cell);