Compare commits

...

3 commits

4 changed files with 39 additions and 6 deletions

View file

@ -381,7 +381,7 @@ pub fn request_n_most_recent_file(self: *Self, from: tp.pid_ref, n: usize) Clien
}
pub fn request_recent_files(self: *Self, from: tp.pid_ref, max: usize) ClientError!void {
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, "" }) catch {};
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, "", self.files.items.len }) catch {};
for (self.files.items, 0..) |file, i| {
from.send(.{ "PRJ", "recent", self.longest_file_path, file.path, file.type, file.icon, file.color }) catch return error.ClientFailed;
if (i >= max) return;
@ -468,7 +468,7 @@ pub fn request_new_or_modified_files(self: *Self, from: tp.pid_ref, max: usize)
fn simple_query_recent_files(self: *Self, from: tp.pid_ref, max: usize, query: []const u8) ClientError!usize {
var i: usize = 0;
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, query }) catch {};
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, query, self.files.items.len }) catch {};
for (self.files.items) |file| {
if (file.path.len < query.len) continue;
if (std.mem.indexOf(u8, file.path, query)) |idx| {
@ -487,7 +487,7 @@ fn simple_query_recent_files(self: *Self, from: tp.pid_ref, max: usize, query: [
pub fn query_recent_files(self: *Self, from: tp.pid_ref, max: usize, query: []const u8) ClientError!usize {
if (query.len < 3)
return self.simple_query_recent_files(from, max, query);
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, query }) catch {};
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, query, self.files.items.len }) catch {};
var searcher = try fuzzig.Ascii.init(
self.allocator,

View file

@ -10,6 +10,7 @@ const tui = @import("tui.zig");
pub fn Options(context: type) type {
return struct {
label: []const u8 = "Enter text",
hint: ?[]const u8 = null,
pos: Widget.Box = .{ .y = 0, .x = 0, .w = 12, .h = 1 },
ctx: Context,
padding: u8 = 1,
@ -24,13 +25,21 @@ pub fn Options(context: type) type {
pub fn on_render_default(_: context, self: *State(Context), theme: *const Widget.Theme) bool {
const style_base = theme.editor_widget;
const style_label = if (self.text.items.len > 0) theme.input else theme.input_placeholder;
const style_input_placeholder = theme.input_placeholder;
const style_label = if (self.text.items.len > 0) theme.input else style_input_placeholder;
self.plane.set_base_style(style_base);
self.plane.erase();
self.plane.home();
self.plane.set_style(style_label);
self.plane.fill(" ");
self.plane.home();
if (self.hint.items.len > 0) {
const hint = self.hint.items;
self.plane.set_style(style_input_placeholder);
_ = self.plane.print_aligned_right(0, "{s} ", .{hint}) catch {};
self.plane.home();
self.plane.set_style(style_label);
}
for (0..self.opts.padding) |_| _ = self.plane.putchar(" ");
if (self.icon_width > 0) if (self.opts.icon) |icon| {
_ = self.plane.print("{s}", .{icon}) catch {};
@ -74,11 +83,16 @@ pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts:
.plane = n,
.opts = opts,
.label = .empty,
.hint = .empty,
.text = .empty,
.icon_width = @intCast(if (tui.config().show_fileicons) if (opts.icon) |icon| n.egc_chunk_width(icon, 0, 1) else 0 else 0),
};
try self.label.appendSlice(self.allocator, self.opts.label);
self.opts.label = self.label.items;
if (self.opts.hint) |hint| {
try self.hint.appendSlice(self.allocator, hint);
self.opts.hint = self.hint.items;
}
return Widget.to(self);
}
@ -90,6 +104,7 @@ pub fn State(ctx_type: type) type {
active: bool = false,
hover: bool = false,
label: std.ArrayList(u8),
hint: std.ArrayList(u8),
opts: Options(ctx_type),
text: std.ArrayList(u8),
icon_width: c_int,
@ -100,6 +115,7 @@ pub fn State(ctx_type: type) type {
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
self.text.deinit(self.allocator);
self.hint.deinit(self.allocator);
self.label.deinit(self.allocator);
self.plane.deinit();
allocator.destroy(self);

View file

@ -38,6 +38,8 @@ longest: usize,
commands: Commands = undefined,
buffer_manager: ?*BufferManager,
split: enum { none, vertical } = .none,
total_items: usize = 0,
total_files_in_project: usize = 0,
const inputbox_label = "Search files by name";
const MenuType = Menu.Options(*Self).MenuType;
@ -146,6 +148,7 @@ fn add_item(
indicator: []const u8,
matches: ?[]const u8,
) !void {
self.total_items += 1;
var label: std.Io.Writer.Allocating = .init(self.allocator);
defer label.deinit();
const writer = &label.writer;
@ -209,12 +212,14 @@ fn process_project_manager(self: *Self, m: tp.message) MessageFilter.Error!void
self.need_select_first = false;
}
tui.need_render();
} else if (try cbor.match(m.buf, .{ "PRJ", "recent_done", tp.extract(&self.longest), tp.extract(&query) })) {
} else if (try cbor.match(m.buf, .{ "PRJ", "recent_done", tp.extract(&self.longest), tp.extract(&query), tp.extract(&self.total_files_in_project) })) {
self.update_count_hint();
self.query_pending = false;
self.need_reset = true;
if (!std.mem.eql(u8, self.inputbox.text.items, query))
try self.start_query();
} else if (try cbor.match(m.buf, .{ "PRJ", "open_done", tp.string, tp.extract(&self.longest), tp.any })) {
} else if (try cbor.match(m.buf, .{ "PRJ", "open_done", tp.string, tp.extract(&self.longest), tp.extract(&self.total_files_in_project) })) {
self.update_count_hint();
self.query_pending = false;
self.need_reset = true;
try self.start_query();
@ -239,7 +244,13 @@ fn reset_results(self: *Self) void {
self.need_select_first = true;
}
fn update_count_hint(self: *Self) void {
self.inputbox.hint.clearRetainingCapacity();
self.inputbox.hint.print(self.inputbox.allocator, "{d}/{d}", .{ self.total_items, self.total_files_in_project }) catch {};
}
fn start_query(self: *Self) MessageFilter.Error!void {
self.total_items = 0;
if (self.query_pending) return;
self.query_pending = true;
try project_manager.query_recent_files(max_recent_files, self.inputbox.text.items);

View file

@ -291,7 +291,13 @@ pub fn Create(options: type) type {
return false;
}
fn update_count_hint(self: *Self) void {
self.inputbox.hint.clearRetainingCapacity();
self.inputbox.hint.print(self.inputbox.allocator, "{d}/{d}", .{ self.total_items, self.entries.items.len }) catch {};
}
fn start_query(self: *Self, n: usize) !void {
defer self.update_count_hint();
self.items = 0;
self.menu.reset_items();
self.menu.selected = null;