minor mzteinit improvements

This commit is contained in:
LordMZTE 2023-06-02 16:29:17 +02:00
parent 8e72b3ab93
commit 96f62f2283
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 23 additions and 10 deletions

View file

@ -49,7 +49,6 @@ pub fn main() void {
fn tryMain() !void {
var stdout = std.io.bufferedWriter(std.io.getStdOut().writer());
var exit = false;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
@ -101,16 +100,21 @@ fn tryMain() !void {
try util.writeAnsiClear(stdout.writer());
try stdout.flush();
var exit = util.ExitMode.run;
cmd.run(alloc, &exit, &env_map) catch |e| {
try stdout.writer().print("Error running command: {}\n\n", .{e});
continue;
};
if (exit) {
try stdout.writer().writeAll("Goodbye!");
try stdout.flush();
std.time.sleep(2 * std.time.ns_per_s);
return;
switch (exit) {
.run => {},
.immediate => return,
.delayed => {
try stdout.writer().writeAll("Goodbye!");
try stdout.flush();
std.time.sleep(2 * std.time.ns_per_s);
return;
},
}
}
}

View file

@ -1,4 +1,5 @@
const std = @import("std");
const log = std.log.scoped(.run);
pub const Command = enum {
startx,
@ -34,15 +35,21 @@ pub const Command = enum {
pub fn run(
self: Command,
alloc: std.mem.Allocator,
exit: *bool,
exit: *@import("util.zig").ExitMode,
env: *const std.process.EnvMap,
) !void {
if (self == .logout) {
exit.* = true;
return;
switch (self) {
.logout => {
exit.* = .delayed;
log.info("user logged out", .{});
return;
},
.shutdown, .reboot => exit.* = .immediate,
else => {},
}
const arg = self.argv();
log.info("run cmd: {s}", .{arg});
var child = std.ChildProcess.init(arg, alloc);
child.env_map = env;
_ = try child.spawnAndWait();

View file

@ -1,3 +1,5 @@
pub fn writeAnsiClear(writer: anytype) !void {
try writer.writeAll("\x1b[2J\x1b[1;1H");
}
pub const ExitMode = enum { run, immediate, delayed };