refactor: lots more writergate fixes - first successful build

This commit is contained in:
CJ van den Berg 2025-09-25 22:01:29 +02:00
parent 5094aa8c85
commit bf0d4402ea
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
48 changed files with 404 additions and 413 deletions

View file

@ -235,9 +235,11 @@ const cmds_ = struct {
const ed = mv.get_active_editor() orelse return;
const root = ed.buf_root() catch return;
var first = true;
var text = std.ArrayList(u8).init(ed.allocator);
var buffer: std.Io.Writer.Allocating = .init(ed.allocator);
defer buffer.deinit();
const writer = &buffer.writer;
if (ed.get_primary().selection) |sel| if (sel.begin.col == 0 and sel.end.row > sel.begin.row) try text.appendSlice("\n");
if (ed.get_primary().selection) |sel| if (sel.begin.col == 0 and sel.end.row > sel.begin.row) try writer.writeAll("\n");
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
if (cursel.selection) |sel| {
@ -245,18 +247,19 @@ const cmds_ = struct {
if (first) {
first = false;
} else {
try text.appendSlice("\n");
try writer.writeAll("\n");
}
try text.appendSlice(copy_text);
try writer.writeAll(copy_text);
}
};
if (text.items.len > 0) {
if (text.items.len > 100) {
ed.logger.print("copy:{s}...", .{std.fmt.fmtSliceEscapeLower(text.items[0..100])});
const text = buffer.written();
if (text.len > 0) {
if (text.len > 100) {
ed.logger.print("copy:{f}...", .{std.ascii.hexEscape(text[0..100], .lower)});
} else {
ed.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)});
ed.logger.print("copy:{f}", .{std.ascii.hexEscape(text, .lower)});
}
ed.set_clipboard_internal(text.items);
ed.set_clipboard_internal(text);
}
}
pub const copy_helix_meta: Meta = .{ .description = "Copy selection to clipboard (helix)" };

View file

