-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcode_view.zig
More file actions
120 lines (103 loc) · 3.38 KB
/
Copy pathcode_view.zig
File metadata and controls
120 lines (103 loc) · 3.38 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! ZigZag Code View Example
//! Demonstrates syntax-highlighted code display.
const std = @import("std");
const zz = @import("zigzag");
const zig_source =
\\const std = @import("std");
\\
\\pub fn main(init: std.process.Init) !void {
\\ const allocator = init.gpa;
\\ var list = std.std.array_list.Managed(u32).init(allocator);
\\ defer list.deinit();
\\
\\ // Add some numbers
\\ for (0..10) |i| {
\\ try list.append(@intCast(i * 42));
\\ }
\\
\\ std.debug.print("Count: {d}\n", .{list.items.len});
\\}
;
const py_source =
\\import json
\\from pathlib import Path
\\
\\class DataProcessor:
\\ """Process data files."""
\\
\\ def __init__(self, path: str):
\\ self.path = Path(path)
\\ self.count = 0
\\
\\ def process(self) -> list:
\\ # Read and parse JSON data
\\ data = json.loads(self.path.read_text())
\\ return [x for x in data if x > 0]
;
const Model = struct {
lang_idx: u8,
pub const Msg = union(enum) {
key: zz.KeyEvent,
};
pub fn init(self: *Model, _: *zz.Context) zz.Cmd(Msg) {
self.* = .{ .lang_idx = 0 };
return .none;
}
pub fn update(self: *Model, msg: Msg, _: *zz.Context) zz.Cmd(Msg) {
switch (msg) {
.key => |k| switch (k.key) {
.char => |c| switch (c) {
'q' => return .quit,
'1' => self.lang_idx = 0,
'2' => self.lang_idx = 1,
else => {},
},
.escape => return .quit,
.tab => self.lang_idx = (self.lang_idx + 1) % 2,
else => {},
},
}
return .none;
}
pub fn view(self: *const Model, ctx: *const zz.Context) ![]const u8 {
const alloc = ctx.allocator;
var title_s = zz.Style{};
title_s = title_s.bold(true);
title_s = title_s.fg(zz.Color.cyan);
title_s = title_s.inline_style(true);
var cv = zz.components.code_view.CodeView{};
const lang_name: []const u8 = if (self.lang_idx == 0) "Zig" else "Python";
if (self.lang_idx == 0) {
cv.source = zig_source;
cv.language = .zig;
cv.highlight_line = 12;
} else {
cv.source = py_source;
cv.language = .python;
cv.highlight_line = 7;
}
var box_s = zz.Style{};
box_s = box_s.borderAll(zz.Border.rounded);
box_s = box_s.borderForeground(zz.Color.gray(10));
box_s = box_s.paddingAll(1);
const code_output = cv.view(alloc);
const boxed = try box_s.render(alloc, code_output);
var help_s = zz.Style{};
help_s = help_s.fg(zz.Color.gray(10));
help_s = help_s.inline_style(true);
const header = try std.fmt.allocPrint(alloc, "{s} [{s}]", .{
try title_s.render(alloc, "Code Viewer"),
lang_name,
});
return std.fmt.allocPrint(alloc, "{s}\n\n{s}\n\n{s}", .{
header,
boxed,
try help_s.render(alloc, "1: Zig 2: Python Tab: switch q: quit"),
});
}
};
pub fn main(init: std.process.Init) !void {
var program = zz.Program(Model).init(init.gpa, init.io, init.environ_map);
defer program.deinit();
try program.run();
}