diff --git a/build.zig b/build.zig index 9265dc8..cdc1868 100644 --- a/build.zig +++ b/build.zig @@ -406,11 +406,6 @@ pub fn build_exe( }, }); - const VcsStatus_mod = b.createModule(.{ - .root_source_file = b.path("src/VcsStatus.zig"), - .imports = &.{}, - }); - const color_mod = b.createModule(.{ .root_source_file = b.path("src/color.zig"), }); @@ -580,7 +575,6 @@ pub fn build_exe( .{ .name = "dizzy", .module = dizzy_dep.module("dizzy") }, .{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") }, .{ .name = "git", .module = git_mod }, - .{ .name = "VcsStatus", .module = VcsStatus_mod }, }, }); @@ -634,7 +628,6 @@ pub fn build_exe( .{ .name = "help.md", .module = help_mod }, .{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") }, .{ .name = "zeit", .module = zeit_mod }, - .{ .name = "VcsStatus", .module = VcsStatus_mod }, }, }); diff --git a/src/EventHandler.zig b/src/EventHandler.zig index c7fac8c..3f54463 100644 --- a/src/EventHandler.zig +++ b/src/EventHandler.zig @@ -108,8 +108,7 @@ pub fn dynamic_cast(self: Self, comptime T: type) ?*T { } pub fn msg(self: Self, m: anytype) tp.result { - var buf: [tp.max_message_size]u8 = undefined; - return self.vtable.send(self.ptr, tp.self_pid(), tp.message.fmtbuf(&buf, m) catch |e| std.debug.panic("EventHandler.msg: {any}", .{e})); + return self.vtable.send(self.ptr, tp.self_pid(), tp.message.fmt(m)); } pub fn send(self: Self, from_: tp.pid_ref, m: tp.message) tp.result { @@ -173,8 +172,7 @@ pub const List = struct { } pub fn msg(self: *const List, m: anytype) tp.result { - var buf: [tp.max_message_size]u8 = undefined; - return self.send(tp.self_pid(), tp.message.fmtbuf(&buf, m) catch |e| std.debug.panic("EventHandler.List.msg: {any}", .{e})); + return self.send(tp.self_pid(), tp.message.fmt(m)); } pub fn send(self: *const List, from: tp.pid_ref, m: tp.message) tp.result { diff --git a/src/Project.zig b/src/Project.zig index e71c915..5dbf46c 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -8,7 +8,6 @@ const Buffer = @import("Buffer"); const fuzzig = @import("fuzzig"); const tracy = @import("tracy"); const git = @import("git"); -const VcsStatus = @import("VcsStatus"); const file_type_config = @import("file_type_config"); const builtin = @import("builtin"); @@ -31,6 +30,7 @@ logger_lsp: log.Logger, logger_git: log.Logger, workspace: ?[]const u8 = null, +branch: ?[]const u8 = null, walker: ?tp.pid = null, @@ -40,12 +40,8 @@ state: struct { workspace_path: State = .none, current_branch: State = .none, workspace_files: State = .none, - status: State = .none, } = .{}, -status: VcsStatus = .{}, -status_request: ?tp.pid = null, - const Self = @This(); const OutOfMemoryError = error{OutOfMemory}; @@ -95,6 +91,7 @@ pub fn init(allocator: std.mem.Allocator, name: []const u8) OutOfMemoryError!Sel pub fn deinit(self: *Self) void { if (self.walker) |pid| pid.send(.{"stop"}) catch {}; if (self.workspace) |p| self.allocator.free(p); + if (self.branch) |p| self.allocator.free(p); var i_ = self.file_language_server_name.iterator(); while (i_.next()) |p| { self.allocator.free(p.key_ptr.*); @@ -594,25 +591,6 @@ pub fn get_mru_position(self: *Self, from: tp.pid_ref, file_path: []const u8) Cl from.send(.{"none"}) catch return error.ClientFailed; } -pub fn request_vcs_status(self: *Self, from: tp.pid_ref) ClientError!void { - switch (self.state.status) { - .none => return error.ClientFailed, - .failed => return, - .running => { - if (self.status_request) |_| return; - self.status_request = from.clone(); - }, - .done => { - if (self.status_request) |_| return; - self.status_request = from.clone(); - self.state.status = .running; - git.status(@intFromPtr(self)) catch { - self.state.status = .failed; - }; - }, - } -} - pub fn request_tasks(self: *Self, from: tp.pid_ref) ClientError!void { var message: std.Io.Writer.Allocating = .init(self.allocator); defer message.deinit(); @@ -2076,10 +2054,6 @@ pub fn query_git(self: *Self) void { git.current_branch(@intFromPtr(self)) catch { self.state.current_branch = .failed; }; - self.state.status = .running; - git.status(@intFromPtr(self)) catch { - self.state.status = .failed; - }; } fn start_walker(self: *Self) void { @@ -2093,9 +2067,7 @@ fn start_walker(self: *Self) void { pub fn process_git(self: *Self, parent: tp.pid_ref, m: tp.message) (OutOfMemoryError || error{Exit})!void { var value: []const u8 = undefined; var path: []const u8 = undefined; - if (try m.match(.{ tp.any, tp.any, "status", tp.more })) { - return self.process_status(m); - } else if (try m.match(.{ tp.any, tp.any, "workspace_path", tp.null_ })) { + if (try m.match(.{ tp.any, tp.any, "workspace_path", tp.null_ })) { self.state.workspace_path = .done; self.start_walker(); try self.loaded(parent); @@ -2111,8 +2083,8 @@ pub fn process_git(self: *Self, parent: tp.pid_ref, m: tp.message) (OutOfMemoryE self.state.current_branch = .done; try self.loaded(parent); } else if (try m.match(.{ tp.any, tp.any, "current_branch", tp.extract(&value) })) { - if (self.status.branch) |p| self.allocator.free(p); - self.status.branch = try self.allocator.dupe(u8, value); + if (self.branch) |p| self.allocator.free(p); + self.branch = try self.allocator.dupe(u8, value); self.state.current_branch = .done; try self.loaded(parent); } else if (try m.match(.{ tp.any, tp.any, "workspace_files", tp.extract(&path) })) { @@ -2135,54 +2107,3 @@ pub fn process_git(self: *Self, parent: tp.pid_ref, m: tp.message) (OutOfMemoryE self.logger_git.err("git", tp.unexpected(m)); } } - -fn process_status(self: *Self, m: tp.message) (OutOfMemoryError || error{Exit})!void { - const any = cbor.any; - const extract = cbor.extract; - const null_ = cbor.null_; - - var value: []const u8 = undefined; - var ahead: []const u8 = undefined; - var behind: []const u8 = undefined; - - if (self.state.status == .done) - self.status.reset(self.allocator); - - if (try m.match(.{ any, any, "status", "#", "branch.oid", extract(&value) })) { - // commit | (initial) - } else if (try m.match(.{ any, any, "status", "#", "branch.head", extract(&value) })) { - if (self.status.branch) |p| self.allocator.free(p); - self.status.branch = try self.allocator.dupe(u8, value); - } else if (try m.match(.{ any, any, "status", "#", "branch.upstream", extract(&value) })) { - // upstream-branch - } else if (try m.match(.{ any, any, "status", "#", "branch.ab", extract(&ahead), extract(&behind) })) { - if (self.status.ahead) |p| self.allocator.free(p); - self.status.ahead = try self.allocator.dupe(u8, ahead); - if (self.status.behind) |p| self.allocator.free(p); - self.status.behind = try self.allocator.dupe(u8, behind); - } else if (try m.match(.{ any, any, "status", "#", "stash", extract(&value) })) { - if (self.status.stash) |p| self.allocator.free(p); - self.status.stash = try self.allocator.dupe(u8, value); - } else if (try m.match(.{ any, any, "status", "1", tp.more })) { - // ordinary file: - self.status.changed += 1; - } else if (try m.match(.{ any, any, "status", "2", tp.more })) { - // rename or copy: - self.status.changed += 1; - } else if (try m.match(.{ any, any, "status", "u", tp.more })) { - // unmerged file:

- self.status.changed += 1; - } else if (try m.match(.{ any, any, "status", "?", tp.more })) { - // untracked file: - self.status.untracked += 1; - } else if (try m.match(.{ any, any, "status", "!", tp.more })) { - // ignored file: - } else if (try m.match(.{ any, any, "status", null_ })) { - self.state.status = .done; - if (self.status_request) |from| { - from.send(.{ "vcs_status", self.status }) catch {}; - from.deinit(); - self.status_request = null; - } - } -} diff --git a/src/VcsStatus.zig b/src/VcsStatus.zig deleted file mode 100644 index 0e5b35e..0000000 --- a/src/VcsStatus.zig +++ /dev/null @@ -1,21 +0,0 @@ -branch: ?[]const u8 = null, -ahead: ?[]const u8 = null, -behind: ?[]const u8 = null, -stash: ?[]const u8 = null, -changed: usize = 0, -untracked: usize = 0, - -pub fn reset(self: *@This(), allocator: std.mem.Allocator) void { - if (self.branch) |p| allocator.free(p); - if (self.ahead) |p| allocator.free(p); - if (self.behind) |p| allocator.free(p); - if (self.stash) |p| allocator.free(p); - self.branch = null; - self.ahead = null; - self.behind = null; - self.stash = null; - self.changed = 0; - self.untracked = 0; -} - -const std = @import("std"); diff --git a/src/project_manager.zig b/src/project_manager.zig index 46cd226..f11cb36 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -124,13 +124,6 @@ pub fn request_tasks(allocator: std.mem.Allocator) (ProjectError || CallError)!t return (try get()).pid.call(allocator, request_timeout, .{ "request_tasks", project }); } -pub fn request_vcs_status() (ProjectManagerError || ProjectError)!void { - const project = tp.env.get().str("project"); - if (project.len == 0) - return error.NoProject; - return send(.{ "request_vcs_status", project }); -} - pub fn add_task(task: []const u8) (ProjectManagerError || ProjectError)!void { const project = tp.env.get().str("project"); if (project.len == 0) @@ -378,8 +371,6 @@ const Process = struct { self.request_path_files(from, project_directory, max, path) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; } else if (try cbor.match(m.buf, .{ "request_tasks", tp.extract(&project_directory) })) { self.request_tasks(from, project_directory) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; - } else if (try cbor.match(m.buf, .{ "request_vcs_status", tp.extract(&project_directory) })) { - self.request_vcs_status(from, project_directory) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; } else if (try cbor.match(m.buf, .{ "add_task", tp.extract(&project_directory), tp.extract(&task) })) { self.add_task(project_directory, task) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; } else if (try cbor.match(m.buf, .{ "delete_task", tp.extract(&project_directory), tp.extract(&task) })) { @@ -520,11 +511,6 @@ const Process = struct { try project.delete_task(task); } - fn request_vcs_status(self: *Process, from: tp.pid_ref, project_directory: []const u8) (ProjectError || Project.ClientError)!void { - const project = self.projects.get(project_directory) orelse return error.NoProject; - try project.request_vcs_status(from); - } - fn did_open(self: *Process, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) (ProjectError || Project.StartLspError || CallError || cbor.Error)!void { const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".did_open" }); defer frame.deinit(); diff --git a/src/tui/Widget.zig b/src/tui/Widget.zig index a9035ca..d6a41ce 100644 --- a/src/tui/Widget.zig +++ b/src/tui/Widget.zig @@ -178,8 +178,7 @@ pub fn deinit(self: Self, allocator: Allocator) void { } pub fn msg(self: *const Self, m: anytype) error{Exit}!bool { - var buf: [tp.max_message_size]u8 = undefined; - return self.vtable.send(self.ptr, tp.self_pid(), tp.message.fmtbuf(&buf, m) catch |e| std.debug.panic("Widget.msg: {any}", .{e})); + return self.vtable.send(self.ptr, tp.self_pid(), tp.message.fmt(m)); } pub fn send(self: *const Self, from_: tp.pid_ref, m: tp.message) error{Exit}!bool { diff --git a/src/tui/status/branch.zig b/src/tui/status/branch.zig index 140e090..b723997 100644 --- a/src/tui/status/branch.zig +++ b/src/tui/status/branch.zig @@ -4,8 +4,7 @@ const tp = @import("thespian"); const EventHandler = @import("EventHandler"); const Plane = @import("renderer").Plane; const command = @import("command"); -const project_manager = @import("project_manager"); -const VcsStatus = @import("VcsStatus"); +const git = @import("git"); const Widget = @import("../Widget.zig"); const Button = @import("../Button.zig"); @@ -20,7 +19,14 @@ const changed_symbol = "+"; const untracked_symbol = "?"; allocator: std.mem.Allocator, -status: VcsStatus = .{}, +workspace_path: ?[]const u8 = null, +branch: ?[]const u8 = null, +ahead: ?[]const u8 = null, +behind: ?[]const u8 = null, +stash: ?[]const u8 = null, +changed: usize = 0, +untracked: usize = 0, +done: bool = true, const Self = @This(); const ButtonType = Button.Options(Self).ButtonType; @@ -45,29 +51,31 @@ pub fn create( } pub fn ctx_init(self: *Self) error{OutOfMemory}!void { - try tui.message_filters().add(MessageFilter.bind(self, receive_filter)); - project_manager.request_vcs_status() catch {}; + try tui.message_filters().add(MessageFilter.bind(self, receive_git)); + git.workspace_path(0) catch {}; } pub fn ctx_deinit(self: *Self) void { tui.message_filters().remove_ptr(self); - self.status.reset(self.allocator); + if (self.branch) |p| self.allocator.free(p); + if (self.ahead) |p| self.allocator.free(p); + if (self.behind) |p| self.allocator.free(p); } fn on_click(self: *Self, _: *ButtonType, _: Widget.Pos) void { - self.refresh_vcs_status(); - command.executeName("show_vcs_status", .{}) catch {}; + self.refresh_git_status(); + command.executeName("show_git_status", .{}) catch {}; } -fn refresh_vcs_status(self: *Self) void { - if (self.status.branch) |_| project_manager.request_vcs_status() catch {}; +fn refresh_git_status(self: *Self) void { + if (self.workspace_path) |_| git.status(0) catch {}; } pub fn receive(self: *Self, _: *ButtonType, _: tp.pid_ref, m: tp.message) error{Exit}!bool { if (try m.match(.{ "E", tp.more })) return self.process_event(m); if (try m.match(.{ "PRJ", "open" })) - project_manager.request_vcs_status() catch {}; + self.refresh_git_status(); return false; } @@ -76,13 +84,13 @@ fn process_event(self: *Self, m: tp.message) error{Exit}!bool { try m.match(.{ tp.any, "save", tp.more }) or try m.match(.{ tp.any, "open", tp.more }) or try m.match(.{ tp.any, "close" })) - self.refresh_vcs_status(); + self.refresh_git_status(); return false; } -fn receive_filter(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool { - return if (try match(m.buf, .{ "vcs_status", tp.more })) - self.process_vcs_status(m) +fn receive_git(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool { + return if (try match(m.buf, .{ "git", more })) + self.process_git(m) else if (try match(m.buf, .{"focus_in"})) self.process_focus_in() else @@ -90,41 +98,100 @@ fn receive_filter(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error } fn process_focus_in(self: *Self) MessageFilter.Error!bool { - self.refresh_vcs_status(); + self.refresh_git_status(); return false; } -fn process_vcs_status(self: *Self, m: tp.message) MessageFilter.Error!bool { +fn process_git(self: *Self, m: tp.message) MessageFilter.Error!bool { + var value: []const u8 = undefined; + if (try match(m.buf, .{ any, any, "workspace_path", null_ })) { + // do nothing, we do not have a git workspace + } else if (try match(m.buf, .{ any, any, "workspace_path", extract(&value) })) { + if (self.workspace_path) |p| self.allocator.free(p); + self.workspace_path = try self.allocator.dupe(u8, value); + // git.current_branch(0) catch {}; + git.status(0) catch {}; + } else if (try match(m.buf, .{ any, any, "current_branch", extract(&value) })) { + if (self.branch) |p| self.allocator.free(p); + self.branch = try self.allocator.dupe(u8, value); + } else if (try match(m.buf, .{ any, any, "status", tp.more })) { + return self.process_status(m); + } else { + return false; + } + return true; +} + +fn process_status(self: *Self, m: tp.message) MessageFilter.Error!bool { defer if (tui.frames_rendered() > 0) Widget.need_render(); - var status: VcsStatus = .{}; - self.status.reset(self.allocator); - if (!try match(m.buf, .{ any, extract(&status) })) return true; + var value: []const u8 = undefined; + var ahead: []const u8 = undefined; + var behind: []const u8 = undefined; - if (status.branch) |branch| self.status.branch = try self.allocator.dupe(u8, branch); - if (status.ahead) |ahead| self.status.ahead = try self.allocator.dupe(u8, ahead); - if (status.behind) |behind| self.status.behind = try self.allocator.dupe(u8, behind); - if (status.stash) |stash| self.status.stash = try self.allocator.dupe(u8, stash); + if (self.done) { + self.done = false; + self.changed = 0; + self.untracked = 0; + if (self.ahead) |p| self.allocator.free(p); + self.ahead = null; + if (self.behind) |p| self.allocator.free(p); + self.behind = null; + if (self.stash) |p| self.allocator.free(p); + self.stash = null; + } + if (try match(m.buf, .{ any, any, "status", "#", "branch.oid", extract(&value) })) { + // commit | (initial) + } else if (try match(m.buf, .{ any, any, "status", "#", "branch.head", extract(&value) })) { + if (self.branch) |p| self.allocator.free(p); + self.branch = try self.allocator.dupe(u8, value); + } else if (try match(m.buf, .{ any, any, "status", "#", "branch.upstream", extract(&value) })) { + // upstream-branch + } else if (try match(m.buf, .{ any, any, "status", "#", "branch.ab", extract(&ahead), extract(&behind) })) { + if (self.ahead) |p| self.allocator.free(p); + self.ahead = try self.allocator.dupe(u8, ahead); + if (self.behind) |p| self.allocator.free(p); + self.behind = try self.allocator.dupe(u8, behind); + } else if (try match(m.buf, .{ any, any, "status", "#", "stash", extract(&value) })) { + if (self.stash) |p| self.allocator.free(p); + self.stash = try self.allocator.dupe(u8, value); + } else if (try match(m.buf, .{ any, any, "status", "1", tp.more })) { + // ordinary file: + self.changed += 1; + } else if (try match(m.buf, .{ any, any, "status", "2", tp.more })) { + // rename or copy: + self.changed += 1; + } else if (try match(m.buf, .{ any, any, "status", "u", tp.more })) { + // unmerged file:

+ self.changed += 1; + } else if (try match(m.buf, .{ any, any, "status", "?", tp.more })) { + // untracked file: + self.untracked += 1; + } else if (try match(m.buf, .{ any, any, "status", "!", tp.more })) { + // ignored file: + } else if (try match(m.buf, .{ any, any, "status", null_ })) { + self.done = true; + } else return false; return true; } fn format(self: *Self, buf: []u8) []const u8 { - const branch = self.status.branch orelse return ""; + const branch = self.branch orelse return ""; var fbs = std.io.fixedBufferStream(buf); const writer = fbs.writer(); writer.print(" {s}{s}", .{ branch_symbol, branch }) catch {}; - if (self.status.ahead) |ahead| if (ahead.len > 1 and ahead[1] != '0') + if (self.ahead) |ahead| if (ahead.len > 1 and ahead[1] != '0') writer.print(" {s}{s}", .{ ahead_symbol, ahead[1..] }) catch {}; - if (self.status.behind) |behind| if (behind.len > 1 and behind[1] != '0') + if (self.behind) |behind| if (behind.len > 1 and behind[1] != '0') writer.print(" {s}{s}", .{ behind_symbol, behind[1..] }) catch {}; - if (self.status.stash) |stash| if (stash.len > 0 and stash[0] != '0') + if (self.stash) |stash| if (stash.len > 0 and stash[0] != '0') writer.print(" {s}{s}", .{ stash_symbol, stash }) catch {}; - if (self.status.changed > 0) - writer.print(" {s}{d}", .{ changed_symbol, self.status.changed }) catch {}; - if (self.status.untracked > 0) - writer.print(" {s}{d}", .{ untracked_symbol, self.status.untracked }) catch {}; + if (self.changed > 0) + writer.print(" {s}{d}", .{ changed_symbol, self.changed }) catch {}; + if (self.untracked > 0) + writer.print(" {s}{d}", .{ untracked_symbol, self.untracked }) catch {}; writer.print(" ", .{}) catch {}; return fbs.getWritten(); }