Compare commits

...

4 commits

3 changed files with 75 additions and 22 deletions

View file

@ -15,8 +15,8 @@
.hash = "dizzy-1.0.0-AAAAAM1wAAAiDbx_6RwcVEOBk8p2XOu8t9WPNc3K7kBK",
},
.thespian = .{
.url = "git+https://github.com/neurocyte/thespian#f2980d3a747abdf0d18a01596dd8b953dd3e6243",
.hash = "thespian-0.0.1-owFOjk0aBgC8w9ibeiVdhftyEIaVIHCnubsJWfkktE8v",
.url = "git+https://github.com/neurocyte/thespian?ref=master#0a386496cda74ef827d5770f6f071a7d2d54b91a",
.hash = "thespian-0.0.1-owFOjlgaBgCAOkRjH9_mDN7dnL8n8K3XA2hqqchjXZIk",
},
.themes = .{
.url = "https://github.com/neurocyte/flow-themes/releases/download/master-952f9f630ea9544088fd30293666ee0650b7a690/flow-themes.tar.gz",

View file

@ -1494,14 +1494,16 @@ pub fn write_state(self: *const Self, writer: MetaWriter) error{ Stop, OutOfMemo
var content = std.ArrayListUnmanaged(u8).empty;
defer content.deinit(self.external_allocator);
try self.root.store(content.writer(self.external_allocator), self.file_eol_mode);
const dirty = self.is_dirty();
try cbor.writeArrayHeader(writer, 8);
try cbor.writeArrayHeader(writer, 9);
try cbor.writeValue(writer, self.get_file_path());
try cbor.writeValue(writer, self.file_exists);
try cbor.writeValue(writer, self.file_eol_mode);
try cbor.writeValue(writer, self.hidden);
try cbor.writeValue(writer, self.ephemeral);
try cbor.writeValue(writer, self.meta orelse &.{});
try cbor.writeValue(writer, dirty);
try cbor.writeValue(writer, self.meta);
try cbor.writeValue(writer, self.file_type_name);
try cbor.writeValue(writer, content.items);
}
@ -1511,7 +1513,8 @@ pub const ExtractStateOperation = enum { none, open_file };
pub fn extract_state(self: *Self, iter: *[]const u8) !void {
var file_path: []const u8 = undefined;
var file_type_name: []const u8 = undefined;
var meta: []const u8 = &.{};
var dirty: bool = undefined;
var meta: ?[]const u8 = null;
var content: []const u8 = undefined;
if (!try cbor.matchValue(iter, .{
@ -1520,6 +1523,7 @@ pub fn extract_state(self: *Self, iter: *[]const u8) !void {
cbor.extract(&self.file_eol_mode),
cbor.extract(&self.hidden),
cbor.extract(&self.ephemeral),
cbor.extract(&dirty),
cbor.extract(&meta),
cbor.extract(&file_type_name),
cbor.extract(&content),
@ -1538,9 +1542,10 @@ pub fn extract_state(self: *Self, iter: *[]const u8) !void {
self.file_type_color = file_type_config.default.color;
}
if (meta.len > 0) {
if (self.meta) |buf| self.external_allocator.free(buf);
self.meta = if (self.meta) |buf| try self.external_allocator.dupe(u8, buf) else null;
if (meta) |buf| {
if (self.meta) |old_buf| self.external_allocator.free(old_buf);
self.meta = try self.external_allocator.dupe(u8, buf);
}
try self.reset_from_string_and_update(content);
if (dirty) self.mark_dirty();
}

View file

@ -5,6 +5,7 @@ const command = @import("command");
const project_manager = @import("project_manager");
const tui = @import("../../tui.zig");
const Widget = @import("../../Widget.zig");
pub const Type = @import("palette.zig").Create(@This());
const module_name = @typeName(@This());
@ -14,7 +15,7 @@ pub const description = "task";
pub const Entry = struct {
label: []const u8,
hint: []const u8,
command: ?[]const u8 = null,
};
pub fn deinit(palette: *Type) void {
@ -29,9 +30,13 @@ 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), .hint = "" };
(try palette.entries.addOne()).* = .{ .label = try palette.allocator.dupe(u8, task) };
} else return error.InvalidTaskMessageField;
}
(try palette.entries.addOne()).* = .{
.label = try palette.allocator.dupe(u8, " Add new task"),
.command = "add_task",
};
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,30 +54,73 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
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, entry);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
palette.items += 1;
}
pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.Theme, selected: bool) bool {
var entry: Entry = undefined;
var iter = button.opts.label; // label contains cbor entry object and matches
if (!(cbor.matchValue(&iter, cbor.extract(&entry)) catch false))
entry.label = "#ERROR#";
const style_base = theme.editor_widget;
const style_label =
if (button.active)
theme.editor_cursor
else if (button.hover or selected)
theme.editor_selection
else if (entry.command) |_|
theme.input_placeholder
else
theme.editor_widget;
const style_hint = if (tui.find_scope_style(theme, "entity.name")) |sty| sty.style else style_label;
button.plane.set_base_style(style_base);
button.plane.erase();
button.plane.home();
button.plane.set_style(style_label);
button.plane.fill(" ");
button.plane.home();
button.plane.set_style(style_hint);
const pointer = if (selected) "" else " ";
_ = button.plane.print("{s}", .{pointer}) catch {};
button.plane.set_style(style_label);
_ = button.plane.print("{s} ", .{entry.label}) catch {};
var index: usize = 0;
var len = cbor.decodeArrayHeader(&iter) catch return false;
while (len > 0) : (len -= 1) {
if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
tui.render_match_cell(&button.plane, 0, index + 1, theme) catch break;
} else break;
}
return false;
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
var task: []const u8 = undefined;
var entry: Entry = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &task) catch false)) return;
if (!(cbor.matchValue(&iter, cbor.extract(&entry)) catch false)) return;
var buffer_name = std.ArrayList(u8).init(menu.*.opts.ctx.allocator);
defer buffer_name.deinit();
buffer_name.writer().print("*{s}*", .{task}) catch {};
project_manager.add_task(task) catch {};
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "create_scratch_buffer", .{ buffer_name.items, "", "conf" } }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "shell_execute_stream", .{task} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
buffer_name.writer().print("*{s}*", .{entry.label}) catch {};
if (entry.command) |cmd| {
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", cmd, .{entry.label} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
} else {
project_manager.add_task(entry.label) catch {};
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "create_scratch_buffer", .{ buffer_name.items, "", "conf" } }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "shell_execute_stream", .{entry.label} }) 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 entry: Entry = 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);
if (!(cbor.matchValue(&iter, cbor.extract(&entry)) catch false)) return false;
command.executeName("delete_task", command.fmt(.{entry.label})) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
return true; //refresh list
}