Repository navigation
Expand file tree
/
Copy pathmain.zig
More file actions
51 lines (43 loc) · 1.76 KB
/
Copy pathmain.zig
File metadata and controls
51 lines (43 loc) · 1.76 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
48
49
50
51
const std = @import("std");
const chroma = @import("chroma");
const ui = chroma.Formatter(@import("chroma.zon"));
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout_file = std.Io.File.stdout();
const use_color = try chroma.terminal.detect(
io,
init.minimal.environ,
stdout_file,
.auto,
);
var output_buffer: [2048]u8 = undefined;
var file_writer = stdout_file.writer(io, &output_buffer);
const writer = &file_writer.interface;
try printExample(writer, use_color);
try writer.flush();
}
fn printExample(writer: *std.Io.Writer, use_color: bool) !void {
const heading = comptime ui.render(
"{@brand}CHROMA 0.2{@reset} comptime-first terminal styling\n" ++
"{@muted}Configured by examples/chroma.zon{@reset}\n\n",
);
try writeRendered(writer, heading, use_color);
const semantic_styles = comptime ui.render(
" {@success}✓ success{@reset} Build completed\n" ++
" {@warning}⚠ warning{@reset} Configuration changed\n" ++
" {@failure}✗ failure{@reset} Connection refused\n\n",
);
try writeRendered(writer, semantic_styles, use_color);
const custom_grammar = comptime ui.render(
" {@label}custom grammar{@reset} {@fg=255/120/50|bold}{s}{@reset}\n" ++
" {@label}runtime policy{@reset} {s}\n",
);
if (use_color) {
try writer.print(custom_grammar.ansi, .{ "{@fg=R/G/B|effect}", "ANSI selected" });
} else {
try writer.print(custom_grammar.plain, .{ "{@fg=R/G/B|effect}", "plain text selected" });
}
}
fn writeRendered(writer: *std.Io.Writer, comptime rendered: chroma.Rendered, use_color: bool) !void {
try writer.writeAll(rendered.select(use_color));
}