| ... | @@ -0,0 +1,174 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | pub const ascii = enum(u8) { |
| 4 | NUL, |
| 5 | SOH, |
| 6 | STX, |
| 7 | ETX, |
| 8 | EOT, |
| 9 | ENQ, |
| 10 | ACK, |
| 11 | BEL, |
| 12 | BS, |
| 13 | TAB, |
| 14 | LF, |
| 15 | VT, |
| 16 | FF, |
| 17 | CR, |
| 18 | SO, |
| 19 | SI, |
| 20 | DLE, |
| 21 | DC1, |
| 22 | DC2, |
| 23 | DC3, |
| 24 | DC4, |
| 25 | NAK, |
| 26 | SYN, |
| 27 | ETB, |
| 28 | CAN, |
| 29 | EM, |
| 30 | SUB, |
| 31 | ESC, |
| 32 | FS, |
| 33 | GS, |
| 34 | RS, |
| 35 | US, |
| 36 | |
| 37 | pub fn s(self: ascii) []const u8 { |
| 38 | return &[_]u8{@enumToInt(self)}; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | pub const escape = struct { |
| 43 | pub const SS2 = ascii.ESC.s() ++ "N"; |
| 44 | pub const SS3 = ascii.ESC.s() ++ "O"; |
| 45 | pub const DCS = ascii.ESC.s() ++ "P"; |
| 46 | pub const CSI = ascii.ESC.s() ++ "["; |
| 47 | pub const ST = ascii.ESC.s() ++ "\\"; |
| 48 | pub const OSC = ascii.ESC.s() ++ "]"; |
| 49 | pub const SOS = ascii.ESC.s() ++ "X"; |
| 50 | pub const PM = ascii.ESC.s() ++ "^"; |
| 51 | pub const APC = ascii.ESC.s() ++ "_"; |
| 52 | pub const RIS = ascii.ESC.s() ++ "c"; |
| 53 | }; |
| 54 | |
| 55 | fn make_csi_sequence(comptime c: []const u8, x: anytype) []const u8 { |
| 56 | return escape.CSI ++ _join(";", arr_i_to_s(x)) ++ c; |
| 57 | } |
| 58 | |
| 59 | fn arr_i_to_s(x: anytype) [][]const u8 { |
| 60 | var res: [x.len][]const u8 = undefined; |
| 61 | for (x) |item, i| { |
| 62 | res[i] = std.fmt.comptimePrint("{}", .{item}); |
| 63 | } |
| 64 | return &res; |
| 65 | } |
| 66 | |
| 67 | pub const csi = struct { |
| 68 | fn CursorUp(n: i32) []const u8 { return make_csi_sequence("A", .{n}); } |
| 69 | fn CursorDown(n: i32) []const u8 { return make_csi_sequence("B", .{n}); } |
| 70 | fn CursorForward(n: i32) []const u8 { return make_csi_sequence("C", .{n}); } |
| 71 | fn CursorBack(n: i32) []const u8 { return make_csi_sequence("D", .{n}); } |
| 72 | fn CursorNextLine(n: i32) []const u8 { return make_csi_sequence("E", .{n}); } |
| 73 | fn CursorPrevLine(n: i32) []const u8 { return make_csi_sequence("F", .{n}); } |
| 74 | fn CursorHorzAbs(n: i32) []const u8 { return make_csi_sequence("G", .{n}); } |
| 75 | fn CursorPos(n: i32, m: i32) []const u8 { return make_csi_sequence("H", .{n, m}); } |
| 76 | fn EraseInDisplay(n: i32) []const u8 { return make_csi_sequence("J", .{n}); } |
| 77 | fn EraseInLine(n: i32) []const u8 { return make_csi_sequence("K", .{n}); } |
| 78 | fn ScrollUp(n: i32) []const u8 { return make_csi_sequence("S", .{n}); } |
| 79 | fn ScrollDown(n: i32) []const u8 { return make_csi_sequence("T", .{n}); } |
| 80 | fn HorzVertPos(n: i32, m: i32) []const u8 { return make_csi_sequence("f", .{n, m}); } |
| 81 | fn SGR(ns: anytype) []const u8 { return make_csi_sequence("m", ns); } |
| 82 | }; |
| 83 | |
| 84 | pub const style = struct { |
| 85 | pub const ResetAll = csi.SGR(.{0}); |
| 86 | |
| 87 | pub const Bold = csi.SGR(.{1}); |
| 88 | pub const Faint = csi.SGR(.{2}); |
| 89 | pub const Italic = csi.SGR(.{3}); |
| 90 | pub const Underline = csi.SGR(.{4}); |
| 91 | pub const BlinkSlow = csi.SGR(.{5}); |
| 92 | pub const BlinkFast = csi.SGR(.{6}); |
| 93 | |
| 94 | pub const ResetFont = csi.SGR(.{10}); |
| 95 | pub const Font1 = csi.SGR(.{11}); |
| 96 | pub const Font2 = csi.SGR(.{12}); |
| 97 | pub const Font3 = csi.SGR(.{13}); |
| 98 | pub const Font4 = csi.SGR(.{14}); |
| 99 | pub const Font5 = csi.SGR(.{15}); |
| 100 | pub const Font6 = csi.SGR(.{16}); |
| 101 | pub const Font7 = csi.SGR(.{17}); |
| 102 | pub const Font8 = csi.SGR(.{18}); |
| 103 | pub const Font9 = csi.SGR(.{19}); |
| 104 | |
| 105 | pub const UnderlineDouble = csi.SGR(.{21}); |
| 106 | pub const ResetIntensity = csi.SGR(.{22}); |
| 107 | pub const ResetItalic = csi.SGR(.{23}); |
| 108 | pub const ResetUnderline = csi.SGR(.{24}); |
| 109 | pub const ResetBlink = csi.SGR(.{25}); |
| 110 | |
| 111 | pub const FgBlack = csi.SGR(.{30}); |
| 112 | pub const FgRed = csi.SGR(.{31}); |
| 113 | pub const FgGreen = csi.SGR(.{32}); |
| 114 | pub const FgYellow = csi.SGR(.{33}); |
| 115 | pub const FgBlue = csi.SGR(.{34}); |
| 116 | pub const FgMagenta = csi.SGR(.{35}); |
| 117 | pub const FgCyan = csi.SGR(.{36}); |
| 118 | pub const FgWhite = csi.SGR(.{37}); |
| 119 | // Fg8bit = func(n int) string { return csi.SGR(38, 5, n) } |
| 120 | // Fg24bit = func(r, g, b int) string { return csi.SGR(38, 2, r, g, b) } |
| 121 | pub const ResetFgColor = csi.SGR(.{39}); |
| 122 | |
| 123 | pub const BgBlack = csi.SGR(.{40}); |
| 124 | pub const BgRed = csi.SGR(.{41}); |
| 125 | pub const BgGreen = csi.SGR(.{42}); |
| 126 | pub const BgYellow = csi.SGR(.{43}); |
| 127 | pub const BgBlue = csi.SGR(.{44}); |
| 128 | pub const BgMagenta = csi.SGR(.{45}); |
| 129 | pub const BgCyan = csi.SGR(.{46}); |
| 130 | pub const BgWhite = csi.SGR(.{47}); |
| 131 | // Bg8bit = func(n int) string { return csi.SGR(48, 5, n) } |
| 132 | // Bg24bit = func(r, g, b int) string { return csi.SGR(48, 2, r, g, b) } |
| 133 | pub const ResetBgColor = csi.SGR(.{49}); |
| 134 | |
| 135 | pub const Framed = csi.SGR(.{51}); |
| 136 | pub const Encircled = csi.SGR(.{52}); |
| 137 | pub const Overlined = csi.SGR(.{53}); |
| 138 | pub const ResetFrameEnci = csi.SGR(.{54}); |
| 139 | pub const ResetOverlined = csi.SGR(.{55}); |
| 140 | }; |
| 141 | |
| 142 | pub const color = struct { |
| 143 | pub const Color = enum(u8) { |
| 144 | Black, |
| 145 | Red, |
| 146 | Green, |
| 147 | Yellow, |
| 148 | Blue, |
| 149 | Magenta, |
| 150 | Cyan, |
| 151 | White, |
| 152 | }; |
| 153 | |
| 154 | pub fn Fg(s: Color, comptime m: []const u8) []const u8 { |
| 155 | return csi.SGR(.{30+@enumToInt(s)}) ++ m ++ style.ResetFgColor; |
| 156 | } |
| 157 | |
| 158 | pub fn Bg(s: Color, comptime m: []const u8) []const u8 { |
| 159 | return csi.SGR(.{40+@enumToInt(s)}) ++ m ++ style.ResetBgColor; |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | // |
| 164 | // private |
| 165 | // |
| 166 | |
| 167 | fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 { |
| 168 | var buf: []const u8 = ""; |
| 169 | for (xs) |x,i| { |
| 170 | buf = buf ++ x; |
| 171 | if (i < xs.len-1) buf = buf ++ delim; |
| 172 | } |
| 173 | return buf; |
| 174 | } |