From 60650dd6af355e4513ae50be5714c1d2226332d3 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sat, 27 Apr 2024 19:35:00 +0200 Subject: [PATCH] prompt now works for bash --- .bashrc | 1 + .config/fish/conf.d/99-prompt.fish | 2 +- confgen.lua | 1 + scripts/prompt/src/FishMode.zig | 43 ---------------- scripts/prompt/src/main.zig | 40 +++++++-------- scripts/prompt/src/prompt.zig | 80 +++++++++++++++++++----------- scripts/prompt/src/shell.zig | 40 +++++++++++++++ scripts/prompt/src/vi_mode.zig | 44 ++++++++++++++++ scripts/sysupdate.rkt | 1 - 9 files changed, 159 insertions(+), 93 deletions(-) create mode 100644 .bashrc delete mode 100644 scripts/prompt/src/FishMode.zig create mode 100644 scripts/prompt/src/shell.zig create mode 100644 scripts/prompt/src/vi_mode.zig diff --git a/.bashrc b/.bashrc new file mode 100644 index 0000000..5ce76ce --- /dev/null +++ b/.bashrc @@ -0,0 +1 @@ +source <(prompt setup bash) diff --git a/.config/fish/conf.d/99-prompt.fish b/.config/fish/conf.d/99-prompt.fish index ffacdc2..36b244a 100644 --- a/.config/fish/conf.d/99-prompt.fish +++ b/.config/fish/conf.d/99-prompt.fish @@ -1,2 +1,2 @@ # see prompt/ directory for this -prompt printfish | source +prompt setup fish | source diff --git a/confgen.lua b/confgen.lua index 20e8e64..6d82634 100644 --- a/confgen.lua +++ b/confgen.lua @@ -6,6 +6,7 @@ cg.addPath ".ssh" cg.addPath ".cargo" cg.addPath "etc" +cg.addFile ".bashrc" cg.addFile ".gtkrc-2.0.cgt" cg.addFile ".Xresources.cgt" cg.addFile ".replrc" diff --git a/scripts/prompt/src/FishMode.zig b/scripts/prompt/src/FishMode.zig deleted file mode 100644 index e5f70d5..0000000 --- a/scripts/prompt/src/FishMode.zig +++ /dev/null @@ -1,43 +0,0 @@ -const std = @import("std"); -const at = @import("ansi-term"); - -pub const Mode = enum { - default, - insert, - replace_one, - replace, - visual, - unknown, -}; - -mode: Mode, - -const Self = @This(); - -pub fn parse(s: []const u8) Self { - return .{ .mode = std.meta.stringToEnum(Mode, s) orelse .unknown }; -} - -/// Gets the color for the mode -pub fn getColor(self: *Self) at.style.Color { - return switch (self.mode) { - .default => .Yellow, - .insert => .Green, - .replace_one => .Magenta, - .replace => .Blue, - .visual => .Magenta, - .unknown => .Red, - }; -} - -/// Gets a string to show for the mode. -/// Returned string is static -pub fn getText(self: *Self) []const u8 { - return switch (self.mode) { - .default => "N", - .insert => "I", - .replace_one, .replace => "R", - .visual => "V", - .unknown => "?", - }; -} diff --git a/scripts/prompt/src/main.zig b/scripts/prompt/src/main.zig index 094e717..6a5db6f 100644 --- a/scripts/prompt/src/main.zig +++ b/scripts/prompt/src/main.zig @@ -1,18 +1,8 @@ const std = @import("std"); -const FishMode = @import("FishMode.zig"); +const ViMode = @import("vi_mode.zig").ViMode; +const Shell = @import("shell.zig").Shell; const prompt = @import("prompt.zig"); -const fish_code = - \\functions -e fish_mode_prompt - \\function fish_prompt - \\ set -x MZPROMPT_STATUS $status - \\ set -x MZPROMPT_FISH_MODE $fish_bind_mode - \\ set -x MZPROMPT_DURATION $CMD_DURATION - \\ set -x MZPROMPT_JOBS (count (jobs)) - \\ {s} show - \\end -; - pub const std_options = std.Options{ .log_level = .debug, .logFn = @import("common").logFn, @@ -24,19 +14,25 @@ pub fn main() !void { const verb = std.mem.span(std.os.argv[1]); - if (std.mem.eql(u8, verb, "printfish")) { - const stdout = std.io.getStdOut(); - try stdout.writer().print(fish_code ++ "\n", .{std.os.argv[0]}); + const stdout = std.io.getStdOut(); + if (std.mem.eql(u8, verb, "setup")) { + if (std.os.argv.len < 3) + return error.NotEnoughArguments; + + const shell = std.meta.stringToEnum(Shell, std.mem.span(std.os.argv[2])) orelse + return error.InvalidShell; + + try shell.writeInitCode(std.mem.span(std.os.argv[0]), stdout.writer()); } else if (std.mem.eql(u8, verb, "show")) { const options = prompt.Options{ .status = try std.fmt.parseInt( - i16, + i32, std.posix.getenv("MZPROMPT_STATUS") orelse return error.MissingEnv, 10, ), - .mode = FishMode.parse( - std.posix.getenv("MZPROMPT_FISH_MODE") orelse + .mode = ViMode.parse( + std.posix.getenv("MZPROMPT_VI_MODE") orelse return error.MissingEnv, ), .duration = try std.fmt.parseInt( @@ -52,15 +48,19 @@ pub fn main() !void { 10, ), .nix_name = @import("nix.zig").findNixShellName(), + .shell = std.meta.stringToEnum(Shell, std.posix.getenv( + "MZPROMPT_SHELL", + ) orelse return error.MissingEnv) orelse return error.InvalidShell, }; var buf: [1024 * 8]u8 = undefined; var fbs = std.io.fixedBufferStream(&buf); prompt.render(fbs.writer(), options) catch |e| { fbs.reset(); - fbs.writer().print("Render Error: {s}\n|> ", .{@errorName(e)}) catch unreachable; + fbs.writer().print("Render Error: {s}\n|> ", .{@errorName(e)}) catch + @panic("emergency prompt failed to print"); }; - try std.io.getStdOut().writeAll(fbs.getWritten()); + try stdout.writeAll(fbs.getWritten()); } else { return error.UnknownCommand; } diff --git a/scripts/prompt/src/prompt.zig b/scripts/prompt/src/prompt.zig index 679f5cf..9e19e37 100644 --- a/scripts/prompt/src/prompt.zig +++ b/scripts/prompt/src/prompt.zig @@ -4,7 +4,8 @@ const known_folders = @import("known-folders"); const ffi = @import("ffi.zig"); const checkGitError = ffi.checkGitError; const c = ffi.c; -const FishMode = @import("FishMode.zig"); +const ViMode = @import("vi_mode.zig").ViMode; +const Shell = @import("shell.zig").Shell; const Style = at.style.Style; const Color = at.style.Color; @@ -23,14 +24,16 @@ const symbols = struct { const watch = ""; const jobs = ""; const nix = "󱄅"; + const bash = ""; }; pub const Options = struct { - status: i16, - mode: FishMode, + status: i32, + mode: ViMode, duration: u32, jobs: u32, nix_name: ?[]const u8, + shell: Shell, }; pub fn render(writer: anytype, options: Options) !void { @@ -56,10 +59,11 @@ fn Renderer(comptime Writer: type) type { try self.setStyle(.{ .foreground = left_color }); try self.writer.writeAll(symbols.top_left); try self.setStyle(.{ .background = left_color }); + try self.renderShell(); try self.renderDuration(); try self.renderJobs(); - try self.renderNix(); try self.renderCwd(); + try self.renderNix(); self.renderGit() catch |err| { switch (err) { error.GitError => {}, // git error will be printed @@ -76,26 +80,46 @@ fn Renderer(comptime Writer: type) type { try self.setStyle(.{ .foreground = left_color, .background = left_color }); try self.writer.writeAll(" "); - const mode_color = self.options.mode.getColor(); + if (self.options.mode != ._none) { + const mode_color = self.options.mode.getColor(); + + try self.setStyle(.{ + .foreground = left_color, + .background = mode_color, + .font_style = .{ .bold = true }, + }); + try self.writer.writeAll(symbols.right_separator ++ " "); + try self.setStyle(.{ + .foreground = .Black, + .background = mode_color, + .font_style = .{ .bold = true }, + }); + try self.writer.writeByte(self.options.mode.getChar()); + try self.writer.writeByte(' '); + try self.setStyle(.{ .foreground = mode_color }); + } else { + try self.writer.writeByte(' '); + try self.setStyle(.{ .foreground = left_color }); + } - try self.setStyle(.{ - .foreground = left_color, - .background = mode_color, - .font_style = .{ .bold = true }, - }); - try self.writer.writeAll(symbols.right_separator ++ " "); - try self.setStyle(.{ - .foreground = .Black, - .background = mode_color, - .font_style = .{ .bold = true }, - }); - try self.writer.writeAll(self.options.mode.getText()); - try self.writer.writeAll(" "); - try self.setStyle(.{ .foreground = mode_color }); try self.writer.writeAll(symbols.right_separator ++ " "); try self.setStyle(.{}); } + fn renderShell(self: *Self) !void { + switch (self.options.shell) { + .fish => {}, + .bash => { + const bgcol = Color{ .Grey = 150 }; + const fgcol = Color.Black; + try self.drawLeftSep(bgcol); + try self.setStyle(.{ .background = bgcol, .foreground = fgcol }); + + try self.writer.writeAll(" " ++ symbols.bash); + }, + } + } + fn renderDuration(self: *Self) !void { if (self.options.duration < 2 * std.time.ms_per_s) return; @@ -150,15 +174,6 @@ fn Renderer(comptime Writer: type) type { try self.writer.print(" {s} {}", .{ symbols.jobs, self.options.jobs }); } - fn renderNix(self: *Self) !void { - if (self.options.nix_name) |name| { - try self.drawLeftSep(.Blue); - try self.setStyle(.{ .background = .Blue, .foreground = .Black}); - - try self.writer.print(" {s} {s}", .{symbols.nix, name }); - } - } - fn renderCwd(self: *Self) !void { var cwd_buf: [512]u8 = undefined; const cwd = try std.posix.getcwd(&cwd_buf); @@ -199,6 +214,15 @@ fn Renderer(comptime Writer: type) type { } } + fn renderNix(self: *Self) !void { + if (self.options.nix_name) |name| { + try self.drawLeftSep(.Blue); + try self.setStyle(.{ .background = .Blue, .foreground = .Black }); + + try self.writer.print(" {s} {s}", .{ symbols.nix, name }); + } + } + fn renderPath(self: *Self, path: []const u8) !void { for (path) |byte| if (byte == '/') diff --git a/scripts/prompt/src/shell.zig b/scripts/prompt/src/shell.zig new file mode 100644 index 0000000..de8ea4b --- /dev/null +++ b/scripts/prompt/src/shell.zig @@ -0,0 +1,40 @@ +const setup_fmtstrs = struct { + const fish = + \\functions -e fish_mode_prompt + \\function fish_prompt + \\ set -x MZPROMPT_SHELL fish + \\ set -x MZPROMPT_STATUS $status + \\ set -x MZPROMPT_VI_MODE $fish_bind_mode + \\ set -x MZPROMPT_DURATION $CMD_DURATION + \\ set -x MZPROMPT_JOBS (count (jobs)) + \\ {[argv0]s} show + \\end + \\ + ; + const bash = + \\__mzprompt_show() {{ + \\ export MZPROMPT_STATUS="$?" + \\ export MZPROMPT_SHELL="bash" + \\ export MZPROMPT_VI_MODE="_none" + \\ export MZPROMPT_DURATION=0 + \\ export MZPROMPT_JOBS=$1 + \\ {[argv0]s} show + \\}} + \\PS1='$(__mzprompt_show \j)' + \\ + ; +}; + +pub const Shell = enum { + fish, + bash, + + pub fn writeInitCode(self: Shell, argv0: []const u8, writer: anytype) !void { + switch (self) { + inline else => |s| { + const fmt = @field(setup_fmtstrs, @tagName(s)); + try writer.print(fmt, .{ .argv0 = argv0 }); + }, + } + } +}; diff --git a/scripts/prompt/src/vi_mode.zig b/scripts/prompt/src/vi_mode.zig new file mode 100644 index 0000000..8a7041d --- /dev/null +++ b/scripts/prompt/src/vi_mode.zig @@ -0,0 +1,44 @@ +const std = @import("std"); +const at = @import("ansi-term"); + +pub const ViMode = enum { + default, + insert, + replace_one, + replace, + visual, + + unknown, + _none, + + pub fn parse(s: []const u8) ViMode { + return std.meta.stringToEnum(ViMode, s) orelse .unknown; + } + + /// Gets the color for the mode + /// Caller asserts that self != .none + pub fn getColor(self: ViMode) at.style.Color { + return switch (self) { + .default => .Yellow, + .insert => .Green, + .replace_one => .Magenta, + .replace => .Blue, + .visual => .Magenta, + .unknown => .Red, + ._none => unreachable, + }; + } + + /// Gets a char to show for the mode. + /// Caller asserts that self != .none + pub fn getChar(self: ViMode) u8 { + return switch (self) { + .default => 'N', + .insert => 'I', + .replace_one, .replace => 'R', + .visual => 'V', + .unknown => '?', + ._none => unreachable, + }; + } +}; diff --git a/scripts/sysupdate.rkt b/scripts/sysupdate.rkt index 0178a4d..88fc7bc 100755 --- a/scripts/sysupdate.rkt +++ b/scripts/sysupdate.rkt @@ -24,7 +24,6 @@ '("paru" "-Syu" "--noconfirm") '("paru" "-Syu"))) (cmd "alecor" "mkcache") -(cmd "update-nvim-plugins") (cmd "tldr" "--update") (unless (empty? failures)