diff --git a/build.zig.zon b/build.zig.zon index 17433f5..de8fddb 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -21,16 +21,16 @@ .hash = "1220ced9b2f6943f688ce1e20a40f143d83c4182320e4b9d30e470b010d44b356f0a", }, .zf = .{ - .url = "https://github.com/natecraddock/zf/archive/91bc9808bebddd5e1663ffde009bf4510a45e48d.tar.gz", - .hash = "1220a763e0496d7dade9329044fbac181eaf7c1a1f4f51ab5e3a0a77d986615aa4e4", + .url = "https://github.com/natecraddock/zf/archive/ed99ca18b02dda052e20ba467e90b623c04690dd.tar.gz", + .hash = "1220edc3b8d8bedbb50555947987e5e8e2f93871ca3c8e8d4cc8f1377c15b5dd35e8", }, .zeit = .{ .url = "https://github.com/rockorager/zeit/archive/1d2dc95d73160096f84830e54b419514e41e78e8.tar.gz", .hash = "1220aad3a3b05b27a2453ddb68caa70a656c530f69e321cf79a89d2a9c4b2dd51640", }, .vaxis = .{ - .url = "https://github.com/rockorager/libvaxis/archive/d36ab043caf7cbb24cae1bd0346dd6b654df0653.tar.gz", - .hash = "12206c252ee00b9dd0214989dfa35a6eea29ac7d65d4053817f161f4c23b6e09dd89", + .url = "https://github.com/water-sucks/libvaxis/archive/2dce933ccca04a87bc6ccf690ddf516516cb2acb.tar.gz", + .hash = "12208b59a13aa5211bc93dcdc00c05ca171f87a7d1ca3599aeaa83bb3bf1df388eb1", }, .koino = .{ .url = "https://github.com/kivikakk/koino/archive/0151bb37714d93688f31e3d7a3d0369106818f26.tar.gz", diff --git a/package.nix b/package.nix index c3da24f..e4c036d 100644 --- a/package.nix +++ b/package.nix @@ -19,7 +19,7 @@ stdenv.mkDerivation rec { inherit stdenv zig; name = pname; src = ./.; - depsHash = "sha256-TNiG5pxvESsAWCAz4BEwie+CMO6ZoNKdnm3qo6wNc4c="; + depsHash = "sha256-HvGYGbvfhgdVY9i6HrEMyhtLlWb8xo6G3zvoUz7LMas="; }; in '' mkdir -p .cache diff --git a/src/generation/ui.zig b/src/generation/ui.zig index ec8be13..08f0903 100644 --- a/src/generation/ui.zig +++ b/src/generation/ui.zig @@ -84,8 +84,9 @@ pub const GenerationTUI = struct { .gen_list = gen_list, .gen_list_ctx = .{ + .active_bg = .{ .index = 1 }, .selected_bg = .{ .index = 1 }, - .row = current_gen_idx, + .row = @truncate(current_gen_idx), }, .candidate_filter_buf = candidate_filter_buf, .filtered_gen_list = initial_filtered_gen_slice, @@ -148,7 +149,7 @@ pub const GenerationTUI = struct { } else { try self.search_input.update(.{ .key_press = key }); - const search_query = self.search_input.buf.items; + const search_query = self.getSearchQuery(); const tokens: []const []const u8 = toks: { var items = ArrayList([]const u8).init(allocator); errdefer items.deinit(); @@ -165,7 +166,7 @@ pub const GenerationTUI = struct { self.gen_list_ctx.row = if (self.filtered_gen_list.len == 0) 0 else - self.filtered_gen_list.len - 1; + @truncate(self.filtered_gen_list.len - 1); } } else { if (key.matchesAny(&.{ vaxis.Key.up, 'k' }, .{})) { @@ -226,8 +227,8 @@ pub const GenerationTUI = struct { const root_win = self.vx.window(); var main_win = root_win.child(.{ - .width = .{ .limit = root_win.width / 6 }, - .height = .{ .limit = root_win.height }, + .width = root_win.width / 6, + .height = root_win.height, .border = .{ .where = .all, .style = .{ .fg = .{ .index = 7 } }, @@ -238,13 +239,13 @@ pub const GenerationTUI = struct { .text = "Generations", .style = .{ .bold = true }, }; - const title_bar_win: vaxis.Window = main_win.child(.{ .height = .{ .limit = 1 } }); + const title_bar_win: vaxis.Window = main_win.child(.{ .height = 1 }); const centered: vaxis.Window = vaxis.widgets.alignment.center(title_bar_win, title_seg.text.len, 2); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); const table_win: vaxis.Window = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); const gen_list = self.filtered_gen_list; @@ -262,9 +263,9 @@ pub const GenerationTUI = struct { if (ctx.row < ctx.start) break :tableStart ctx.start - (ctx.start - ctx.row); if (ctx.row >= gen_list.len - 1) - ctx.row = gen_list.len - 1; + ctx.row = @truncate(gen_list.len - 1); if (ctx.row >= end) - break :tableStart ctx.start + (ctx.row - end + 1); + break :tableStart @truncate(ctx.start + (ctx.row - end + 1)); break :tableStart ctx.start; }; end = ctx.start + max_items; @@ -276,9 +277,9 @@ pub const GenerationTUI = struct { const gen = data.value; const tile = table_win.child(.{ - .y_off = i, - .width = .{ .limit = table_win.width }, - .height = .{ .limit = 1 }, + .y_off = @intCast(i), + .width = table_win.width, + .height = 1, }); const generation_segment: vaxis.Segment = .{ @@ -298,18 +299,18 @@ pub const GenerationTUI = struct { break :blk false; }; - _ = try tile.printSegment(generation_segment, .{ .col_offset = 3 }); + _ = tile.printSegment(generation_segment, .{ .col_offset = 3 }); if (deletion_toggled) { - _ = try tile.printSegment(.{ .text = "*" }, .{ .col_offset = 1 }); + _ = tile.printSegment(.{ .text = "*" }, .{ .col_offset = 1 }); } } const mode_win: vaxis.Window = main_win.child(.{ .y_off = main_win.height - 1, - .height = .{ .limit = 1 }, + .height = 1, }); const mode_seg: vaxis.Segment = .{ .text = try fmt.allocPrint(allocator, "{s}", .{@tagName(self.mode)}) }; - _ = try mode_win.printSegment(mode_seg, .{}); + _ = mode_win.printSegment(mode_seg, .{}); return main_win; } @@ -321,8 +322,8 @@ pub const GenerationTUI = struct { const search_bar_win = root_win.child(.{ .x_off = root_win.width / 6, .y_off = root_win.height - 3, - .width = .{ .limit = root_win.width - (root_win.width / 6) }, - .height = .{ .limit = 3 }, + .width = root_win.width - (root_win.width / 6), + .height = 3, .border = .{ .where = .all, .style = .{ .fg = .{ .index = 7 } }, @@ -343,9 +344,9 @@ pub const GenerationTUI = struct { .text = value, .style = .{ .italic = true }, }; - _ = try win.printSegment(gen_number_title_seg, .{ .row_offset = row_offset.*, .col_offset = 3 }); + _ = win.printSegment(gen_number_title_seg, .{ .row_offset = @truncate(row_offset.*), .col_offset = 3 }); row_offset.* += 1; - _ = try win.printSegment(gen_number_seg, .{ .row_offset = row_offset.*, .col_offset = 3 }); + _ = win.printSegment(gen_number_seg, .{ .row_offset = @truncate(row_offset.*), .col_offset = 3 }); row_offset.* += 2; } @@ -355,8 +356,8 @@ pub const GenerationTUI = struct { const main_win: vaxis.Window = root_win.child(.{ .x_off = root_win.width / 6, - .width = .{ .limit = root_win.width / 2 }, - .height = .{ .limit = root_win.height - 3 }, + .width = root_win.width / 2, + .height = root_win.height - 3, .border = .{ .where = .all, .style = .{ .fg = .{ .index = 7 } }, @@ -367,13 +368,13 @@ pub const GenerationTUI = struct { .text = "Information", .style = .{ .bold = true }, }; - const title_bar_win: vaxis.Window = main_win.child(.{ .height = .{ .limit = 1 } }); + const title_bar_win: vaxis.Window = main_win.child(.{ .height = 1 }); const centered: vaxis.Window = vaxis.widgets.alignment.center(title_bar_win, title_seg.text.len, 2); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); const info_win: vaxis.Window = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); if (self.filtered_gen_list.len == 0) { @@ -381,7 +382,7 @@ pub const GenerationTUI = struct { .text = "No generations found", .style = .{ .italic = true }, }; - _ = try info_win.printSegment(no_selected_gen_seg, .{ .col_offset = 3 }); + _ = info_win.printSegment(no_selected_gen_seg, .{ .col_offset = 3 }); return main_win; } @@ -420,8 +421,8 @@ pub const GenerationTUI = struct { const main_win: vaxis.Window = root.child(.{ .x_off = (root.width * 2) / 3, - .width = .{ .limit = root.width / 3 }, - .height = .{ .limit = root.height / 2 }, + .width = root.width / 3, + .height = root.height / 2, .border = .{ .where = .all, .style = .{ @@ -434,13 +435,13 @@ pub const GenerationTUI = struct { .text = "Selected Generations", .style = .{ .bold = true }, }; - const title_win: vaxis.Window = main_win.child(.{ .height = .{ .limit = 1 } }); + const title_win: vaxis.Window = main_win.child(.{ .height = 1 }); const centered: vaxis.Window = vaxis.widgets.alignment.center(title_win, title_seg.text.len, 2); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); const info_win: vaxis.Window = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); const selected_gens = self.gens_to_delete.items; @@ -452,7 +453,7 @@ pub const GenerationTUI = struct { } break :blk try utils.concatStringsSep(allocator, gen_number_strs.items, ", "); }; - _ = try info_win.printSegment(.{ .text = selected_gens_str }, .{ .wrap = .word }); + _ = info_win.printSegment(.{ .text = selected_gens_str }, .{ .wrap = .word }); return main_win; } @@ -464,8 +465,8 @@ pub const GenerationTUI = struct { const main_win: vaxis.Window = root_win.child(.{ .x_off = (root_win.width * 2) / 3, .y_off = root_win.height / 2, - .width = .{ .limit = root_win.width / 3 }, - .height = .{ .limit = root_win.height / 2 - 2 }, + .width = root_win.width / 3, + .height = root_win.height / 2 - 2, .border = .{ .where = .all, .style = .{ .fg = .{ .index = 7 } }, @@ -476,13 +477,13 @@ pub const GenerationTUI = struct { .text = "Keybinds", .style = .{ .bold = true }, }; - const title_win: vaxis.Window = main_win.child(.{ .height = .{ .limit = 1 } }); + const title_win: vaxis.Window = main_win.child(.{ .height = 1 }); const centered: vaxis.Window = vaxis.widgets.alignment.center(title_win, title_seg.text.len, 2); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); const info_win: vaxis.Window = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); const keybinds: []const []const []const u8 = &.{ @@ -516,14 +517,18 @@ pub const GenerationTUI = struct { .text = " :: " ++ key_desc, }; - _ = try info_win.printSegment(key_seg, .{ .row_offset = row_offset, .col_offset = 1 }); - _ = try info_win.printSegment(key_desc_seg, .{ .row_offset = row_offset, .col_offset = desc_col_offset }); + _ = info_win.printSegment(key_seg, .{ .row_offset = row_offset, .col_offset = 1 }); + _ = info_win.printSegment(key_desc_seg, .{ .row_offset = row_offset, .col_offset = desc_col_offset }); row_offset += 1; } return main_win; } + fn getSearchQuery(self: Self) []const u8 { + return self.search_input.buf.buffer[0..self.search_input.buf.realLength()]; + } + pub fn deinit(self: *Self) void { self.gens_to_delete.deinit(); self.allocator.free(self.candidate_filter_buf); diff --git a/src/option/ui.zig b/src/option/ui.zig index dda5dfe..8d61a90 100644 --- a/src/option/ui.zig +++ b/src/option/ui.zig @@ -204,7 +204,7 @@ pub const OptionSearchTUI = struct { break :blk; } if (key.matches(vaxis.Key.enter, .{})) { - if (self.search_input.buf.items.len == 0 or self.option_results.len == 0) break :blk; + if (self.search_input.buf.realLength() == 0 or self.option_results.len == 0) break :blk; self.active_window = .value; const orig_counter = self.value_cmd_ctr.fetchAdd(1, .seq_cst); const thread = std.Thread.spawn(.{ .allocator = self.allocator }, OptionSearchTUI.evaluateOptionValue, .{ self, orig_counter, loop }) catch null; @@ -234,9 +234,7 @@ pub const OptionSearchTUI = struct { break :blk; } else { try self.search_input.update(.{ .key_press = key }); - - const tokens = try utils.splitScalarAlloc(allocator, self.search_input.buf.items, ' '); - + const tokens = try utils.splitScalarAlloc(allocator, self.getSearchQuery(), ' '); const results = utils.search.rankCandidatesStruct(NixosOption, "name", self.candidate_filter_buf, self.options, tokens, true, true); std.sort.block(OptionCandidate, results, {}, compareOptionCandidates); ctx.row = 0; @@ -292,11 +290,11 @@ pub const OptionSearchTUI = struct { }; const help_prompt_row_win = win.child(.{ .y_off = win.height - 2, - .height = .{ .limit = 1 }, + .height = 1, }); const centered = vaxis.widgets.alignment.center(help_prompt_row_win, help_seg.text.len, 1); if (self.active_window != .value) { - _ = try centered.printSegment(help_seg, .{}); + _ = centered.printSegment(help_seg, .{}); } _ = try self.drawResultsList(root_win, allocator); @@ -319,7 +317,7 @@ pub const OptionSearchTUI = struct { }); const title_win = main_win.child(.{ - .height = .{ .limit = 2 }, + .height = 2, .border = .{ .where = .bottom, .style = .{ .fg = .{ .index = 7 } }, @@ -331,12 +329,12 @@ pub const OptionSearchTUI = struct { .style = .{ .bold = true }, }; const centered = vaxis.widgets.alignment.center(title_win, title_seg.text.len, 1); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); const info_win = main_win.child(.{ .y_off = 2, .x_off = 1, - .height = .{ .limit = root_win.height - 2 }, + .height = root_win.height - 2, }); main_win.hideCursor(); @@ -414,8 +412,8 @@ pub const OptionSearchTUI = struct { fn drawResultsList(self: *Self, root_win: vaxis.Window, allocator: Allocator) !vaxis.Window { var main_win = root_win.child(.{ - .width = .{ .limit = root_win.width / 2 }, - .height = .{ .limit = root_win.height - 3 }, + .width = root_win.width / 2, + .height = root_win.height - 3, .border = .{ .where = .all, .style = .{ .fg = .{ .index = if (self.active_window == .input) 5 else 7 } }, @@ -423,15 +421,15 @@ pub const OptionSearchTUI = struct { }, }); - const query = self.search_input.buf.items; + const query = self.getSearchQuery(); const title_seg: vaxis.Segment = .{ .text = "Results", .style = .{ .bold = true }, }; - const title_win: vaxis.Window = main_win.child(.{ .height = .{ .limit = 1 } }); + const title_win: vaxis.Window = main_win.child(.{ .height = 1 }); const centered: vaxis.Window = vaxis.widgets.alignment.center(title_win, title_seg.text.len, 2); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); // Don't show all results if the search bar is empty. if (query.len == 0) { @@ -440,7 +438,7 @@ pub const OptionSearchTUI = struct { const table_win: vaxis.Window = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); const options = self.option_results; @@ -472,7 +470,7 @@ pub const OptionSearchTUI = struct { if (end > options.len) end = options.len; const selected_row = ctx.row; - const tokens = try utils.splitScalarAlloc(allocator, self.search_input.buf.items, ' '); + const tokens = try utils.splitScalarAlloc(allocator, self.getSearchQuery(), ' '); const matches_buf = blk: { var size: usize = 0; @@ -486,9 +484,9 @@ pub const OptionSearchTUI = struct { const option = data.value; const tile = table_win.child(.{ - .y_off = table_win.height -| i -| 1, - .width = .{ .limit = table_win.width }, - .height = .{ .limit = 1 }, + .y_off = @intCast(table_win.height -| i -| 1), + .width = table_win.width, + .height = 1, }); const selected = ctx.start + i == selected_row; @@ -504,9 +502,9 @@ pub const OptionSearchTUI = struct { .bg = tile_bg, }, }; - _ = try tile.printSegment(option_name_seg, .{ .col_offset = 3 }); + _ = tile.printSegment(option_name_seg, .{ .col_offset = 3 }); - const matches = zf.highlight(option.name, tokens, true, true, matches_buf); + const matches = zf.highlight(option.name, tokens, matches_buf, .{ .plain = true, .to_lower = false }); for (matches) |idx| { const cell: vaxis.Cell = .{ .char = .{ .grapheme = option.name[idx..(idx + 1)] }, @@ -515,7 +513,7 @@ pub const OptionSearchTUI = struct { .bg = tile_bg, }, }; - tile.writeCell(3 + idx, 0, cell); + tile.writeCell(@truncate(3 + idx), 0, cell); } if (selected) { @@ -526,7 +524,7 @@ pub const OptionSearchTUI = struct { .bg = tile_bg, }, }; - _ = try tile.printSegment(selected_arrow_seg, .{}); + _ = tile.printSegment(selected_arrow_seg, .{}); } } @@ -534,12 +532,12 @@ pub const OptionSearchTUI = struct { } fn drawSearchBar(self: *Self, root_win: vaxis.Window, allocator: Allocator) !vaxis.Window { - const query = self.search_input.buf.items; + const query = self.search_input.buf.buffer; const search_bar_win = root_win.child(.{ .y_off = root_win.height - 3, - .width = .{ .limit = root_win.width / 2 }, - .height = .{ .limit = 3 }, + .width = root_win.width / 2, + .height = 3, .border = .{ .where = .all, .style = .{ .fg = .{ .index = if (self.active_window == .input) 5 else 7 } }, @@ -553,23 +551,23 @@ pub const OptionSearchTUI = struct { .fg = .{ .index = 4 }, }, }; - _ = try search_bar_win.printSegment(.{ .text = ">" }, .{}); + _ = search_bar_win.printSegment(.{ .text = ">" }, .{}); const count_seg: vaxis.Segment = .{ .text = if (query.len != 0) try fmt.allocPrint(allocator, "{d} / {d}", .{ self.option_results.len, self.options.len }) else "", .style = .{ .fg = .{ .index = 4 } }, }; if (count_seg.text.len != 0) { - _ = try search_bar_win.printSegment(count_seg, .{ .col_offset = search_bar_win.width - count_seg.text.len }); + _ = search_bar_win.printSegment(count_seg, .{ .col_offset = @truncate(search_bar_win.width - count_seg.text.len) }); } var input_win = search_bar_win.child(.{ .x_off = 2, - .width = .{ .limit = search_bar_win.width - 2 - count_seg.text.len }, + .width = @truncate(search_bar_win.width - 2 - count_seg.text.len), }); self.search_input.draw(input_win); - if (self.search_input.buf.items.len == 0) { - _ = try input_win.printSegment(placeholder_seg, .{}); + if (self.search_input.buf.buffer.len == 0) { + _ = input_win.printSegment(placeholder_seg, .{}); } return search_bar_win; @@ -578,7 +576,7 @@ pub const OptionSearchTUI = struct { fn drawResultPreview(self: *Self, root_win: vaxis.Window, allocator: Allocator) !vaxis.Window { const main_win = root_win.child(.{ .x_off = root_win.width / 2, - .width = .{ .limit = root_win.width / 2 }, + .width = root_win.width / 2, .border = .{ .where = .all, .style = .{ .fg = .{ .index = if (self.active_window == .preview) 5 else 7 } }, @@ -590,21 +588,21 @@ pub const OptionSearchTUI = struct { .text = "Option Preview", .style = .{ .bold = true }, }; - const title_win: vaxis.Window = main_win.child(.{ .height = .{ .limit = 1 } }); + const title_win: vaxis.Window = main_win.child(.{ .height = 1 }); const centered: vaxis.Window = vaxis.widgets.alignment.center(title_win, title_seg.text.len, 2); - _ = try centered.printSegment(title_seg, .{}); + _ = centered.printSegment(title_seg, .{}); const info_win: vaxis.Window = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); - if (self.search_input.buf.items.len == 0) { + if (self.search_input.buf.buffer.len == 0) { return main_win; } if (self.option_results.len == 0) { - _ = try info_win.printSegment(.{ + _ = info_win.printSegment(.{ .text = "No results found.", .style = .{ .italic = true, @@ -697,7 +695,7 @@ pub const OptionSearchTUI = struct { buf.clear(self.allocator); const title_win = main_win.child(.{ - .height = .{ .limit = 2 }, + .height = 2, .border = .{ .where = .bottom, .style = .{ .fg = .{ .index = 7 } }, @@ -708,12 +706,12 @@ pub const OptionSearchTUI = struct { .text = opt_name, .style = .{ .bold = true }, }; - const title_centered_win = vaxis.widgets.alignment.center(title_win, title_seg.text.len, 1); - _ = try title_centered_win.printSegment(title_seg, .{}); + const title_centered_win = vaxis.widgets.alignment.center(title_win, @truncate(title_seg.text.len), 1); + _ = title_centered_win.printSegment(title_seg, .{}); const info_win = main_win.child(.{ .y_off = 2, - .height = .{ .limit = main_win.height - 2 }, + .height = main_win.height - 2, }); switch (self.option_eval_value) { @@ -755,6 +753,10 @@ pub const OptionSearchTUI = struct { } } + fn getSearchQuery(self: Self) []const u8 { + return self.search_input.buf.buffer[0..self.search_input.buf.realLength()]; + } + pub fn deinit(self: *Self) void { self.allocator.free(self.candidate_filter_buf); self.value_view_buf.deinit(self.allocator); diff --git a/src/utils/search.zig b/src/utils/search.zig index e83fa77..a1d1583 100644 --- a/src/utils/search.zig +++ b/src/utils/search.zig @@ -31,7 +31,7 @@ pub fn rankCandidates( var index: usize = 0; for (candidates) |candidate| { - if (zf.rank(candidate, tokens, case_sensitive, plain)) |rank| { + if (zf.rank(candidate, tokens, .{ .plain = plain, .to_lower = !case_sensitive })) |rank| { ranked[index] = .{ .str = candidate, .rank = rank }; index += 1; } @@ -79,7 +79,7 @@ pub fn rankCandidatesStruct( } }; - if (zf.rank(candidate_str, tokens, case_sensitive, plain)) |rank| { + if (zf.rank(candidate_str, tokens, .{ .plain = plain, .to_lower = !case_sensitive })) |rank| { ranked[index] = .{ .value = candidate, .rank = rank }; index += 1; } diff --git a/src/utils/vaxis.zig b/src/utils/vaxis.zig index bc7e74a..eae9b9a 100644 --- a/src/utils/vaxis.zig +++ b/src/utils/vaxis.zig @@ -16,7 +16,7 @@ pub fn appendToTextBuffer(allocator: Allocator, vx: vaxis.Vaxis, buf: *TextViewB try buf.append(allocator, .{ .bytes = content, - .gd = &vx.unicode.grapheme_data, + .gd = &vx.unicode.width_data.g_data, .wd = &vx.unicode.width_data, });