kiloz

Following through https://viewsourcecode.org/snaptoken/kilo/index.html in Zig
git clone git://code.mfashby.net:/kiloz
Log | Files | Refs | README

commit 5702e585fb5ef422e82aecc0653aeaa2c21da572
parent 4940f97f8cc9ad33079dec85720f8eef0f2a2a0f
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Sat, 13 Jan 2024 20:01:17 +0000

Step 75

Diffstat:
Dsrc/c.zig | 0
Msrc/main.zig | 35++++++++++++++++++++++++-----------
2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/c.zig b/src/c.zig diff --git a/src/main.zig b/src/main.zig @@ -41,6 +41,7 @@ const EditorState = struct { cx: usize = 0, cy: usize = 0, rowoff: usize = 0, + coloff: usize = 0, erow: []ERow = &[_]ERow{}, fn init(a: std.mem.Allocator) EditorState { @@ -238,7 +239,7 @@ fn editorRefreshScreen(es: *EditorState) !void { //try wtr.writeAll("\x1b[2J"); // J clear 2 whole screen try wtr.writeAll("\x1b[H"); // Reset cursor (to 1:1) try editorDrawRows(es, wtr); - try std.fmt.format(wtr, "\x1b[{};{}H", .{ es.cy - es.rowoff + 1, es.cx + 1 }); // Move the cursor to our stored position + try std.fmt.format(wtr, "\x1b[{};{}H", .{ es.cy - es.rowoff + 1, es.cx - es.coloff + 1 }); // Move the cursor to our stored position try wtr.writeAll("\x1b[?25h"); // Show the cursor again try std.io.getStdOut().writeAll(es.screen_buf.items); } @@ -247,7 +248,7 @@ fn editorDrawRows(es: *const EditorState, wtr: anytype) !void { for (0..es.screenrows) |y| { try wtr.writeAll("\x1b[K"); // Clear row before writing it - var lw = limitedWriter(wtr, es.screencols); // Never write more than we have columns + var lw = truncateWriter(wtr, es.screencols); // Never write more than we have columns const wtr2 = lw.writer(); const filerow = y + es.rowoff; if (filerow >= es.erow.len) { @@ -263,7 +264,13 @@ fn editorDrawRows(es: *const EditorState, wtr: anytype) !void { try wtr2.writeAll("~"); } } else { - try wtr2.writeAll(es.erow[filerow]); + var row = es.erow[filerow]; + if (row.len < es.coloff) { + row = ""; + } else { + row = row[es.coloff..]; + } + try wtr2.writeAll(row); } if (y < es.screenrows-1) { @@ -279,6 +286,12 @@ fn editorScroll(es: *EditorState) void { if (es.cy >= es.rowoff + es.screenrows) { es.rowoff = es.cy - es.screenrows + 1; } + if (es.cx < es.coloff) { + es.coloff = es.cx; + } + if (es.cx >= es.coloff + es.screencols) { + es.coloff = es.cx - es.screencols + 1; + } } //// Input @@ -313,7 +326,7 @@ fn editorMoveCursor(key: editorKey, es: *EditorState) !void { if (es.cx > 0) es.cx -= 1; }, .ARROW_RIGHT => { - if (es.cx < (es.screencols - 1)) es.cx += 1; + es.cx += 1; }, .PAGE_UP => { es.cy = std.math.sub(usize, es.cy, es.screenrows) catch 0; @@ -341,14 +354,14 @@ fn initEditor(es: *EditorState) !void { //// Extra stuff -fn limitedWriter(base_writer: anytype, max: usize) LimitedWriter(@TypeOf(base_writer)) { - return LimitedWriter(@TypeOf(base_writer)){ +fn truncateWriter(base_writer: anytype, max: usize) TruncateWriter(@TypeOf(base_writer)) { + return TruncateWriter(@TypeOf(base_writer)){ .base_writer = base_writer, .max = max, }; } -fn LimitedWriter(comptime BaseWriter: type) type { +fn TruncateWriter(comptime BaseWriter: type) type { return struct { const Self = @This(); const WriteError = BaseWriter.Error || error{NoSpaceLeft}; @@ -360,14 +373,14 @@ fn LimitedWriter(comptime BaseWriter: type) type { fn write(self: *Self, bytes: []const u8) WriteError!usize { const rem = self.max - self.written; - if (rem == 0) return error.NoSpaceLeft; - + if (rem == 0) return bytes.len; var to_write = bytes; if (bytes.len > rem) { to_write = bytes[0..rem]; } - self.written += to_write.len; - return try self.base_writer.write(to_write); + const written = try self.base_writer.write(to_write); + self.written += written; + return written; } fn writer(self: *Self) Writer {