From adeb8e4472b42419b24b72988c2f1d98a6ffc228 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sat, 18 Jun 2022 18:16:41 +0200 Subject: [PATCH] fancify prompt --- prompt/src/ffi.zig | 1 - prompt/src/prompt.zig | 49 ++++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/prompt/src/ffi.zig b/prompt/src/ffi.zig index 862382dc..1c76654a 100644 --- a/prompt/src/ffi.zig +++ b/prompt/src/ffi.zig @@ -7,7 +7,6 @@ pub const c = @cImport({ pub fn checkGitError(errno: c_int) !void { if (errno < 0) { const err = c.git_error_last(); - // TODO: this looks terrible. save to buf or something std.log.err( "libgit2 error: {}/{}: {s}", .{ errno, err.*.klass, err.*.message }, diff --git a/prompt/src/prompt.zig b/prompt/src/prompt.zig index 96016a36..1c8724a4 100644 --- a/prompt/src/prompt.zig +++ b/prompt/src/prompt.zig @@ -15,6 +15,8 @@ const symbols = struct { const top_end = ""; const staged = ""; const unstaged = ""; + const home = ""; + const root = ""; }; pub fn render(writer: anytype, status: i16, mode: FishMode) !void { @@ -52,7 +54,7 @@ fn Renderer(comptime Writer: type) type { try self.renderCwd(); self.renderGit() catch |err| { switch (err) { - error.GitError => {}, + error.GitError => {}, // git error will be printed else => return err, } }; @@ -86,7 +88,6 @@ fn Renderer(comptime Writer: type) type { try self.setStyle(.{}); } - // TODO: fancify (using symbols.path_separator) + some formatting fn renderCwd(self: *Self) !void { const pwd = std.fs.cwd(); const realpath = try pwd.realpathAlloc(std.heap.c_allocator, "."); @@ -102,30 +103,54 @@ fn Renderer(comptime Writer: type) type { try self.setStyle(.{ .background = .{ .Yellow = {} }, .foreground = .{ .Magenta = {} }, - .font_style = .{ .bold = true }, }); - try self.writer.writeAll(" ~/"); + try self.writer.writeAll(" " ++ symbols.home); if (home.len != realpath.len) { - try self.setStyle(.{ - .background = .{ .Yellow = {} }, - .foreground = .{ .Black = {} }, - }); - try self.writer.writeAll(realpath[(home.len + 1)..]); + try self.renderPathSep(); + try self.renderPath(realpath[(home.len + 1)..]); } written_path = true; } } + // write root-relative path if (!written_path) { try self.setStyle(.{ .background = .{ .Yellow = {} }, - .foreground = .{ .Black = {} }, + .foreground = .{ .Red = {} }, }); - try self.writer.writeAll(" "); - try self.writer.writeAll(realpath); + try self.writer.writeAll(" " ++ symbols.root); + + // don't render separators when we're in / + if (realpath.len > 1) { + try self.renderPathSep(); + try self.renderPath(realpath[1..]); + } } } + fn renderPath(self: *Self, path: []const u8) !void { + for (path) |byte| + if (byte == '/') + try self.renderPathSep() + else + try self.writer.writeByte(byte); + } + + fn renderPathSep(self: *Self) !void { + try self.setStyle(.{ + .background = self.last_style.?.background, + .foreground = .{ .Blue = {} }, + }); + + try self.writer.writeAll(" " ++ symbols.path_separator ++ " "); + + try self.setStyle(.{ + .background = self.last_style.?.background, + .foreground = .{ .Black = {} }, + }); + } + fn renderGit(self: *Self) !void { _ = self; try checkGitError(c.git_libgit2_init());