feat(task): add project tasks

This commit is contained in:
CJ van den Berg 2025-01-25 20:25:03 +01:00
parent 6130aa6b79
commit 2f7bee1bef
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
8 changed files with 433 additions and 5 deletions

View file

@ -0,0 +1,123 @@
const std = @import("std");
const tp = @import("thespian");
const cbor = @import("cbor");
const log = @import("log");
const root = @import("root");
const input = @import("input");
const keybind = @import("keybind");
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
pub fn Create(options: type) type {
return struct {
allocator: std.mem.Allocator,
input: std.ArrayList(u8),
commands: Commands = undefined,
const Commands = command.Collection(cmds);
const Self = @This();
pub fn create(allocator: std.mem.Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self);
self.* = .{
.allocator = allocator,
.input = std.ArrayList(u8).init(allocator),
};
try self.commands.init(self);
if (@hasDecl(options, "restore_state"))
options.restore_state(self) catch {};
var mode = try keybind.mode("mini/buffer", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = options.name(self) } };
}
pub fn deinit(self: *Self) void {
self.commands.deinit();
self.input.deinit();
self.allocator.destroy(self);
}
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
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.update_mini_mode_text();
return false;
}
fn message(comptime fmt: anytype, args: anytype) void {
var buf: [256]u8 = undefined;
tp.self_pid().send(.{ "message", std.fmt.bufPrint(&buf, fmt, args) catch @panic("too large") }) catch {};
}
fn update_mini_mode_text(self: *Self) void {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = self.input.items;
mini_mode.cursor = tui.egc_chunk_width(self.input.items, 0, 8);
}
}
const cmds = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn mini_mode_reset(self: *Self, _: Ctx) Result {
self.input.clearRetainingCapacity();
self.update_mini_mode_text();
}
pub const mini_mode_reset_meta = .{ .description = "Clear input" };
pub fn mini_mode_cancel(_: *Self, _: Ctx) Result {
command.executeName("exit_mini_mode", .{}) catch {};
}
pub const mini_mode_cancel_meta = .{ .description = "Cancel input" };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
if (self.input.items.len > 0) {
self.input.shrinkRetainingCapacity(self.input.items.len - tui.egc_last(self.input.items).len);
}
self.update_mini_mode_text();
}
pub const mini_mode_delete_backwards_meta = .{ .description = "Delete backwards" };
pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result {
var egc: u32 = 0;
if (!try ctx.args.match(.{tp.extract(&egc)}))
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]);
self.update_mini_mode_text();
}
pub const mini_mode_insert_code_point_meta = .{ .arguments = &.{.integer} };
pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result {
var bytes: []const u8 = undefined;
if (!try ctx.args.match(.{tp.extract(&bytes)}))
return error.InvalidMiniBufferInsertBytesArgument;
try self.input.appendSlice(bytes);
self.update_mini_mode_text();
}
pub const mini_mode_insert_bytes_meta = .{ .arguments = &.{.string} };
pub fn mini_mode_select(self: *Self, _: Ctx) Result {
options.select(self);
self.update_mini_mode_text();
}
pub const mini_mode_select_meta = .{ .description = "Select" };
pub fn mini_mode_paste(self: *Self, ctx: Ctx) Result {
return mini_mode_insert_bytes(self, ctx);
}
pub const mini_mode_paste_meta = .{ .arguments = &.{.string} };
};
};
}

View file

@ -29,6 +29,10 @@ pub fn load_entries(palette: *Type) !usize {
return if (palette.entries.items.len == 0) label.len else 2;
}
pub fn clear_entries(palette: *Type) void {
palette.entries.clearRetainingCapacity();
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
defer value.deinit();

View file

@ -432,7 +432,7 @@ pub fn Create(options: type) type {
const button = self.menu.get_selected() orelse return;
const refresh = options.delete_item(self.menu, button);
if (refresh) {
self.entries.clearRetainingCapacity();
options.clear_entries(self);
self.longest_hint = try options.load_entries(self);
if (self.entries.items.len > 0)
self.initial_selected = self.menu.selected;

View file

@ -0,0 +1,71 @@
const std = @import("std");
const cbor = @import("cbor");
const tp = @import("thespian");
const command = @import("command");
const project_manager = @import("project_manager");
const tui = @import("../../tui.zig");
pub const Type = @import("palette.zig").Create(@This());
const module_name = @typeName(@This());
pub const label = "Run a task";
pub const name = " task";
pub const description = "task";
pub const Entry = struct {
label: []const u8,
hint: []const u8,
};
pub fn deinit(palette: *Type) void {
clear_entries(palette);
}
pub fn load_entries(palette: *Type) !usize {
const rsp = try project_manager.request_tasks(palette.allocator);
defer palette.allocator.free(rsp.buf);
var iter: []const u8 = rsp.buf;
var len = try cbor.decodeArrayHeader(&iter);
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), .hint = "" };
} else return error.InvalidTaskMessageField;
}
return if (palette.entries.items.len == 0) label.len else 1;
}
pub fn clear_entries(palette: *Type) void {
for (palette.entries.items) |entry|
palette.allocator.free(entry.label);
palette.entries.clearRetainingCapacity();
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.allocator);
defer value.deinit();
const writer = value.writer();
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.hint);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
palette.items += 1;
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
var task: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &task) catch false)) return;
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "add_task", .{task} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "create_scratch_buffer", .{"*task*"} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "shell_execute_insert", .{task} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
}
pub fn delete_item(menu: *Type.MenuState, button: *Type.ButtonState) bool {
var task: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &task) catch false)) return false;
command.executeName("delete_task", command.fmt(.{task})) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
return true; //refresh list
}