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

@ -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, .{