forked from sam701/zig-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrinter.zig
More file actions
47 lines (36 loc) · 1.14 KB
/
Printer.zig
File metadata and controls
47 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const std = @import("std");
const command = @import("./command.zig");
const Self = @This();
writer: *std.Io.Writer,
use_color: bool,
const color_clear = "0";
pub fn init(writer: *std.Io.Writer, use_color: bool) Self {
return .{
.writer = writer,
.use_color = use_color,
};
}
pub fn flush(self: *Self) void {
self.writer.flush() catch unreachable;
}
pub inline fn write(self: *Self, text: []const u8) void {
_ = self.writer.writeAll(text) catch unreachable;
}
pub inline fn printNewLine(self: *Self) void {
self.writer.writeByte('\n') catch unreachable;
}
pub inline fn format(self: *Self, comptime fmt: []const u8, args: anytype) void {
self.writer.print(fmt, args) catch unreachable;
}
pub inline fn printColor(self: *Self, color: []const u8) void {
if (self.use_color)
self.format("{c}[{s}m", .{ 0x1b, color });
}
pub inline fn printInColor(self: *Self, color: []const u8, text: []const u8) void {
self.printColor(color);
self.write(text);
self.printColor(color_clear);
}
pub inline fn printSpaces(self: *Self, cnt: usize) void {
self.writer.splatByteAll(' ', cnt) catch unreachable;
}