add messages to mzteinit

This commit is contained in:
LordMZTE 2023-07-18 13:04:33 +02:00
parent 005a04779f
commit 2d76686186
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
5 changed files with 40 additions and 15 deletions

View file

@ -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();

View file

@ -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),

View file

@ -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();

View file

@ -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, .{});
}

View file

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