prompt now works for bash

This commit is contained in:
LordMZTE 2024-04-27 19:35:00 +02:00
parent 3406a93f50
commit 60650dd6af
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
9 changed files with 159 additions and 93 deletions

1
.bashrc Normal file
View File

@ -0,0 +1 @@
source <(prompt setup bash)

View File

@ -1,2 +1,2 @@
# see prompt/ directory for this
prompt printfish | source
prompt setup fish | source

View File

@ -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"

View File

@ -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 => "?",
};
}

View File

@ -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;
}

View File

@ -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 == '/')

View File

@ -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 });
},
}
}
};

View File

@ -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,
};
}
};

View File

@ -24,7 +24,6 @@
'("paru" "-Syu" "--noconfirm")
'("paru" "-Syu")))
(cmd "alecor" "mkcache")
(cmd "update-nvim-plugins")
(cmd "tldr" "--update")
(unless (empty? failures)