update mzteinit

This commit is contained in:
LordMZTE 2022-10-04 16:00:23 +02:00
parent 37d063ae24
commit 33acf673ae
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 29 additions and 4 deletions

View file

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

View file

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

View file

@ -0,0 +1,3 @@
pub fn writeAnsiClear(writer: anytype) !void {
try writer.writeAll("\x1b[2J\x1b[1;1H");
}