Skip to content

Commit

Permalink
fix: update tui code to work with latest libvaxis
Browse files Browse the repository at this point in the history
  • Loading branch information
furtidev committed Nov 13, 2024
1 parent 3776b97 commit 24aabb3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
13 changes: 7 additions & 6 deletions src/tui/EditBuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/tui/opts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
Expand Down
32 changes: 17 additions & 15 deletions src/tui/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });
Expand All @@ -349,30 +349,32 @@ 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 },
);
}
}
}

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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -413,7 +415,7 @@ pub const State = struct {
} else .default,
};

res = try win.printSegment(.{
res = win.printSegment(.{
.text = slice.str,
.style = highlight_style,
}, .{
Expand Down

0 comments on commit 24aabb3

Please sign in to comment.