···47474848 var n: usize = 0;
49495050- var clip_y: usize = 0;
5050+ var clip_y: u16 = 0;
51515252 while (true) {
5353 const event = loop.nextEvent();
+3-2
examples/main.zig
···6767 // the old and only updated cells will be drawn
6868 win.clear();
69697070+ const msg_len: u16 = @intCast(msg.len);
7071 // Create some child window. .expand means the height and width will
7172 // fill the remaining space of the parent. Child windows do not store a
7273 // reference to their parent: this is true immediate mode. Do not store
7374 // windows, always create new windows each render cycle
7475 const child = win.child(
7575- .{ .x_off = win.width / 2 - msg.len / 2, .y_off = win.height / 2 },
7676+ .{ .x_off = win.width / 2 - msg_len / 2, .y_off = win.height / 2 },
7677 );
7778 // Loop through the message and print the cells to the screen
7879 for (msg, 0..) |_, i| {
···8687 .fg = .{ .index = color_idx },
8788 },
8889 };
8989- child.writeCell(i, 0, cell);
9090+ child.writeCell(@intCast(i), 0, cell);
9091 }
9192 // Render the screen
9293 try vx.render(tty.anyWriter());
···99/// Table Context for maintaining state and drawing Tables with `drawTable()`.
1010pub const TableContext = struct {
1111 /// Current active Row of the Table.
1212- row: usize = 0,
1212+ row: u16 = 0,
1313 /// Current active Column of the Table.
1414- col: usize = 0,
1414+ col: u16 = 0,
1515 /// Starting point within the Data List.
1616- start: usize = 0,
1616+ start: u16 = 0,
1717 /// Selected Rows.
1818- sel_rows: ?[]usize = null,
1818+ sel_rows: ?[]u16 = null,
19192020 /// Active status of the Table.
2121 active: bool = false,
2222 /// Active Content Callback Function.
2323 /// If available, this will be called to vertically expand the active row with additional info.
2424- active_content_fn: ?*const fn (*vaxis.Window, *const anyopaque) anyerror!usize = null,
2424+ active_content_fn: ?*const fn (*vaxis.Window, *const anyopaque) anyerror!u16 = null,
2525 /// Active Content Context
2626 /// This will be provided to the `active_content` callback when called.
2727 active_ctx: *const anyopaque = &{},
2828 /// Y Offset for rows beyond the Active Content.
2929 /// (This will be calculated automatically)
3030- active_y_off: usize = 0,
3030+ active_y_off: u16 = 0,
31313232 /// The Background Color for Selected Rows.
3333 selected_bg: vaxis.Cell.Color,
···4747 row_bg_2: vaxis.Cell.Color = .{ .rgb = [_]u8{ 8, 8, 8 } },
48484949 /// Y Offset for drawing to the parent Window.
5050- y_off: usize = 0,
5050+ y_off: u16 = 0,
5151 /// X Offset for printing each Cell/Item.
5252- cell_x_off: usize = 1,
5252+ cell_x_off: u16 = 1,
53535454 /// Column Width
5555 /// Note, if this is left `null` the Column Width will be dynamically calculated during `drawTable()`.
···7878 /// Dynamically calculate Column Widths such that the entire (or most) of the screen is filled horizontally.
7979 dynamic_fill,
8080 /// Dynamically calculate the Column Width for each Column based on its Header Length and the provided Padding length.
8181- dynamic_header_len: usize,
8181+ dynamic_header_len: u16,
8282 /// Statically set all Column Widths to the same value.
8383- static_all: usize,
8383+ static_all: u16,
8484 /// Statically set individual Column Widths to specific values.
8585- static_individual: []const usize,
8585+ static_individual: []const u16,
8686};
87878888/// Column Indexes
···216216 });
217217218218 // Headers
219219- if (table_ctx.col > headers.len - 1) table_ctx.col = headers.len - 1;
220220- var col_start: usize = 0;
219219+ if (table_ctx.col > headers.len - 1) table_ctx.col = @intCast(headers.len - 1);
220220+ var col_start: u16 = 0;
221221 for (headers[0..], 0..) |hdr_txt, idx| {
222222 const col_width = try calcColWidth(
223223- idx,
223223+ @intCast(idx),
224224 headers,
225225 table_ctx.col_width,
226226 table_win,
···260260261261 // Rows
262262 if (table_ctx.active_content_fn == null) table_ctx.active_y_off = 0;
263263- const max_items =
264264- if (data_items.len > table_win.height -| 1) table_win.height -| 1 else data_items.len;
263263+ const max_items: u16 =
264264+ if (data_items.len > table_win.height -| 1) table_win.height -| 1 else @intCast(data_items.len);
265265 var end = table_ctx.start + max_items;
266266 if (table_ctx.row + table_ctx.active_y_off >= win.height -| 2)
267267 end -|= table_ctx.active_y_off;
268268- if (end > data_items.len) end = data_items.len;
268268+ if (end > data_items.len) end = @intCast(data_items.len);
269269 table_ctx.start = tableStart: {
270270 if (table_ctx.row == 0)
271271 break :tableStart 0;
272272 if (table_ctx.row < table_ctx.start)
273273 break :tableStart table_ctx.start - (table_ctx.start - table_ctx.row);
274274 if (table_ctx.row >= data_items.len - 1)
275275- table_ctx.row = data_items.len - 1;
275275+ table_ctx.row = @intCast(data_items.len - 1);
276276 if (table_ctx.row >= end)
277277 break :tableStart table_ctx.start + (table_ctx.row - end + 1);
278278 break :tableStart table_ctx.start;
···280280 end = table_ctx.start + max_items;
281281 if (table_ctx.row + table_ctx.active_y_off >= win.height -| 2)
282282 end -|= table_ctx.active_y_off;
283283- if (end > data_items.len) end = data_items.len;
283283+ if (end > data_items.len) end = @intCast(data_items.len);
284284 table_ctx.start = @min(table_ctx.start, end);
285285 table_ctx.active_y_off = 0;
286286 for (data_items[table_ctx.start..end], 0..) |data, row| {
···288288 if (table_ctx.active and table_ctx.start + row == table_ctx.row)
289289 break :rowColors .{ table_ctx.active_fg, table_ctx.active_bg };
290290 if (table_ctx.sel_rows) |rows| {
291291- if (mem.indexOfScalar(usize, rows, table_ctx.start + row) != null)
291291+ if (mem.indexOfScalar(u16, rows, @intCast(table_ctx.start + row)) != null)
292292 break :rowColors .{ table_ctx.selected_fg, table_ctx.selected_bg };
293293 }
294294 if (row % 2 == 0) break :rowColors .{ .default, table_ctx.row_bg_1 };
···296296 };
297297 var row_win = table_win.child(.{
298298 .x_off = 0,
299299- .y_off = 1 + row + table_ctx.active_y_off,
299299+ .y_off = @intCast(1 + row + table_ctx.active_y_off),
300300 .width = .{ .limit = table_win.width },
301301 .height = .{ .limit = 1 },
302302 //.border = .{ .where = if (table_ctx.row_borders) .top else .none },
···386386387387/// Calculate the Column Width of `col` using the provided Number of Headers (`num_hdrs`), Width Style (`style`), and Table Window (`table_win`).
388388pub fn calcColWidth(
389389- col: usize,
389389+ col: u16,
390390 headers: []const []const u8,
391391 style: WidthStyle,
392392 table_win: vaxis.Window,
393393-) !usize {
393393+) !u16 {
394394 return switch (style) {
395395 .dynamic_fill => dynFill: {
396396- var cw = table_win.width / headers.len;
396396+ var cw: u16 = table_win.width / @as(u16, @intCast(headers.len));
397397 if (cw % 2 != 0) cw +|= 1;
398398 while (cw * headers.len < table_win.width - 1) cw +|= 1;
399399 break :dynFill cw;
400400 },
401401 .dynamic_header_len => dynHdrs: {
402402 if (col >= headers.len) break :dynHdrs error.NotEnoughStaticWidthsProvided;
403403- break :dynHdrs headers[col].len + (style.dynamic_header_len * 2);
403403+ break :dynHdrs @as(u16, @intCast(headers[col].len)) + (style.dynamic_header_len * 2);
404404 },
405405 .static_all => style.static_all,
406406 .static_individual => statInd: {
+10-10
src/widgets/TextInput.zig
···1818buf: Buffer,
19192020/// the number of graphemes to skip when drawing. Used for horizontal scrolling
2121-draw_offset: usize = 0,
2121+draw_offset: u16 = 0,
2222/// the column we placed the cursor the last time we drew
2323-prev_cursor_col: usize = 0,
2323+prev_cursor_col: u16 = 0,
2424/// the grapheme index of the cursor the last time we drew
2525-prev_cursor_idx: usize = 0,
2525+prev_cursor_idx: u16 = 0,
2626/// approximate distance from an edge before we scroll
2727-scroll_offset: usize = 4,
2727+scroll_offset: u16 = 4,
28282929unicode: *const Unicode,
3030···8888}
89899090/// calculates the display width from the draw_offset to the cursor
9191-pub fn widthToCursor(self: *TextInput, win: Window) usize {
9292- var width: usize = 0;
9191+pub fn widthToCursor(self: *TextInput, win: Window) u16 {
9292+ var width: u16 = 0;
9393 const first_half = self.buf.firstHalf();
9494 var first_iter = self.unicode.graphemeIterator(first_half);
9595 var i: usize = 0;
···120120 self.buf.moveGapRight(grapheme.len);
121121}
122122123123-pub fn graphemesBeforeCursor(self: *const TextInput) usize {
123123+pub fn graphemesBeforeCursor(self: *const TextInput) u16 {
124124 const first_half = self.buf.firstHalf();
125125 var first_iter = self.unicode.graphemeIterator(first_half);
126126- var i: usize = 0;
126126+ var i: u16 = 0;
127127 while (first_iter.next()) |_| {
128128 i += 1;
129129 }
···153153 // one way to _ensure_ this is to move the gap... but that's a cost we probably don't want to pay.
154154 const first_half = self.buf.firstHalf();
155155 var first_iter = self.unicode.graphemeIterator(first_half);
156156- var col: usize = 0;
157157- var i: usize = 0;
156156+ var col: u16 = 0;
157157+ var i: u16 = 0;
158158 while (first_iter.next()) |grapheme| {
159159 if (i < self.draw_offset) {
160160 i += 1;
+11-11
src/widgets/View.zig
···20202121/// View Initialization Config
2222pub const Config = struct {
2323- width: usize,
2424- height: usize,
2323+ width: u16,
2424+ height: u16,
2525};
26262727/// Initialize a new View
···5858}
59596060pub const DrawOptions = struct {
6161- x_off: usize = 0,
6262- y_off: usize = 0,
6161+ x_off: u16 = 0,
6262+ y_off: u16 = 0,
6363};
64646565pub fn draw(self: *View, win: Window, opts: DrawOptions) void {
···80808181/// Render Config for `toWin()`
8282pub const RenderConfig = struct {
8383- x: usize = 0,
8484- y: usize = 0,
8383+ x: u16 = 0,
8484+ y: u16 = 0,
8585 width: Extent = .fit,
8686 height: Extent = .fit,
87878888 pub const Extent = union(enum) {
8989 fit,
9090- max: usize,
9090+ max: u16,
9191 };
9292};
93939494/// Render a portion of this View to the provided Window (`win`).
9595/// This will return the bounded X (col), Y (row) coordinates based on the rendering.
9696-pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { usize, usize } {
9696+pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { u16, u16 } {
9797 var x = @min(self.screen.width - 1, config.x);
9898 var y = @min(self.screen.height - 1, config.y);
9999 const width = width: {
···123123}
124124125125/// Writes a cell to the location in the View
126126-pub fn writeCell(self: *View, col: usize, row: usize, cell: Cell) void {
126126+pub fn writeCell(self: *View, col: u16, row: u16, cell: Cell) void {
127127 self.screen.writeCell(col, row, cell);
128128}
129129130130/// Reads a cell at the location in the View
131131-pub fn readCell(self: *const View, col: usize, row: usize) ?Cell {
131131+pub fn readCell(self: *const View, col: u16, row: u16) ?Cell {
132132 return self.screen.readCell(col, row);
133133}
134134···138138}
139139140140/// Returns the width of the grapheme. This depends on the terminal capabilities
141141-pub fn gwidth(self: View, str: []const u8) usize {
141141+pub fn gwidth(self: View, str: []const u8) u16 {
142142 return gw.gwidth(str, self.screen.width_method, &self.screen.unicode.width_data);
143143}
144144