diff --git a/src/tui/EditBuffer.zig b/src/tui/EditBuffer.zig index 07520fe..0837447 100644 --- a/src/tui/EditBuffer.zig +++ b/src/tui/EditBuffer.zig @@ -10,7 +10,7 @@ const ArrayList = std.ArrayList; const EditBuffer = @This(); buffer: ArrayList(u8), -cursor: usize, +cursor: u16, dirty: bool, pub fn init(allocator: Allocator) EditBuffer { @@ -25,8 +25,8 @@ pub fn deinit(eb: *EditBuffer) void { eb.buffer.deinit(); } -pub fn len(eb: *const EditBuffer) usize { - return eb.buffer.items.len; +pub fn len(eb: *const EditBuffer) u16 { + return @intCast(eb.buffer.items.len); } pub fn slice(eb: *EditBuffer) []const u8 { @@ -36,7 +36,8 @@ pub fn slice(eb: *EditBuffer) []const u8 { /// Insert utf-8 encoded text into the buffer at the cursor position pub fn insert(eb: *EditBuffer, bytes: []const u8) !void { try eb.buffer.insertSlice(eb.cursor, bytes); - eb.cursor += bytes.len; + const bytes_len: u16 = @intCast(bytes.len); + eb.cursor += bytes_len; eb.dirty = true; } @@ -64,12 +65,12 @@ pub fn deleteTo(eb: *EditBuffer, pos: usize) void { } /// Set the cursor to an absolute position -pub fn setCursor(eb: *EditBuffer, pos: usize) void { +pub fn setCursor(eb: *EditBuffer, pos: u16) void { eb.cursor = if (pos > eb.len()) eb.len() else pos; } /// Move the cursor relative to it's current position -pub fn moveCursor(eb: *EditBuffer, amount: usize, direction: Direction) void { +pub fn moveCursor(eb: *EditBuffer, amount: u16, direction: Direction) void { eb.cursor = switch (direction) { .left => if (amount >= eb.cursor) 0 else eb.cursor - amount, .right => if (eb.cursor + amount > eb.len()) eb.len() else eb.cursor + amount, diff --git a/src/tui/opts.zig b/src/tui/opts.zig index b671865..5600842 100644 --- a/src/tui/opts.zig +++ b/src/tui/opts.zig @@ -94,7 +94,7 @@ const OptionIter = struct { pub const Config = struct { // Commandline options keep_order: bool = false, - height: usize = 10, + height: u16 = 10, filter: ?[]const u8 = null, plain: bool = false, delimiter: []const u8 = "\n", @@ -144,7 +144,7 @@ pub fn parse(allocator: Allocator, args: []const []const u8, stderr: File.Writer // height else if (mem.eql(u8, opt, "height") or mem.eql(u8, opt, "l") or mem.eql(u8, opt, "lines")) { const height_str = iter.getArg() orelse missingArg(stderr, iter, opt); - const height = fmt.parseUnsigned(usize, height_str, 10) catch argError(stderr, "height must be an integer"); + const height = fmt.parseUnsigned(u16, height_str, 10) catch argError(stderr, "height must be an integer"); if (height < 2) argError(stderr, "height must be an integer greater than 1"); config.height = height; } diff --git a/src/tui/ui.zig b/src/tui/ui.zig index fa56a5d..60decae 100644 --- a/src/tui/ui.zig +++ b/src/tui/ui.zig @@ -298,18 +298,18 @@ pub const State = struct { win.clear(); const width = state.vx.screen.width; - const preview_width: usize = if (state.preview) |_| + const preview_width: u16 = if (state.preview) |_| @intFromFloat(@as(f64, @floatFromInt(width)) * state.config.preview_width) else 0; const items_width = width - preview_width; - const items = win.child(.{ .height = .{ .limit = state.config.height }, .width = .{ .limit = items_width } }); + const items = win.child(.{ .height = state.config.height, .width = items_width }); const height = @min(state.vx.screen.height, state.config.height); // draw the candidates - var line: usize = 0; + var line: u16 = 0; while (line < height - 1) : (line += 1) { if (line < candidates.len) state.drawCandidate( items, @@ -328,18 +328,18 @@ pub const State = struct { if (num_selected > 0) { const stats = try std.fmt.bufPrint(&buf, "{}/{} [{}]", .{ candidates.len, total_candidates, num_selected }); const stats_width = numDigits(candidates.len) + numDigits(total_candidates) + numDigits(num_selected) + 4; - _ = try items.printSegment(.{ .text = stats }, .{ .col_offset = items_width - stats_width, .row_offset = 0 }); + _ = items.printSegment(.{ .text = stats }, .{ .col_offset = items_width - stats_width, .row_offset = 0 }); } else { const stats = try std.fmt.bufPrint(&buf, "{}/{}", .{ candidates.len, total_candidates }); const stats_width = numDigits(candidates.len) + numDigits(total_candidates) + 1; - _ = try items.printSegment(.{ .text = stats }, .{ .col_offset = items_width - stats_width, .row_offset = 0 }); + _ = items.printSegment(.{ .text = stats }, .{ .col_offset = items_width - stats_width, .row_offset = 0 }); } } // draw the prompt // TODO: handle display of queries longer than the screen width // const query_width = state.query.slice().len; - _ = try items.print(&.{ + _ = items.print(&.{ .{ .text = state.config.prompt }, .{ .text = state.query.slice() }, }, .{ .col_offset = 0, .row_offset = 0 }); @@ -349,15 +349,16 @@ pub const State = struct { const preview_win = win.child(.{ .x_off = items_width, .y_off = 0, - .height = .{ .limit = state.config.height }, - .width = .{ .limit = preview_width }, + .height = state.config.height, + .width = preview_width, .border = .{ .where = .left }, }); var lines = std.mem.splitScalar(u8, preview.output, '\n'); - for (0..height) |l| { + for (0..height) |captured_l| { + const l: u16 = @intCast(captured_l); if (lines.next()) |preview_line| { - _ = try preview_win.printSegment( + _ = preview_win.printSegment( .{ .text = preview_line }, .{ .row_offset = l, .wrap = .none }, ); @@ -365,14 +366,15 @@ pub const State = struct { } } - items.showCursor(state.config.prompt.len + state.query.cursor, 0); + const config_prompt_len: u16 = @intCast(state.config.prompt.len); + items.showCursor(config_prompt_len + state.query.cursor, 0); try state.vx.render(state.tty.anyWriter()); } fn drawCandidate( state: *State, win: vaxis.Window, - line: usize, + line: u16, str: []const u8, tokens: [][]const u8, selected: bool, @@ -383,7 +385,7 @@ pub const State = struct { // no highlights, just output the string if (matches.len == 0) { - _ = try win.print(&.{ + _ = win.print(&.{ .{ .text = if (selected) "* " else " " }, .{ .text = str, @@ -397,7 +399,7 @@ pub const State = struct { } else { var slicer = HighlightSlicer.init(str, matches); - var res = try win.printSegment(.{ + var res = win.printSegment(.{ .text = if (selected) "* " else " ", }, .{ .row_offset = line, @@ -413,7 +415,7 @@ pub const State = struct { } else .default, }; - res = try win.printSegment(.{ + res = win.printSegment(.{ .text = slice.str, .style = highlight_style, }, .{