const std = @import("std"); stdout: std.fs.File.Writer, stderr: std.fs.File.Writer, colors: bool = false, disable: bool = false, tabs: usize = 0, const Self = @This(); pub fn init() Self { var stdout = std.io.getStdOut(); const alloc = std.heap.c_allocator; var enable_colors = false; const NO_COLOR_DEFINED = std.process.hasNonEmptyEnvVar(alloc, "NO_COLOR") catch false; if (!NO_COLOR_DEFINED) { enable_colors = stdout.getOrEnableAnsiEscapeSupport(); } return .{ .stdout = stdout.writer(), .stderr = std.io.getStdErr().writer(), .colors = enable_colors, .tabs = 0, }; } pub fn sub(self: Self) Self { return .{ .stdout = self.stdout, .stderr = self.stderr, .disable = self.disable, .tabs = self.tabs + 1, }; } pub fn raw(self: Self, comptime fmt: []const u8, args: anytype) void { if (self.disable) return; _ = self.stdout.print(fmt, args) catch {}; } fn tag(colors: bool, writer: std.fs.File.Writer, t: []const u8) !void { if (colors) { try writer.writeAll("\x1b[2m["); try writer.writeAll(t); try writer.writeAll("]\x1b[0m "); return; } try writer.writeAll("["); try writer.writeAll(t); try writer.writeAll("] "); } pub fn header(self: Self, text: []const u8) void { self.stdout.writeByte('\n') catch unreachable; tag(self.colors, self.stdout, text) catch unreachable; self.stdout.writeByte('\n') catch unreachable; } pub fn info(self: Self, comptime fmt: []const u8, args: anytype) void { if (self.disable) return; tag(self.colors, self.stdout, "info.") catch unreachable; _ = self.stdout.print(fmt, args) catch return; self.stdout.writeByte('\n') catch unreachable; } pub fn warn(self: Self, comptime fmt: []const u8, args: anytype) void { if (self.disable) return; tag(self.colors, self.stdout, "WARN.") catch unreachable; _ = self.stdout.print(fmt, args) catch return; self.stdout.writeByte('\n') catch unreachable; } pub fn err(self: Self, comptime fmt: []const u8, args: anytype) void { if (self.disable) return; tag(self.colors, self.stderr, "ERROR") catch unreachable; _ = self.stderr.print(fmt, args) catch return; self.stdout.writeByte('\n') catch unreachable; } pub fn fatal(self: Self, comptime fmt: []const u8, args: anytype) noreturn { tag(self.colors, self.stderr, "FATAL") catch unreachable; _ = self.stderr.print(fmt, args) catch unreachable; self.stderr.writeAll("\n") catch unreachable; std.process.exit(1); }