Makko, the people-oriented static site generator made for blogging. forge.starlightnet.work/Team/Makko
ssg static-site-generator makko starlight-network
at main 2.6 kB view raw
1const std = @import("std"); 2 3stdout: std.fs.File.Writer, 4stderr: std.fs.File.Writer, 5colors: bool = false, 6disable: bool = false, 7tabs: usize = 0, 8 9const Self = @This(); 10 11pub fn init() Self { 12 var stdout = std.io.getStdOut(); 13 14 const alloc = std.heap.c_allocator; 15 16 var enable_colors = false; 17 const NO_COLOR_DEFINED = std.process.hasNonEmptyEnvVar(alloc, "NO_COLOR") catch false; 18 if (!NO_COLOR_DEFINED) { 19 enable_colors = stdout.getOrEnableAnsiEscapeSupport(); 20 } 21 22 return .{ 23 .stdout = stdout.writer(), 24 .stderr = std.io.getStdErr().writer(), 25 .colors = enable_colors, 26 .tabs = 0, 27 }; 28} 29 30pub fn sub(self: Self) Self { 31 return .{ 32 .stdout = self.stdout, 33 .stderr = self.stderr, 34 .disable = self.disable, 35 .tabs = self.tabs + 1, 36 }; 37} 38 39pub fn raw(self: Self, comptime fmt: []const u8, args: anytype) void { 40 if (self.disable) return; 41 _ = self.stdout.print(fmt, args) catch {}; 42} 43 44fn tag(colors: bool, writer: std.fs.File.Writer, t: []const u8) !void { 45 if (colors) { 46 try writer.writeAll("\x1b[2m["); 47 try writer.writeAll(t); 48 try writer.writeAll("]\x1b[0m "); 49 return; 50 } 51 52 try writer.writeAll("["); 53 try writer.writeAll(t); 54 try writer.writeAll("] "); 55} 56 57pub fn header(self: Self, text: []const u8) void { 58 self.stdout.writeByte('\n') catch unreachable; 59 tag(self.colors, self.stdout, text) catch unreachable; 60 self.stdout.writeByte('\n') catch unreachable; 61} 62 63pub fn info(self: Self, comptime fmt: []const u8, args: anytype) void { 64 if (self.disable) return; 65 66 tag(self.colors, self.stdout, "info.") catch unreachable; 67 _ = self.stdout.print(fmt, args) catch return; 68 self.stdout.writeByte('\n') catch unreachable; 69} 70 71pub fn warn(self: Self, comptime fmt: []const u8, args: anytype) void { 72 if (self.disable) return; 73 74 tag(self.colors, self.stdout, "WARN.") catch unreachable; 75 _ = self.stdout.print(fmt, args) catch return; 76 self.stdout.writeByte('\n') catch unreachable; 77} 78 79pub fn err(self: Self, comptime fmt: []const u8, args: anytype) void { 80 if (self.disable) return; 81 82 tag(self.colors, self.stderr, "ERROR") catch unreachable; 83 _ = self.stderr.print(fmt, args) catch return; 84 self.stdout.writeByte('\n') catch unreachable; 85} 86 87pub fn fatal(self: Self, comptime fmt: []const u8, args: anytype) noreturn { 88 tag(self.colors, self.stderr, "FATAL") catch unreachable; 89 _ = self.stderr.print(fmt, args) catch unreachable; 90 self.stderr.writeAll("\n") catch unreachable; 91 92 std.process.exit(1); 93}