feat: add git branch widget

This commit is contained in:
CJ van den Berg 2025-04-20 22:51:43 +02:00
parent 845403f2ae
commit 45574ff5c5
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 83 additions and 9 deletions

View file

@ -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;

68
src/tui/status/branch.zig Normal file
View file

@ -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;
}

View file

@ -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;