authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:09:19 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:09:19 -08:00
loge8254d11e5346de06a38f74ec7531958bdbc16a1
treef7ba7c35d1bd8db1054c33f2d6319bae72f299ae
parent099cd5c67482bf339740212a457a053a98a96944

zig: add util/ansi


1 files changed, 130 insertions(+), 0 deletions(-)

src/util/ansi.zig created+130
......@@ -0,0 +1,130 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const u = @import("index.zig");
5
6//
7//
8
9pub const ansi = struct {
10 pub const escape = struct {
11 pub const SS2 = u.ascii.ESC.s() ++ "N";
12 pub const SS3 = u.ascii.ESC.s() ++ "O";
13 pub const DCS = u.ascii.ESC.s() ++ "P";
14 pub const CSI = u.ascii.ESC.s() ++ "[";
15 pub const ST = u.ascii.ESC.s() ++ "\\";
16 pub const OSC = u.ascii.ESC.s() ++ "]";
17 pub const SOS = u.ascii.ESC.s() ++ "X";
18 pub const PM = u.ascii.ESC.s() ++ "^";
19 pub const APC = u.ascii.ESC.s() ++ "_";
20 pub const RIS = u.ascii.ESC.s() ++ "c";
21 };
22
23 fn make_csi_sequence(comptime c: []const u8, x: anytype) []const u8 {
24 return escape.CSI ++ u._join(";", arr_i_to_s(x)) ++ c;
25 }
26
27 fn arr_i_to_s(x: anytype) [][]const u8 {
28 var res: [x.len][]const u8 = undefined;
29 for (x) |item, i| {
30 res[i] = std.fmt.comptimePrint("{}", .{item});
31 }
32 return &res;
33 }
34
35 pub const csi = struct {
36 fn CursorUp(n: i32) []const u8 { return make_csi_sequence("A", .{n}); }
37 fn CursorDown(n: i32) []const u8 { return make_csi_sequence("B", .{n}); }
38 fn CursorForward(n: i32) []const u8 { return make_csi_sequence("C", .{n}); }
39 fn CursorBack(n: i32) []const u8 { return make_csi_sequence("D", .{n}); }
40 fn CursorNextLine(n: i32) []const u8 { return make_csi_sequence("E", .{n}); }
41 fn CursorPrevLine(n: i32) []const u8 { return make_csi_sequence("F", .{n}); }
42 fn CursorHorzAbs(n: i32) []const u8 { return make_csi_sequence("G", .{n}); }
43 fn CursorPos(n: i32, m: i32) []const u8 { return make_csi_sequence("H", .{n, m}); }
44 fn EraseInDisplay(n: i32) []const u8 { return make_csi_sequence("J", .{n}); }
45 fn EraseInLine(n: i32) []const u8 { return make_csi_sequence("K", .{n}); }
46 fn ScrollUp(n: i32) []const u8 { return make_csi_sequence("S", .{n}); }
47 fn ScrollDown(n: i32) []const u8 { return make_csi_sequence("T", .{n}); }
48 fn HorzVertPos(n: i32, m: i32) []const u8 { return make_csi_sequence("f", .{n, m}); }
49 fn SGR(ns: anytype) []const u8 { return make_csi_sequence("m", ns); }
50 };
51
52 pub const style = struct {
53 pub const ResetAll = csi.SGR(.{0});
54
55 pub const Bold = csi.SGR(.{1});
56 pub const Faint = csi.SGR(.{2});
57 pub const Italic = csi.SGR(.{3});
58 pub const Underline = csi.SGR(.{4});
59 pub const BlinkSlow = csi.SGR(.{5});
60 pub const BlinkFast = csi.SGR(.{6});
61
62 pub const ResetFont = csi.SGR(.{10});
63 pub const Font1 = csi.SGR(.{11});
64 pub const Font2 = csi.SGR(.{12});
65 pub const Font3 = csi.SGR(.{13});
66 pub const Font4 = csi.SGR(.{14});
67 pub const Font5 = csi.SGR(.{15});
68 pub const Font6 = csi.SGR(.{16});
69 pub const Font7 = csi.SGR(.{17});
70 pub const Font8 = csi.SGR(.{18});
71 pub const Font9 = csi.SGR(.{19});
72
73 pub const UnderlineDouble = csi.SGR(.{21});
74 pub const ResetIntensity = csi.SGR(.{22});
75 pub const ResetItalic = csi.SGR(.{23});
76 pub const ResetUnderline = csi.SGR(.{24});
77 pub const ResetBlink = csi.SGR(.{25});
78
79 pub const FgBlack = csi.SGR(.{30});
80 pub const FgRed = csi.SGR(.{31});
81 pub const FgGreen = csi.SGR(.{32});
82 pub const FgYellow = csi.SGR(.{33});
83 pub const FgBlue = csi.SGR(.{34});
84 pub const FgMagenta = csi.SGR(.{35});
85 pub const FgCyan = csi.SGR(.{36});
86 pub const FgWhite = csi.SGR(.{37});
87 // Fg8bit = func(n int) string { return csi.SGR(38, 5, n) }
88 // Fg24bit = func(r, g, b int) string { return csi.SGR(38, 2, r, g, b) }
89 pub const ResetFgColor = csi.SGR(.{39});
90
91 pub const BgBlack = csi.SGR(.{40});
92 pub const BgRed = csi.SGR(.{41});
93 pub const BgGreen = csi.SGR(.{42});
94 pub const BgYellow = csi.SGR(.{43});
95 pub const BgBlue = csi.SGR(.{44});
96 pub const BgMagenta = csi.SGR(.{45});
97 pub const BgCyan = csi.SGR(.{46});
98 pub const BgWhite = csi.SGR(.{47});
99 // Bg8bit = func(n int) string { return csi.SGR(48, 5, n) }
100 // Bg24bit = func(r, g, b int) string { return csi.SGR(48, 2, r, g, b) }
101 pub const ResetBgColor = csi.SGR(.{49});
102
103 pub const Framed = csi.SGR(.{51});
104 pub const Encircled = csi.SGR(.{52});
105 pub const Overlined = csi.SGR(.{53});
106 pub const ResetFrameEnci = csi.SGR(.{54});
107 pub const ResetOverlined = csi.SGR(.{55});
108 };
109
110 pub const color = struct {
111 pub const Color = enum(u8) {
112 Black,
113 Red,
114 Green,
115 Yellow,
116 Blue,
117 Magenta,
118 Cyan,
119 White,
120 };
121
122 pub fn Fg(s: Color, comptime m: []const u8) []const u8 {
123 return csi.SGR(.{30+@enumToInt(s)}) ++ m ++ style.ResetFgColor;
124 }
125
126 pub fn Bg(s: Color, comptime m: []const u8) []const u8 {
127 return csi.SGR(.{40+@enumToInt(s)}) ++ m ++ style.ResetBgColor;
128 }
129 };
130};