feat: add git branch widget
This commit is contained in:
		
							parent
							
								
									845403f2ae
								
							
						
					
					
						commit
						45574ff5c5
					
				
					 4 changed files with 83 additions and 9 deletions
				
			
		
							
								
								
									
										68
									
								
								src/tui/status/branch.zig
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/tui/status/branch.zig
									
										
									
									
									
										Normal 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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue