I'm working on a text editor. The base buffer implementation isn't very relevant here, but suffice it to say that it can supply ordered slices that together comprise any byte range in the logical document. It exposes this generic method for writing any sequence of bytes into some generic writer:
pub fn materializeRange(self: *TextBuffer, w: anytype, start: usize, len: usize) @TypeOf(w).Error!void {
// when a view should be written directly to a writer
if (len == 0) return;
var it = self.getSliceIter(start, len);
while (it.next()) |slice| try w.writeAll(slice);
}
Currently, my interface is implemented with sokol-zig and the actual text is rendered line-by-line using that library's debugtext binding. I need to implement a custom writer which
A) provides a writeAll (or something similar, I don't care what it's called since I control the callsite) method taking bytes
B) allows those writes to be accumulated into an internal buffer and flushed when the full line is written
So far after some iterating I've settled on this cludgy implementation, which works but has some hacks (like storing the pos by pointer, since the writer is passed as an immutable argument). I feel like this completely sidesteps the nice new rewrite, so I was wondering how I make this less horrible? Thanks!
(post too long so I'll put the implementation in a message beneath this)