@ -25,7 +25,7 @@ pub fn Create(options: type) type {
errdefer allocator.destroy(self);
self.* = .{
.allocator = allocator,
.input = std.ArrayList(u8).init(allocator),
.input = .empty,
};
try self.commands.init(self);
if (@hasDecl(options, "restore_state"))
@ -39,7 +39,7 @@ pub fn Create(options: type) type {
pub fn deinit(self: *Self) void {
self.commands.deinit();
self.input.deinit();
self.input.deinit(self.allocator);
self.allocator.destroy(self);
}
@ -47,7 +47,7 @@ pub fn Create(options: type) type {
var text: []const u8 = undefined;
if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
self.input.appendSlice(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.input.appendSlice(self.allocator, text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
self.update_mini_mode_text();
return false;
@ -96,7 +96,7 @@ pub fn Create(options: type) type {
return error.InvalidMiniBufferInsertCodePointArgument;
var buf: [32]u8 = undefined;
const bytes = try input.ucs32_to_utf8(&[_]u32{egc}, &buf);
try self.input.appendSlice(buf[0..bytes]);
try self.input.appendSlice(self.allocator, buf[0..bytes]);
self.update_mini_mode_text();
}
pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} };
@ -105,7 +105,7 @@ pub fn Create(options: type) type {
var bytes: []const u8 = undefined;
if (!try ctx.args.match(.{tp.extract(&bytes)}))
return error.InvalidMiniBufferInsertBytesArgument;
try self.input.appendSlice(bytes);
try self.input.appendSlice(self.allocator, bytes);
self.update_mini_mode_text();
}
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };

View file

@ -47,10 +47,10 @@ pub fn Create(options: type) type {
errdefer allocator.destroy(self);
self.* = .{
.allocator = allocator,
.file_path = std.ArrayList(u8).init(allocator),
.query = std.ArrayList(u8).init(allocator),
.match = std.ArrayList(u8).init(allocator),
.entries = std.ArrayList(Entry).init(allocator),
.file_path = .empty,
.query = .empty,
.match = .empty,
.entries = .empty,
};
try self.commands.init(self);
try tui.message_filters().add(MessageFilter.bind(self, receive_path_entry));
@ -68,10 +68,10 @@ pub fn Create(options: type) type {
self.commands.deinit();
tui.message_filters().remove_ptr(self);
self.clear_entries();
self.entries.deinit();
self.match.deinit();
self.query.deinit();
self.file_path.deinit();
self.entries.deinit(self.allocator);
self.match.deinit(self.allocator);
self.query.deinit(self.allocator);
self.file_path.deinit(self.allocator);
self.rendered_mini_buffer.deinit(self.allocator);
self.allocator.destroy(self);
}
@ -80,7 +80,7 @@ pub fn Create(options: type) type {
var text: []const u8 = undefined;
if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
self.file_path.appendSlice(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.file_path.appendSlice(self.allocator, text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
self.update_mini_mode_text();
return false;
@ -102,14 +102,14 @@ pub fn Create(options: type) type {
self.match.clearRetainingCapacity();
self.clear_entries();
if (root.is_directory(self.file_path.items)) {
try self.query.appendSlice(self.file_path.items);
try self.query.appendSlice(self.allocator, self.file_path.items);
} else if (self.file_path.items.len > 0) blk: {
const basename_begin = std.mem.lastIndexOfScalar(u8, self.file_path.items, std.fs.path.sep) orelse {
try self.match.appendSlice(self.file_path.items);
try self.match.appendSlice(self.allocator, self.file_path.items);
break :blk;
};
try self.query.appendSlice(self.file_path.items[0 .. basename_begin + 1]);
try self.match.appendSlice(self.file_path.items[basename_begin + 1 ..]);
try self.query.appendSlice(self.allocator, self.file_path.items[0 .. basename_begin + 1]);
try self.match.appendSlice(self.allocator, self.file_path.items[basename_begin + 1 ..]);
}
// log.logger("file_browser").print("query: '{s}' match: '{s}'", .{ self.query.items, self.match.items });
try project_manager.request_path_files(max_complete_paths, self.query.items);
@ -125,7 +125,7 @@ pub fn Create(options: type) type {
if (self.match.items.len > 0) {
try self.construct_path(self.query.items, self.match.items, .file, 0);
} else {
try self.file_path.appendSlice(self.query.items);
try self.file_path.appendSlice(self.allocator, self.query.items);
}
self.update_mini_mode_text();
return;
@ -177,7 +177,7 @@ pub fn Create(options: type) type {
}
fn add_entry(self: *Self, file_name: []const u8, entry_type: EntryType, file_type: []const u8, icon: []const u8, color: u24) !void {
(try self.entries.addOne()).* = .{
(try self.entries.addOne(self.allocator)).* = .{
.name = try self.allocator.dupe(u8, file_name),
.type = entry_type,
.file_type = try self.allocator.dupe(u8, file_type),
@ -212,12 +212,12 @@ pub fn Create(options: type) type {
fn construct_path(self: *Self, path_: []const u8, entry_name: []const u8, entry_type: EntryType, entry_no: usize) error{OutOfMemory}!void {
self.matched_entry = entry_no;
const path = project_manager.normalize_file_path(path_);
try self.file_path.appendSlice(path);
try self.file_path.appendSlice(self.allocator, path);
if (path.len > 0 and path[path.len - 1] != std.fs.path.sep)
try self.file_path.append(std.fs.path.sep);
try self.file_path.appendSlice(entry_name);
try self.file_path.append(self.allocator, std.fs.path.sep);
try self.file_path.appendSlice(self.allocator, entry_name);
if (entry_type == .dir)
try self.file_path.append(std.fs.path.sep);
try self.file_path.append(self.allocator, std.fs.path.sep);
}
fn match_path(self: *Self) !void {
@ -351,7 +351,7 @@ pub fn Create(options: type) type {
self.complete_trigger_count = 0;
var buf: [32]u8 = undefined;
const bytes = try input.ucs32_to_utf8(&[_]u32{egc}, &buf);
try self.file_path.appendSlice(buf[0..bytes]);
try self.file_path.appendSlice(self.allocator, buf[0..bytes]);
self.update_mini_mode_text();
}
pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} };
@ -361,7 +361,7 @@ pub fn Create(options: type) type {
if (!try ctx.args.match(.{tp.extract(&bytes)}))
return error.InvalidFileBrowserInsertBytesArgument;
self.complete_trigger_count = 0;
try self.file_path.appendSlice(bytes);
try self.file_path.appendSlice(self.allocator, bytes);
self.update_mini_mode_text();
}
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };

View file

@ -32,8 +32,8 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.
errdefer allocator.destroy(self);
self.* = .{
.allocator = allocator,
.input_ = ArrayList(u8).init(allocator),
.last_input = ArrayList(u8).init(allocator),
.input_ = .empty,
.last_input = .empty,
.start_view = editor.view,
.start_cursor = editor.get_primary().cursor,
.editor = editor,
@ -42,7 +42,7 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.
if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
try self.input_.appendSlice(text);
try self.input_.appendSlice(self.allocator, text);
}
var mode = try keybind.mode("mini/find", allocator, .{
.insert_command = "mini_mode_insert_bytes",
@ -53,8 +53,8 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.
pub fn deinit(self: *Self) void {
self.commands.deinit();
self.input_.deinit();
self.last_input.deinit();
self.input_.deinit(self.allocator);
self.last_input.deinit(self.allocator);
self.allocator.destroy(self);
}
@ -74,11 +74,11 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [16]u8 = undefined;
const bytes = input.ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e, @errorReturnTrace());
try self.input_.appendSlice(buf[0..bytes]);
try self.input_.appendSlice(self.allocator, buf[0..bytes]);
}
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.input_.appendSlice(bytes);
try self.input_.appendSlice(self.allocator, bytes);
}
fn flush_input(self: *Self) !void {
@ -86,7 +86,7 @@ fn flush_input(self: *Self) !void {
if (eql(u8, self.input_.items, self.last_input.items))
return;
self.last_input.clearRetainingCapacity();
try self.last_input.appendSlice(self.input_.items);
try self.last_input.appendSlice(self.allocator, self.input_.items);
self.editor.find_operation = .goto_next_match;
const primary = self.editor.get_primary();
primary.selection = null;
@ -136,7 +136,7 @@ fn find_history_next(self: *Self) void {
fn load_history(self: *Self, pos: usize) void {
if (self.editor.find_history) |*history| {
self.input_.clearRetainingCapacity();
self.input_.appendSlice(history.items[pos]) catch {};
self.input_.appendSlice(self.allocator, history.items[pos]) catch {};
}
}
@ -189,7 +189,7 @@ const cmds = struct {
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
self.input_.resize(self.input_.items.len - tui.egc_last(self.input_.items).len) catch {};
self.input_.resize(self.allocator, self.input_.items.len - tui.egc_last(self.input_.items).len) catch {};
self.update_mini_mode_text();
}
pub const mini_mode_delete_backwards_meta: Meta = .{ .description = "Delete backwards" };

View file

@ -14,18 +14,18 @@ pub fn load_entries(self: *Type) error{ Exit, OutOfMemory }!void {
var project_name_buf: [512]u8 = undefined;
const project_path = tp.env.get().str("project");
const project_name = project_manager.abbreviate_home(&project_name_buf, project_path);
try self.file_path.appendSlice(project_name);
try self.file_path.append(std.fs.path.sep);
try self.file_path.appendSlice(self.allocator, project_name);
try self.file_path.append(self.allocator, std.fs.path.sep);
const editor = tui.get_active_editor() orelse return;
if (editor.file_path) |old_path|
if (std.mem.lastIndexOf(u8, old_path, "/")) |pos|
try self.file_path.appendSlice(old_path[0 .. pos + 1]);
try self.file_path.appendSlice(self.allocator, old_path[0 .. pos + 1]);
if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
if (!(text.len > 2 and std.mem.eql(u8, text[0..2], "..")))
self.file_path.clearRetainingCapacity();
try self.file_path.appendSlice(text);
try self.file_path.appendSlice(self.allocator, text);
}
}
@ -34,9 +34,9 @@ pub fn name(_: *Type) []const u8 {
}
pub fn select(self: *Type) void {
var buf = std.ArrayList(u8).init(self.allocator);
defer buf.deinit();
const file_path = project_manager.expand_home(&buf, self.file_path.items);
var buf: std.ArrayList(u8) = .empty;
defer buf.deinit(self.allocator);
const file_path = project_manager.expand_home(self.allocator, &buf, self.file_path.items);
if (root.is_directory(file_path))
tp.self_pid().send(.{ "cmd", "change_project", .{file_path} }) catch {}
else if (file_path.len > 0)

View file

@ -11,13 +11,13 @@ pub const create = Type.create;
pub fn load_entries(self: *Type) !void {
const editor = tui.get_active_editor() orelse return;
try self.file_path.appendSlice(editor.file_path orelse "");
try self.file_path.appendSlice(self.allocator, editor.file_path orelse "");
if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
if (!(text.len > 2 and std.mem.eql(u8, text[0..2], "..")))
self.file_path.clearRetainingCapacity();
try self.file_path.appendSlice(text);
try self.file_path.appendSlice(self.allocator, text);
}
}

View file

@ -27,7 +27,7 @@ pub fn load_entries(palette: *Type) !usize {
defer palette.allocator.free(buffers);
for (buffers) |buffer| {
const indicator = tui.get_buffer_state_indicator(buffer);
(try palette.entries.addOne()).* = .{
(try palette.entries.addOne(palette.allocator)).* = .{
.label = buffer.get_file_path(),
.icon = buffer.file_type_icon orelse "",
.color = buffer.file_type_color,
@ -42,15 +42,15 @@ pub fn clear_entries(palette: *Type) void {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.icon);
try cbor.writeValue(writer, entry.color);
try cbor.writeValue(writer, entry.indicator);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -28,7 +28,7 @@ pub fn load_entries(palette: *Type) !usize {
const hint = hints.get(p.name) orelse "";
longest_description = @max(longest_description, p.meta.description.len);
longest_total = @max(longest_total, p.meta.description.len + hint.len + 1);
(try palette.entries.addOne()).* = .{
(try palette.entries.addOne(palette.allocator)).* = .{
.label = if (p.meta.description.len > 0) p.meta.description else p.name,
.name = p.name,
.hint = hint,
@ -41,14 +41,14 @@ pub fn load_entries(palette: *Type) !usize {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.hint);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try cbor.writeValue(writer, entry.id);
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}
@ -93,9 +93,10 @@ fn write_state(palette: *Type) !void {
const state_file = try std.fmt.bufPrint(&state_file_buffer, "{s}/{s}", .{ try root.get_state_dir(), "commands" });
var file = try std.fs.createFileAbsolute(state_file, .{ .truncate = true });
defer file.close();
var buffer = std.io.bufferedWriter(file.writer());
defer buffer.flush() catch {};
const writer = buffer.writer();
var buf: [4096]u8 = undefined;
var file_writer = file.writer(&buf);
const writer = &file_writer.interface;
defer writer.flush() catch {};
for (palette.entries.items) |cmd_| {
if (cmd_.used_time == 0) continue;

View file

@ -26,7 +26,7 @@ pub fn load_entries(palette: *Type) !usize {
while (iter.len > 0) {
var cbor_item: []const u8 = undefined;
if (!try cbor.matchValue(&iter, cbor.extract_cbor(&cbor_item))) return error.BadCompletion;
(try palette.entries.addOne()).* = .{ .cbor = cbor_item, .label = undefined, .sort_text = undefined };
(try palette.entries.addOne(palette.allocator)).* = .{ .cbor = cbor_item, .label = undefined, .sort_text = undefined };
}
var max_label_len: usize = 0;
@ -54,12 +54,12 @@ pub fn clear_entries(palette: *Type) void {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try writer.writeAll(entry.cbor);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -44,7 +44,7 @@ pub fn Variant(comptime command: []const u8, comptime label_: []const u8, allow_
for (file_type_config.get_all_names()) |file_type_name| {
const file_type = try file_type_config.get(file_type_name) orelse unreachable;
idx += 1;
(try palette.entries.addOne()).* = .{
(try palette.entries.addOne(palette.allocator)).* = .{
.label = file_type.description orelse file_type_config.default.description,
.name = file_type.name,
.icon = file_type.icon orelse file_type_config.default.icon,
@ -59,15 +59,15 @@ pub fn Variant(comptime command: []const u8, comptime label_: []const u8, allow_
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.icon);
try cbor.writeValue(writer, entry.color);
try cbor.writeValue(writer, entry.name);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -38,7 +38,7 @@ pub fn load_entries(palette: *Type) !usize {
defer palette.allocator.free(fontfaces);
for (fontfaces) |fontface| {
idx += 1;
(try palette.entries.addOne()).* = .{ .label = fontface };
(try palette.entries.addOne(palette.allocator)).* = .{ .label = fontface };
if (previous_fontface) |previous_fontface_| if (std.mem.eql(u8, fontface, previous_fontface_)) {
palette.initial_selected = idx;
};
@ -47,12 +47,12 @@ pub fn load_entries(palette: *Type) !usize {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -26,8 +26,9 @@ pub fn load_entries(palette: *Type) !usize {
const hints = if (tui.input_mode()) |m| m.keybind_hints else @panic("no keybind hints");
var longest_hint: usize = 0;
for (command.commands.items) |cmd_| if (cmd_) |p| {
var label_ = std.ArrayList(u8).init(palette.allocator);
const writer = label_.writer();
var label_: std.Io.Writer.Allocating = .init(palette.allocator);
defer label_.deinit();
const writer = &label_.writer;
try writer.writeAll(p.name);
if (p.meta.description.len > 0) try writer.print(" ({s})", .{p.meta.description});
if (p.meta.arguments.len > 0) {
@ -47,7 +48,7 @@ pub fn load_entries(palette: *Type) !usize {
const hint = hints.get(p.name) orelse "";
longest_hint = @max(longest_hint, hint.len);
(try palette.entries.addOne()).* = .{
(try palette.entries.addOne(palette.allocator)).* = .{
.label = try label_.toOwnedSlice(),
.hint = hint,
.id = p.id,
@ -57,14 +58,14 @@ pub fn load_entries(palette: *Type) !usize {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.hint);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try cbor.writeValue(writer, entry.id);
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -139,15 +139,15 @@ fn add_item(
indicator: []const u8,
matches: ?[]const u8,
) !void {
var label = std.ArrayList(u8).init(self.allocator);
var label: std.Io.Writer.Allocating = .init(self.allocator);
defer label.deinit();
const writer = label.writer();
const writer = &label.writer;
try cbor.writeValue(writer, file_name);
try cbor.writeValue(writer, file_icon);
try cbor.writeValue(writer, file_color);
try cbor.writeValue(writer, indicator);
if (matches) |cb| _ = try writer.write(cb) else try cbor.writeValue(writer, &[_]usize{});
try self.menu.add_item_with_handler(label.items, menu_action_open_file);
try self.menu.add_item_with_handler(label.written(), menu_action_open_file);
}
fn receive_project_manager(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool {
@ -259,13 +259,13 @@ fn delete_code_point(self: *Self) !void {
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [6]u8 = undefined;
const bytes = try input.ucs32_to_utf8(&[_]u32{c}, &buf);
try self.inputbox.text.appendSlice(buf[0..bytes]);
try self.inputbox.text.appendSlice(self.allocator, buf[0..bytes]);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
return self.start_query();
}
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.inputbox.text.appendSlice(bytes);
try self.inputbox.text.appendSlice(self.allocator, bytes);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
return self.start_query();
}

View file

@ -43,7 +43,7 @@ pub fn load_entries_with_args(palette: *Type, ctx: command.Context) !usize {
return error.InvalidMessageField;
if (!try cbor.matchValue(&iter, cbor.extract(&open)))
return error.InvalidMessageField;
(try palette.entries.addOne()).* = .{ .label = try palette.allocator.dupe(u8, name_), .open = open };
(try palette.entries.addOne(palette.allocator)).* = .{ .label = try palette.allocator.dupe(u8, name_), .open = open };
}
return 1;
}
@ -53,13 +53,13 @@ pub fn clear_entries(palette: *Type) void {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, if (entry.open) "-" else "");
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -78,7 +78,7 @@ pub fn Create(options: type) type {
.icon = if (@hasDecl(options, "icon")) options.icon else null,
}))).dynamic_cast(InputBox.State(*Self)) orelse unreachable,
.view_rows = get_view_rows(tui.screen()),
.entries = std.ArrayList(Entry).init(allocator),
.entries = .empty,
};
if (self.menu.scrollbar) |scrollbar| scrollbar.style_factory = scrollbar_style;
self.longest_hint = if (@hasDecl(options, "load_entries_with_args"))
@ -103,7 +103,7 @@ pub fn Create(options: type) type {
self.commands.deinit();
if (@hasDecl(options, "deinit"))
options.deinit(self);
self.entries.deinit();
self.entries.deinit(self.allocator);
tui.message_filters().remove_ptr(self);
if (tui.mainview()) |mv| {
mv.floating_views.remove(self.menu.container_widget);
@ -270,7 +270,7 @@ pub fn Create(options: type) type {
}
}
fn query_entries(self: *Self, query: []const u8) error{OutOfMemory}!usize {
fn query_entries(self: *Self, query: []const u8) error{ OutOfMemory, WriteFailed }!usize {
var searcher = try fuzzig.Ascii.init(
self.allocator,
self.longest, // haystack max size
@ -285,12 +285,12 @@ pub fn Create(options: type) type {
matches: []const usize,
};
var matches = std.ArrayList(Match).init(self.allocator);
var matches: std.ArrayList(Match) = .empty;
for (self.entries.items) |*entry| {
const match = searcher.scoreMatches(entry.label, query);
if (match.score) |score|
(try matches.addOne()).* = .{
(try matches.addOne(self.allocator)).* = .{
.entry = entry,
.score = score,
.matches = try self.allocator.dupe(usize, match.matches),
@ -343,14 +343,14 @@ pub fn Create(options: type) type {
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [6]u8 = undefined;
const bytes = try input.ucs32_to_utf8(&[_]u32{c}, &buf);
try self.inputbox.text.appendSlice(buf[0..bytes]);
try self.inputbox.text.appendSlice(self.allocator, buf[0..bytes]);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
self.view_pos = 0;
return self.start_query(0);
}
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.inputbox.text.appendSlice(bytes);
try self.inputbox.text.appendSlice(self.allocator, bytes);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
self.view_pos = 0;
return self.start_query(0);

View file

@ -30,11 +30,11 @@ pub fn load_entries(palette: *Type) !usize {
while (len > 0) : (len -= 1) {
var task: []const u8 = undefined;
if (try cbor.matchValue(&iter, cbor.extract(&task))) {
(try palette.entries.addOne()).* = .{ .label = try palette.allocator.dupe(u8, task) };
(try palette.entries.addOne(palette.allocator)).* = .{ .label = try palette.allocator.dupe(u8, task) };
} else return error.InvalidTaskMessageField;
}
(try palette.entries.addOne()).* = .{ .label = "", .command = "add_task" };
(try palette.entries.addOne()).* = .{ .label = "", .command = "palette_menu_delete_item" };
(try palette.entries.addOne(palette.allocator)).* = .{ .label = "", .command = "add_task" };
(try palette.entries.addOne(palette.allocator)).* = .{ .label = "", .command = "palette_menu_delete_item" };
return if (palette.entries.items.len == 0) label.len else blk: {
var longest: usize = 0;
for (palette.entries.items) |item| longest = @max(longest, item.label.len);
@ -49,12 +49,12 @@ pub fn clear_entries(palette: *Type) void {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}

View file

@ -30,7 +30,7 @@ pub fn load_entries(palette: *Type) !usize {
previous_theme = tui.theme().name;
for (Widget.themes) |theme| {
idx += 1;
(try palette.entries.addOne()).* = .{
(try palette.entries.addOne(palette.allocator)).* = .{
.label = theme.description,
.name = theme.name,
};
@ -43,13 +43,13 @@ pub fn load_entries(palette: *Type) !usize {
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = value.writer();
const writer = &value.writer;
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.name);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
try palette.menu.add_item_with_handler(value.written(), select);
palette.items += 1;
}