diff --git a/src/git.zig b/src/git.zig index 1febeb3..b2b4626 100644 --- a/src/git.zig +++ b/src/git.zig @@ -1,16 +1,27 @@ const std = @import("std"); const tp = @import("thespian"); const shell = @import("shell"); +const bin_path = @import("bin_path"); -const git_binary = "git"; +var git_path: ?struct { + path: ?[:0]const u8 = null, +} = null; + +fn get_git() ?[]const u8 { + if (git_path) |p| return p.path; + const path = bin_path.find_binary_in_path(std.heap.c_allocator, "git") catch null; + git_path = .{ .path = path }; + return path; +} pub fn get_current_branch(allocator: std.mem.Allocator) !void { + const git_binary = get_git() orelse return error.GitBinaryNotFound; const git_current_branch_cmd = tp.message.fmt(.{ git_binary, "rev-parse", "--abbrev-ref", "HEAD" }); const handlers = struct { fn out(_: usize, parent: tp.pid_ref, _: []const u8, output: []const u8) void { var it = std.mem.splitScalar(u8, output, '\n'); - while (it.next()) |line| if (line.len > 0) - parent.send(.{ "git", "current_branch", line }) catch {}; + while (it.next()) |branch| if (branch.len > 0) + parent.send(.{ "git", "current_branch", branch }) catch {}; } }; try shell.execute(allocator, git_current_branch_cmd, .{ diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 113f1d0..7d68dcb 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -2,7 +2,6 @@ const std = @import("std"); const tp = @import("thespian"); const cbor = @import("cbor"); const tracy = @import("tracy"); -const git = @import("git"); const ripgrep = @import("ripgrep"); const root = @import("root"); const location_history = @import("location_history"); @@ -828,11 +827,6 @@ const cmds = struct { } pub const find_in_files_query_meta: Meta = .{ .arguments = &.{.string} }; - pub fn git_branch(self: *Self, _: Ctx) Result { - try git.get_current_branch(self.allocator); - } - pub const git_branch_meta: Meta = .{ .description = "Get the current git branch" }; - pub fn shell_execute_log(self: *Self, ctx: Ctx) Result { if (!try ctx.args.match(.{ tp.string, tp.more })) return error.InvalidShellArgument; diff --git a/src/tui/status/branch.zig b/src/tui/status/branch.zig new file mode 100644 index 0000000..86b7295 --- /dev/null +++ b/src/tui/status/branch.zig @@ -0,0 +1,68 @@ +const std = @import("std"); +const tp = @import("thespian"); +const cbor = @import("cbor"); + +const EventHandler = @import("EventHandler"); +const Plane = @import("renderer").Plane; +const git = @import("git"); + +const Widget = @import("../Widget.zig"); +const MessageFilter = @import("../MessageFilter.zig"); +const tui = @import("../tui.zig"); + +const branch_symbol = "󰘬"; + +allocator: std.mem.Allocator, +plane: Plane, +branch: ?[]const u8 = null, + +const Self = @This(); + +pub fn create(allocator: std.mem.Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { + const self: *Self = try allocator.create(Self); + self.* = .{ + .allocator = allocator, + .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), + }; + try tui.message_filters().add(MessageFilter.bind(self, receive_git)); + git.get_current_branch(self.allocator) catch {}; + return Widget.to(self); +} + +pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { + if (self.branch) |p| self.allocator.free(p); + self.plane.deinit(); + allocator.destroy(self); +} + +fn receive_git(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool { + var branch: []const u8 = undefined; + if (try cbor.match(m.buf, .{ "git", "current_branch", tp.extract(&branch) })) { + if (self.branch) |p| self.allocator.free(p); + self.branch = try self.allocator.dupe(u8, branch); + return true; + } + return false; +} + +pub fn layout(self: *Self) Widget.Layout { + const branch = self.branch orelse return .{ .static = 0 }; + var buf: [256]u8 = undefined; + var fbs = std.io.fixedBufferStream(&buf); + const writer = fbs.writer(); + writer.print("{s} {s}", .{ branch_symbol, branch }) catch {}; + const len = self.plane.egc_chunk_width(fbs.getWritten(), 0, 1); + return .{ .static = len }; +} + +pub fn render(self: *Self, theme: *const Widget.Theme) bool { + const branch = self.branch orelse return false; + self.plane.set_base_style(theme.editor); + self.plane.erase(); + self.plane.home(); + self.plane.set_style(theme.statusbar); + self.plane.fill(" "); + self.plane.home(); + _ = self.plane.print("{s} {s}", .{ branch_symbol, branch }) catch {}; + return false; +} diff --git a/src/tui/status/widget.zig b/src/tui/status/widget.zig index 80ebc45..5dfcf2f 100644 --- a/src/tui/status/widget.zig +++ b/src/tui/status/widget.zig @@ -19,6 +19,7 @@ const widgets = std.static_string_map.StaticStringMap(CreateFunction).initCompti .{ "clock", @import("clock.zig").create }, .{ "keybind", @import("keybindstate.zig").create }, .{ "tabs", @import("tabs.zig").create }, + .{ "branch", @import("branch.zig").create }, }); pub const CreateError = error{ OutOfMemory, WidgetInitFailed }; pub const CreateFunction = *const fn (allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, arg: ?[]const u8) CreateError!Widget;