diff --git a/scripts/mzteinit/src/main.zig b/scripts/mzteinit/src/main.zig index c5f7546..ed624b1 100644 --- a/scripts/mzteinit/src/main.zig +++ b/scripts/mzteinit/src/main.zig @@ -1,21 +1,34 @@ const std = @import("std"); const at = @import("ansi-term"); const run = @import("run.zig"); +const util = @import("util.zig"); pub fn main() !void { var stdout = std.io.bufferedWriter(std.io.getStdOut().writer()); + var exit = false; - try stdout.writer().writeAll("\x1b[2J\x1b[1;1H"); // clear while (true) { + try util.writeAnsiClear(stdout.writer()); + const cmd = ui(&stdout) catch |e| { std.debug.print("Error rendering the UI: {}\n", .{e}); break; }; - cmd.run() catch |e| { + + try util.writeAnsiClear(stdout.writer()); + try stdout.flush(); + + cmd.run(&exit) catch |e| { try stdout.writer().print("Error running command: {}\n\n", .{e}); continue; }; - try stdout.writer().writeAll("\x1b[2J\x1b[1;1H"); // clear + + if (exit) { + try stdout.writer().writeAll("Goodbye!"); + try stdout.flush(); + std.time.sleep(2 * std.time.ns_per_s); + return; + } } } diff --git a/scripts/mzteinit/src/run.zig b/scripts/mzteinit/src/run.zig index 305546a..d8c4d02 100644 --- a/scripts/mzteinit/src/run.zig +++ b/scripts/mzteinit/src/run.zig @@ -4,6 +4,7 @@ pub const Command = enum { startx, shell, zellij, + logout, shutdown, reboot, @@ -12,6 +13,7 @@ pub const Command = enum { 'x', 'X' => .startx, 's', 'S' => .shell, 'z', 'Z' => .zellij, + 'l', 'L' => .logout, 'p', 'P' => .shutdown, 'r', 'R' => .reboot, else => null, @@ -23,12 +25,18 @@ pub const Command = enum { .startx => 'X', .shell => 'S', .zellij => 'Z', + .logout => 'L', .shutdown => 'P', .reboot => 'R', }; } - pub fn run(self: Command) !void { + pub fn run(self: Command, exit: *bool) !void { + if (self == .logout) { + exit.* = true; + return; + } + var mem: [512]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&mem); const arg = self.argv(); @@ -41,6 +49,7 @@ pub const Command = enum { .startx => &.{"startx"}, .shell => &.{"fish"}, .zellij => &.{"zellij"}, + .logout => unreachable, .shutdown => &.{ "systemctl", "poweroff" }, .reboot => &.{ "systemctl", "reboot" }, }; diff --git a/scripts/mzteinit/src/util.zig b/scripts/mzteinit/src/util.zig new file mode 100644 index 0000000..00732f7 --- /dev/null +++ b/scripts/mzteinit/src/util.zig @@ -0,0 +1,3 @@ +pub fn writeAnsiClear(writer: anytype) !void { + try writer.writeAll("\x1b[2J\x1b[1;1H"); +}