const std = @import("std"); const W = std.Io.Writer; const E = W.Error; pub const Style = enum(u8) { reset, bold, dim, italic, underline, blink, inverse, invisible, strikethrough, pub fn ansi(self: Style) u8 { return @intFromEnum(self); } }; pub fn style(w: *W, b: Style) E!void { try base(w, b.ansi()); } pub const Color = enum(u8) { black, red, green, yellow, blue, magenta, cyan, white, default, pub fn int(n: u8) Color { return @enumFromInt(n); } pub fn foreground(self: Color) u8 { return @intFromEnum(self) + 30; } pub fn background(self: Color) u8 { return @intFromEnum(self) + 40; } pub fn foreground_bright(self: Color) u8 { return @intFromEnum(self) + 90; } pub fn background_bright(self: Color) u8 { return @intFromEnum(self) + 100; } }; pub fn fg(w: *W, color: Color) E!void { try base(w, color.foreground()); } pub fn bg(w: *W, color: Color) E!void { try base(w, color.background()); } pub fn fg_bright(w: *W, color: Color) E!void { try base(w, color.foreground_bright()); } pub fn bg_bright(w: *W, color: Color) E!void { try base(w, color.background_bright()); } pub fn fg_256(w: *W, color: u8) E!void { try params(w, &[_]u8{ 38, 5, color }); } pub fn bg_256(w: *W, color: u8) E!void { try params(w, &[_]u8{ 48, 5, color }); } pub fn fg_rgb(w: *W, red: u8, green: u8, blue: u8) E!void { try params(w, &[_]u8{ 38, 2, red, green, blue }); } pub fn bg_rgb(w: *W, red: u8, green: u8, blue: u8) E!void { try params(w, &[_]u8{ 48, 2, red, green, blue }); } fn params(w: *W, p: []const u8) E!void { try csi(w); for (p, 0..) |c, i| { try w.printInt(c, 10, .lower, .{}); if (i < p.len - 1) try w.writeByte(';'); } try w.writeByte('m'); } fn base(w: *W, c: u8) E!void { try csi(w); try w.printInt(c, 10, .lower, .{}); try w.writeByte('m'); } fn csi(w: *W) E!void { try w.writeByte('\x1B'); try w.writeByte('['); }