dotfiles/scripts/prompt/src/main.zig

68 lines
2.1 KiB
Zig
Raw Normal View History

2022-06-18 17:08:41 +02:00
const std = @import("std");
const FishMode = @import("FishMode.zig");
const prompt = @import("prompt.zig");
const fish_code =
\\functions -e fish_mode_prompt
\\function fish_prompt
2023-04-20 00:05:06 +02:00
\\ set -x MZPROMPT_STATUS $status
\\ set -x MZPROMPT_FISH_MODE $fish_bind_mode
\\ set -x MZPROMPT_DURATION $CMD_DURATION
2023-04-20 07:54:30 +02:00
\\ set -x MZPROMPT_JOBS (count (jobs))
2023-04-20 00:05:06 +02:00
\\ {s} show
2022-06-18 17:08:41 +02:00
\\end
;
2024-03-20 20:45:59 +01:00
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
2022-06-18 17:08:41 +02:00
pub fn main() !void {
if (std.os.argv.len < 2)
return error.NotEnoughArguments;
2023-06-27 23:00:13 +02:00
const verb = std.mem.span(std.os.argv[1]);
if (std.mem.eql(u8, verb, "printfish")) {
2022-06-18 17:08:41 +02:00
const stdout = std.io.getStdOut();
try stdout.writer().print(fish_code ++ "\n", .{std.os.argv[0]});
2023-06-27 23:00:13 +02:00
} else if (std.mem.eql(u8, verb, "show")) {
2023-04-20 00:05:06 +02:00
const options = prompt.Options{
.status = try std.fmt.parseInt(
i16,
std.posix.getenv("MZPROMPT_STATUS") orelse
2023-04-20 00:05:06 +02:00
return error.MissingEnv,
10,
),
.mode = FishMode.parse(
std.posix.getenv("MZPROMPT_FISH_MODE") orelse
2023-04-20 00:05:06 +02:00
return error.MissingEnv,
),
.duration = try std.fmt.parseInt(
u32,
std.posix.getenv("MZPROMPT_DURATION") orelse
2023-04-20 00:05:06 +02:00
return error.MissingEnv,
10,
),
2023-04-20 07:54:30 +02:00
.jobs = try std.fmt.parseInt(
u32,
std.posix.getenv("MZPROMPT_JOBS") orelse
2023-04-20 07:54:30 +02:00
return error.MissingEnv,
10,
),
2024-04-14 22:06:02 +02:00
.nix_name = @import("nix.zig").findNixShellName(),
2023-04-20 00:05:06 +02:00
};
2023-04-18 22:32:47 +02:00
2023-07-01 19:20:02 +02:00
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;
2023-04-18 22:37:40 +02:00
};
2023-07-01 19:20:02 +02:00
try std.io.getStdOut().writeAll(fbs.getWritten());
2022-06-18 17:08:41 +02:00
} else {
return error.UnknownCommand;
}
}