authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-12-09 11:23:46 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-12-09 11:23:46 -08:00
log8027e88a66c608e39d9018e586043d3b37411e29
tree3c87ec811fed5978ebf50df41ca0a3970b01712b
parentd61c8a8ea976d4a7056a39738ce75ac57d5e33cc

initial commit

extracted from zigmod code

6 files changed, 256 insertions(+), 0 deletions(-)

.gitignore created+2
...@@ -0,0 +1,2 @@
1/zig-cache
2/deps.zig
README.md created+29
...@@ -0,0 +1,29 @@
1# zig-ansi
2![loc](https://sloc.xyz/github/nektro/zig-ansi)
3[![license](https://img.shields.io/github/license/nektro/zig-ansi.svg)](https://github.com/nektro/zig-ansi/blob/master/LICENSE)
4[![discord](https://img.shields.io/discord/551971034593755159.svg?logo=discord)](https://discord.gg/P6Y4zQC)
5
6ANSI utilities for CLI usage in Zig.
7
8## Zig
9- https://ziglang.org/
10- https://github.com/ziglang/zig
11- https://github.com/ziglang/zig/wiki/Community
12
13## Getting Started
14Using https://github.com/nektro/zigmod, add a `git` type with a path of `https://github.com/nektro/zig-ansi`.
15
16## Usage
17See `src/main.zig` or do `zig build run` to see examples.
18
19See `src/lib.zig` for source code.
20
21## Built With
22- Zig 0.7.0
23
24## Contact
25- hello@nektro.net
26- https://twitter.com/nektro
27
28## License
29MIT
build.zig created+27
...@@ -0,0 +1,27 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 // Standard target options allows the person running `zig build` to choose
5 // what target to build for. Here we do not override the defaults, which
6 // means any target is allowed, and the default is native. Other options
7 // for restricting supported target set are available.
8 const target = b.standardTargetOptions(.{});
9
10 // Standard release options allow the person running `zig build` to select
11 // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
12 const mode = b.standardReleaseOptions();
13
14 const exe = b.addExecutable("zig-ansi", "src/main.zig");
15 exe.setTarget(target);
16 exe.setBuildMode(mode);
17 exe.install();
18
19 const run_cmd = exe.run();
20 run_cmd.step.dependOn(b.getInstallStep());
21 if (b.args) |args| {
22 run_cmd.addArgs(args);
23 }
24
25 const run_step = b.step("run", "Run the app");
26 run_step.dependOn(&run_cmd.step);
27}
src/lib.zig created+174
...@@ -0,0 +1,174 @@
1const std = @import("std");
2
3pub 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
42pub 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
55fn make_csi_sequence(comptime c: []const u8, x: anytype) []const u8 {
56 return escape.CSI ++ _join(";", arr_i_to_s(x)) ++ c;
57}
58
59fn 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
67pub 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
84pub 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
142pub 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
167fn _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}
src/main.zig created+21
...@@ -0,0 +1,21 @@
1const std = @import("std");
2
3const ansi = @import("./lib.zig");
4
5pub fn main() anyerror!void {
6 std.debug.warn(comptime ansi.color.Fg(.Red, "All your codebase are belong to us.\n"), .{});
7 std.debug.warn(comptime ansi.color.Fg(.Green, "All your codebase are belong to us.\n"), .{});
8 std.debug.warn(comptime ansi.color.Fg(.Yellow, "All your codebase are belong to us.\n"), .{});
9 std.debug.warn(comptime ansi.color.Fg(.Blue, "All your codebase are belong to us.\n"), .{});
10 std.debug.warn(comptime ansi.color.Fg(.Magenta, "All your codebase are belong to us.\n"), .{});
11 std.debug.warn(comptime ansi.color.Fg(.Cyan, "All your codebase are belong to us.\n"), .{});
12
13 std.debug.warn("\n", .{});
14
15 std.debug.warn(comptime ansi.color.Bg(.Red, "All your codebase are belong to us.\n"), .{});
16 std.debug.warn(comptime ansi.color.Bg(.Green, "All your codebase are belong to us.\n"), .{});
17 std.debug.warn(comptime ansi.color.Bg(.Yellow, "All your codebase are belong to us.\n"), .{});
18 std.debug.warn(comptime ansi.color.Bg(.Blue, "All your codebase are belong to us.\n"), .{});
19 std.debug.warn(comptime ansi.color.Bg(.Magenta, "All your codebase are belong to us.\n"), .{});
20 std.debug.warn(comptime ansi.color.Bg(.Cyan, "All your codebase are belong to us.\n"), .{});
21}
zig.mod created+3
...@@ -0,0 +1,3 @@
1id: s84v9o48ucb0xq0cmzq0cn433hgw0iaqztugja16h8bzxu3h
2name: ansi
3main: src/lib.zig