forked from rockorager/libvaxis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextView.zig
More file actions
224 lines (188 loc) · 7.04 KB
/
TextView.zig
File metadata and controls
224 lines (188 loc) · 7.04 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const std = @import("std");
const vaxis = @import("../main.zig");
const uucode = @import("uucode");
const ScrollView = vaxis.widgets.ScrollView;
/// Simple grapheme representation to replace Graphemes.Grapheme
const Grapheme = struct {
len: u16,
offset: u32,
};
pub const BufferWriter = struct {
pub const Error = error{OutOfMemory};
pub const Writer = std.io.GenericWriter(@This(), Error, write);
allocator: std.mem.Allocator,
buffer: *Buffer,
pub fn write(self: @This(), bytes: []const u8) Error!usize {
try self.buffer.append(self.allocator, .{
.bytes = bytes,
});
return bytes.len;
}
pub fn writer(self: @This()) Writer {
return .{ .context = self };
}
};
pub const Buffer = struct {
const StyleList = std.ArrayList(vaxis.Style);
const StyleMap = std.HashMapUnmanaged(usize, usize, std.hash_map.AutoContext(usize), std.hash_map.default_max_load_percentage);
pub const Content = struct {
bytes: []const u8,
};
pub const Style = struct {
begin: usize,
end: usize,
style: vaxis.Style,
};
pub const Error = error{OutOfMemory};
grapheme: std.MultiArrayList(Grapheme) = .empty,
content: std.ArrayListUnmanaged(u8) = .empty,
style_list: StyleList = .empty,
style_map: StyleMap = .empty,
rows: usize = 0,
cols: usize = 0,
// used when appending to a buffer
last_cols: usize = 0,
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
self.style_map.deinit(allocator);
self.style_list.deinit(allocator);
self.grapheme.deinit(allocator);
self.content.deinit(allocator);
self.* = undefined;
}
/// Clears all buffer data.
pub fn clear(self: *@This(), allocator: std.mem.Allocator) void {
self.deinit(allocator);
self.* = .{};
}
/// Replaces contents of the buffer, all previous buffer data is lost.
pub fn update(self: *@This(), allocator: std.mem.Allocator, content: Content) Error!void {
self.clear(allocator);
errdefer self.clear(allocator);
try self.append(allocator, content);
}
/// Appends content to the buffer.
pub fn append(self: *@This(), allocator: std.mem.Allocator, content: Content) Error!void {
var cols: usize = self.last_cols;
var iter = uucode.grapheme.Iterator(uucode.utf8.Iterator).init(.init(content.bytes));
var grapheme_start: usize = 0;
var prev_break: bool = true;
while (iter.nextCodePoint()) |result| {
if (prev_break and !result.is_break) {
// Start of a new grapheme
const cp_len: usize = std.unicode.utf8CodepointSequenceLength(result.code_point) catch 1;
grapheme_start = iter.i - cp_len;
}
if (result.is_break) {
// End of a grapheme
const grapheme_end = iter.i;
const grapheme_len = grapheme_end - grapheme_start;
try self.grapheme.append(allocator, .{
.len = @intCast(grapheme_len),
.offset = @intCast(self.content.items.len + grapheme_start),
});
const cluster = content.bytes[grapheme_start..grapheme_end];
if (std.mem.eql(u8, cluster, "\n")) {
self.cols = @max(self.cols, cols);
cols = 0;
} else {
// Calculate width using gwidth
const w = vaxis.gwidth.gwidth(cluster, .unicode);
cols +|= w;
}
grapheme_start = grapheme_end;
}
prev_break = result.is_break;
}
// Flush the last grapheme if we ended mid-cluster
if (!prev_break and grapheme_start < content.bytes.len) {
const grapheme_len = content.bytes.len - grapheme_start;
try self.grapheme.append(allocator, .{
.len = @intCast(grapheme_len),
.offset = @intCast(self.content.items.len + grapheme_start),
});
const cluster = content.bytes[grapheme_start..];
if (!std.mem.eql(u8, cluster, "\n")) {
const w = vaxis.gwidth.gwidth(cluster, .unicode);
cols +|= w;
}
}
try self.content.appendSlice(allocator, content.bytes);
self.last_cols = cols;
self.cols = @max(self.cols, cols);
self.rows +|= std.mem.count(u8, content.bytes, "\n");
}
/// Clears all styling data.
pub fn clearStyle(self: *@This(), allocator: std.mem.Allocator) void {
self.style_list.deinit(allocator);
self.style_map.deinit(allocator);
}
/// Update style for range of the buffer contents.
pub fn updateStyle(self: *@This(), allocator: std.mem.Allocator, style: Style) Error!void {
const style_index = blk: {
for (self.style_list.items, 0..) |s, i| {
if (std.meta.eql(s, style.style)) {
break :blk i;
}
}
try self.style_list.append(allocator, style.style);
break :blk self.style_list.items.len - 1;
};
for (style.begin..style.end) |i| {
try self.style_map.put(allocator, i, style_index);
}
}
pub fn writer(
self: *@This(),
allocator: std.mem.Allocator,
) BufferWriter.Writer {
return .{
.context = .{
.allocator = allocator,
.buffer = self,
},
};
}
};
scroll_view: ScrollView = .{},
pub fn input(self: *@This(), key: vaxis.Key) void {
self.scroll_view.input(key);
}
pub fn draw(self: *@This(), win: vaxis.Window, buffer: Buffer) void {
self.scroll_view.draw(win, .{ .cols = buffer.cols, .rows = buffer.rows });
const Pos = struct { x: usize = 0, y: usize = 0 };
var pos: Pos = .{};
var byte_index: usize = 0;
const bounds = self.scroll_view.bounds(win);
for (buffer.grapheme.items(.len), buffer.grapheme.items(.offset), 0..) |g_len, g_offset, index| {
if (bounds.above(pos.y)) {
break;
}
const cluster = buffer.content.items[g_offset..][0..g_len];
defer byte_index += cluster.len;
if (std.mem.eql(u8, cluster, "\n")) {
if (index == buffer.grapheme.len - 1) {
break;
}
pos.y +|= 1;
pos.x = 0;
continue;
} else if (bounds.below(pos.y)) {
continue;
}
const width = win.gwidth(cluster);
defer pos.x +|= width;
if (!bounds.colInside(pos.x)) {
continue;
}
const style: vaxis.Style = blk: {
if (buffer.style_map.get(byte_index)) |style_index| {
break :blk buffer.style_list.items[style_index];
}
break :blk .{};
};
self.scroll_view.writeCell(win, pos.x, pos.y, .{
.char = .{ .grapheme = cluster, .width = @intCast(width) },
.style = style,
});
}
}