Compare commits
2 commits
5ad074c681
...
45574ff5c5
Author | SHA1 | Date | |
---|---|---|---|
45574ff5c5 | |||
845403f2ae |
7 changed files with 91 additions and 11 deletions
|
@ -305,6 +305,10 @@ pub fn build_exe(
|
||||||
.root_source_file = b.path("src/color.zig"),
|
.root_source_file = b.path("src/color.zig"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const bin_path_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/bin_path.zig"),
|
||||||
|
});
|
||||||
|
|
||||||
const Buffer_mod = b.createModule(.{
|
const Buffer_mod = b.createModule(.{
|
||||||
.root_source_file = b.path("src/buffer/Buffer.zig"),
|
.root_source_file = b.path("src/buffer/Buffer.zig"),
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
|
@ -426,6 +430,7 @@ pub fn build_exe(
|
||||||
.{ .name = "thespian", .module = thespian_mod },
|
.{ .name = "thespian", .module = thespian_mod },
|
||||||
.{ .name = "cbor", .module = cbor_mod },
|
.{ .name = "cbor", .module = cbor_mod },
|
||||||
.{ .name = "shell", .module = shell_mod },
|
.{ .name = "shell", .module = shell_mod },
|
||||||
|
.{ .name = "bin_path", .module = bin_path_mod },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -538,6 +543,7 @@ pub fn build_exe(
|
||||||
exe.root_module.addImport("input", input_mod);
|
exe.root_module.addImport("input", input_mod);
|
||||||
exe.root_module.addImport("syntax", syntax_mod);
|
exe.root_module.addImport("syntax", syntax_mod);
|
||||||
exe.root_module.addImport("color", color_mod);
|
exe.root_module.addImport("color", color_mod);
|
||||||
|
exe.root_module.addImport("bin_path", bin_path_mod);
|
||||||
exe.root_module.addImport("version", b.createModule(.{ .root_source_file = version_file }));
|
exe.root_module.addImport("version", b.createModule(.{ .root_source_file = version_file }));
|
||||||
exe.root_module.addImport("version_info", b.createModule(.{ .root_source_file = version_info_file }));
|
exe.root_module.addImport("version_info", b.createModule(.{ .root_source_file = version_info_file }));
|
||||||
|
|
||||||
|
|
17
src/git.zig
17
src/git.zig
|
@ -1,16 +1,27 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const tp = @import("thespian");
|
const tp = @import("thespian");
|
||||||
const shell = @import("shell");
|
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 {
|
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 git_current_branch_cmd = tp.message.fmt(.{ git_binary, "rev-parse", "--abbrev-ref", "HEAD" });
|
||||||
const handlers = struct {
|
const handlers = struct {
|
||||||
fn out(_: usize, parent: tp.pid_ref, _: []const u8, output: []const u8) void {
|
fn out(_: usize, parent: tp.pid_ref, _: []const u8, output: []const u8) void {
|
||||||
var it = std.mem.splitScalar(u8, output, '\n');
|
var it = std.mem.splitScalar(u8, output, '\n');
|
||||||
while (it.next()) |line| if (line.len > 0)
|
while (it.next()) |branch| if (branch.len > 0)
|
||||||
parent.send(.{ "git", "current_branch", line }) catch {};
|
parent.send(.{ "git", "current_branch", branch }) catch {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
try shell.execute(allocator, git_current_branch_cmd, .{
|
try shell.execute(allocator, git_current_branch_cmd, .{
|
||||||
|
|
|
@ -3,7 +3,7 @@ const syntax = @import("syntax");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const RGB = @import("color").RGB;
|
const RGB = @import("color").RGB;
|
||||||
|
|
||||||
const bin_path = @import("bin_path.zig");
|
const bin_path = @import("bin_path");
|
||||||
|
|
||||||
const checkmark_width = if (builtin.os.tag != .windows) 2 else 3;
|
const checkmark_width = if (builtin.os.tag != .windows) 2 else 3;
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ const cbor = @import("cbor");
|
||||||
const thespian = @import("thespian");
|
const thespian = @import("thespian");
|
||||||
const flags = @import("flags");
|
const flags = @import("flags");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
|
const bin_path = @import("bin_path");
|
||||||
|
|
||||||
const bin_path = @import("bin_path.zig");
|
|
||||||
const list_languages = @import("list_languages.zig");
|
const list_languages = @import("list_languages.zig");
|
||||||
pub const file_link = @import("file_link.zig");
|
pub const file_link = @import("file_link.zig");
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ const std = @import("std");
|
||||||
const tp = @import("thespian");
|
const tp = @import("thespian");
|
||||||
const cbor = @import("cbor");
|
const cbor = @import("cbor");
|
||||||
const tracy = @import("tracy");
|
const tracy = @import("tracy");
|
||||||
const git = @import("git");
|
|
||||||
const ripgrep = @import("ripgrep");
|
const ripgrep = @import("ripgrep");
|
||||||
const root = @import("root");
|
const root = @import("root");
|
||||||
const location_history = @import("location_history");
|
const location_history = @import("location_history");
|
||||||
|
@ -828,11 +827,6 @@ const cmds = struct {
|
||||||
}
|
}
|
||||||
pub const find_in_files_query_meta: Meta = .{ .arguments = &.{.string} };
|
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 {
|
pub fn shell_execute_log(self: *Self, ctx: Ctx) Result {
|
||||||
if (!try ctx.args.match(.{ tp.string, tp.more }))
|
if (!try ctx.args.match(.{ tp.string, tp.more }))
|
||||||
return error.InvalidShellArgument;
|
return error.InvalidShellArgument;
|
||||||
|
|
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 },
|
.{ "clock", @import("clock.zig").create },
|
||||||
.{ "keybind", @import("keybindstate.zig").create },
|
.{ "keybind", @import("keybindstate.zig").create },
|
||||||
.{ "tabs", @import("tabs.zig").create },
|
.{ "tabs", @import("tabs.zig").create },
|
||||||
|
.{ "branch", @import("branch.zig").create },
|
||||||
});
|
});
|
||||||
pub const CreateError = error{ OutOfMemory, WidgetInitFailed };
|
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;
|
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