From 551c33f37ce8627d08514cabacf1f772fd43698a Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Fri, 9 Sep 2022 16:33:24 +0200 Subject: [PATCH] improve mzteinit io --- scripts/mzteinit/src/main.zig | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/scripts/mzteinit/src/main.zig b/scripts/mzteinit/src/main.zig index 3a34f1e6..68f8d1fc 100644 --- a/scripts/mzteinit/src/main.zig +++ b/scripts/mzteinit/src/main.zig @@ -3,22 +3,24 @@ const at = @import("ansi-term"); const run = @import("run.zig"); pub fn main() !void { + var stdout = std.io.bufferedWriter(std.io.getStdOut().writer()); + + try stdout.writer().writeAll("\x1b[2J\x1b[1;1H"); // clear while (true) { - try std.io.getStdOut().writeAll("\x1b[2J\x1b[1;1H"); // clear - const cmd = ui() catch |e| { + const cmd = ui(&stdout) catch |e| { std.debug.print("Error rendering the UI: {}\n", .{e}); break; }; cmd.run() catch |e| { - try std.io.getStdOut().writeAll("\x1b[2J\x1b[1;1H"); // clear - std.debug.print("Error running command: {}\n", .{e}); + try stdout.writer().print("Error running command: {}\n\n", .{e}); + continue; }; + try stdout.writer().writeAll("\x1b[2J\x1b[1;1H"); // clear } } -fn ui() !run.Command { - var stdout = std.io.bufferedWriter(std.io.getStdOut().writer()); - const w = stdout.writer(); +fn ui(buf_writer: anytype) !run.Command { + const w = buf_writer.writer(); try @import("figlet.zig").writeFiglet(w); try w.writeAll( @@ -37,7 +39,7 @@ fn ui() !run.Command { } try at.format.resetStyle(w); - try stdout.flush(); + try buf_writer.flush(); const old_termios = try std.os.tcgetattr(std.os.STDIN_FILENO); var new_termios = old_termios; @@ -52,7 +54,7 @@ fn ui() !run.Command { cmd = run.Command.fromChar(c[0]); if (cmd == null) { try w.print("Unknown command '{s}'\n", .{c}); - try stdout.flush(); + try buf_writer.flush(); } } try std.os.tcsetattr(std.os.STDIN_FILENO, .NOW, old_termios);