prompt displays duration

This commit is contained in:
LordMZTE 2023-04-20 00:05:06 +02:00
parent 2811920e9c
commit 91a4d34811
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 68 additions and 17 deletions

View file

@ -5,7 +5,10 @@ const prompt = @import("prompt.zig");
const fish_code =
\\functions -e fish_mode_prompt
\\function fish_prompt
\\ {s} show $status $fish_bind_mode
\\ set -x MZPROMPT_STATUS $status
\\ set -x MZPROMPT_FISH_MODE $fish_bind_mode
\\ set -x MZPROMPT_DURATION $CMD_DURATION
\\ {s} show
\\end
;
@ -17,14 +20,27 @@ pub fn main() !void {
const stdout = std.io.getStdOut();
try stdout.writer().print(fish_code ++ "\n", .{std.os.argv[0]});
} else if (std.cstr.cmp(std.os.argv[1], "show") == 0) {
if (std.os.argv.len < 4)
return error.NotEnoughArguments;
const status = try std.fmt.parseInt(i16, std.mem.sliceTo(std.os.argv[2], 0), 10);
const mode = FishMode.parse(std.mem.sliceTo(std.os.argv[3], 0));
const options = prompt.Options{
.status = try std.fmt.parseInt(
i16,
std.os.getenv("MZPROMPT_STATUS") orelse
return error.MissingEnv,
10,
),
.mode = FishMode.parse(
std.os.getenv("MZPROMPT_FISH_MODE") orelse
return error.MissingEnv,
),
.duration = try std.fmt.parseInt(
u32,
std.os.getenv("MZPROMPT_DURATION") orelse
return error.MissingEnv,
10,
),
};
var buf = std.BoundedArray(u8, 1024 * 8).init(0) catch unreachable;
prompt.render(buf.writer(), status, mode) catch |e| {
prompt.render(buf.writer(), options) catch |e| {
buf.resize(0) catch unreachable;
buf.writer().print("Render Error: {s}\n|> ", .{@errorName(e)}) catch unreachable;
};

View file

@ -20,14 +20,20 @@ const symbols = struct {
const unstaged = "";
const home = "";
const root = "";
const watch = "";
};
pub fn render(writer: anytype, status: i16, mode: FishMode) !void {
pub const Options = struct {
status: i16,
mode: FishMode,
duration: u32,
};
pub fn render(writer: anytype, options: Options) !void {
try (Renderer(@TypeOf(writer)){
.last_style = null,
.writer = writer,
.status = status,
.mode = mode,
.options = options,
}).render();
}
@ -35,15 +41,12 @@ fn Renderer(comptime Writer: type) type {
return struct {
last_style: ?Style,
writer: Writer,
status: i16,
mode: FishMode,
options: Options,
const Self = @This();
pub fn render(self: *Self) !void {
//const alloc = std.heap.c_allocator;
const left_color = if (self.status == 0)
const left_color = if (self.options.status == 0)
Color{ .Green = {} }
else
Color{ .Red = {} };
@ -51,6 +54,7 @@ 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.renderDuration();
try self.renderCwd();
self.renderGit() catch |err| {
switch (err) {
@ -68,7 +72,7 @@ fn Renderer(comptime Writer: type) type {
try self.setStyle(.{ .foreground = left_color, .background = left_color });
try self.writer.writeAll(" ");
const mode_color = self.mode.getColor();
const mode_color = self.options.mode.getColor();
try self.setStyle(.{
.foreground = left_color,
@ -81,13 +85,44 @@ fn Renderer(comptime Writer: type) type {
.background = mode_color,
.font_style = .{ .bold = true },
});
try self.writer.writeAll(self.mode.getText());
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 renderDuration(self: *Self) !void {
if (self.options.duration < 2 * std.time.ms_per_s)
return;
try self.drawLeftSep(.Blue);
try self.setStyle(.{
.background = .Blue,
.foreground = .White,
.font_style = .{ .bold = true },
});
try self.writer.writeAll(" ");
try self.writer.writeAll(symbols.watch);
try self.writer.writeAll(" ");
const hours = self.options.duration / std.time.ms_per_hour;
const minutes = (self.options.duration / std.time.ms_per_min) - hours * 60;
const seconds = ((self.options.duration / std.time.ms_per_s) - minutes * 60) - hours * std.time.s_per_hour;
if (hours > 0) {
try self.writer.print("{}h ", .{hours});
}
if (minutes > 0 or hours > 0) {
try self.writer.print("{}min ", .{minutes});
}
if (seconds > 0 or minutes > 0 or hours > 0) {
try self.writer.print("{}s", .{seconds});
}
}
fn renderCwd(self: *Self) !void {
var cwd_buf: [512]u8 = undefined;
const cwd = try std.os.getcwd(&cwd_buf);