From 2d7668618610c168c34ea936d7f2e9cce928b8e0 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Tue, 18 Jul 2023 13:04:33 +0200 Subject: [PATCH] add messages to mzteinit --- scripts/mzteinit/src/env.zig | 7 +++++++ scripts/mzteinit/src/figlet.zig | 8 ++++---- scripts/mzteinit/src/main.zig | 12 +----------- scripts/mzteinit/src/message.zig | 21 +++++++++++++++++++++ scripts/mzteinit/src/util.zig | 7 +++++++ 5 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 scripts/mzteinit/src/message.zig diff --git a/scripts/mzteinit/src/env.zig b/scripts/mzteinit/src/env.zig index 08d1a73..16ba662 100644 --- a/scripts/mzteinit/src/env.zig +++ b/scripts/mzteinit/src/env.zig @@ -1,6 +1,8 @@ const std = @import("std"); const sysdaemon = @import("sysdaemon.zig"); +const msg = @import("message.zig").msg; + const log = std.log.scoped(.env); const delimitedWriter = @import("delimited_writer.zig").delimitedWriter; @@ -8,6 +10,7 @@ const delimitedWriter = @import("delimited_writer.zig").delimitedWriter; /// Initialize the environment. /// Returns true if the environment should be transferred to the system daemon. pub fn populateEnvironment(env: *std.process.EnvMap) !bool { + try msg("Loading environment...", .{}); // buffer for building values for env vars var buf: [1024 * 8]u8 = undefined; @@ -75,6 +78,7 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool { // icon path icons: { + try msg("building $ICONPATH...", .{}); const path = "/usr/share/icons/candy-icons"; var dir = std.fs.openIterableDirAbsolute(path, .{}) catch { log.warn( @@ -126,6 +130,7 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool { // racket bins racket: { + try msg("acquiring racket binary path...", .{}); const res = std.ChildProcess.exec(.{ .allocator = alloc, .argv = &.{ @@ -175,6 +180,8 @@ pub fn populateSysdaemonEnvironment(env: *const std.process.EnvMap) !void { if (try sysdaemon.getCurrentSystemDaemon() != .systemd) return; + try msg("updating SystemD environment...", .{}); + var argv = try std.ArrayList([]const u8).initCapacity(env.hash_map.allocator, env.count() + 3); defer argv.deinit(); diff --git a/scripts/mzteinit/src/figlet.zig b/scripts/mzteinit/src/figlet.zig index cb04cd2..0be3dfd 100644 --- a/scripts/mzteinit/src/figlet.zig +++ b/scripts/mzteinit/src/figlet.zig @@ -1,6 +1,8 @@ const std = @import("std"); const at = @import("ansi-term"); +const util = @import("util.zig"); + // r means switch to red const figlet = \\ __ __r__ ________ ____________ @@ -15,12 +17,10 @@ pub fn writeFiglet(writer: anytype) !void { for (figlet) |char| { switch (char) { 'r' => { - try at.format.updateStyle(writer, .{ .foreground = .Red }, style); - style = .{ .foreground = .Red }; + try util.updateStyle(writer, &style, .{ .foreground = .Red }); }, '\n' => { - try at.format.updateStyle(writer, .{ .foreground = .Default }, style); - style = .{ .foreground = .Default }; + try util.updateStyle(writer, &style, .{}); try writer.writeByte('\n'); }, else => try writer.writeByte(char), diff --git a/scripts/mzteinit/src/main.zig b/scripts/mzteinit/src/main.zig index 72ecdf6..aa99d48 100644 --- a/scripts/mzteinit/src/main.zig +++ b/scripts/mzteinit/src/main.zig @@ -54,19 +54,9 @@ fn tryMain() !void { defer _ = gpa.deinit(); const alloc = gpa.allocator(); - var env_map = std.process.EnvMap.init(alloc); + var env_map = try std.process.getEnvMap(alloc); defer env_map.deinit(); - for (std.os.environ) |env_var_z| { - const env_var = std.mem.span(env_var_z); - const eq_idx = std.mem.indexOfScalar(u8, env_var, '=').?; - - const key = env_var[0..eq_idx]; - const value = env_var[eq_idx + 1 ..]; - - try env_map.put(key, value); - } - if (env_map.get("MZTEINIT")) |_| { try stdout.writer().writeAll("mzteinit running already, starting shell\n"); try stdout.flush(); diff --git a/scripts/mzteinit/src/message.zig b/scripts/mzteinit/src/message.zig new file mode 100644 index 0000000..f535d3a --- /dev/null +++ b/scripts/mzteinit/src/message.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const at = @import("ansi-term"); + +const util = @import("util.zig"); + +pub fn msg(comptime fmt: []const u8, args: anytype) !void { + const writer = std.io.getStdErr().writer(); + var style: ?at.style.Style = null; + + try util.updateStyle(writer, &style, .{ .font_style = .{ .bold = true } }); + try writer.writeByte('['); + try util.updateStyle(writer, &style, .{ .font_style = .{ .bold = true }, .foreground = .Red }); + try writer.writeAll(" MZTEINIT "); + try util.updateStyle(writer, &style, .{ .font_style = .{ .bold = true } }); + try writer.writeByte(']'); + try util.updateStyle(writer, &style, .{ .foreground = .Cyan }); + + try std.fmt.format(writer, " " ++ fmt ++ "\n", args); + + try util.updateStyle(writer, &style, .{}); +} diff --git a/scripts/mzteinit/src/util.zig b/scripts/mzteinit/src/util.zig index e1ea9a3..800af44 100644 --- a/scripts/mzteinit/src/util.zig +++ b/scripts/mzteinit/src/util.zig @@ -1,3 +1,10 @@ +const at = @import("ansi-term"); + pub const ansi_clear = "\x1b[2J\x1b[1;1H"; pub const ExitMode = enum { run, immediate, delayed }; + +pub inline fn updateStyle(writer: anytype, current: *?at.style.Style, new: at.style.Style) !void { + try at.format.updateStyle(writer, new, current.*); + current.* = new; +}