kiloz

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

commit 89e4e22d67111a3943171460d2b8d24ecac789a9
parent 02eaeb22b7b2af1d7d66046ea6687a6e97d0d552
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Sat, 20 Jan 2024 20:41:29 +0000

Add line number to status bar

Diffstat:
Msrc/main.zig | 19++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/main.zig b/src/main.zig @@ -363,13 +363,22 @@ fn editorDrawRows(es: *const EditorState, wtr: anytype) !void { // wtr should be std.io.writer fn editorDrawStatusBar(es: *const EditorState, wtr: anytype) !void { try wtr.writeAll("\x1b[7m"); // invert colours - var lw = truncateWriter(wtr, es.screencols); // Never write more than we have columns - const wtr2 = lw.writer(); + const buf = try es.a.alloc(u8, es.screencols); + defer es.a.free(buf); + @memset(buf, ' '); const fname = es.filename orelse "<no file>"; - try std.fmt.format(wtr2, "{s} - {} lines", .{fname, es.erow.len}); - for (0..es.screencols) |_| { - try wtr2.writeByte(' '); + _ = std.fmt.bufPrint(buf, "{s} - {} lines", .{fname, es.erow.len}) catch |e| switch (e) { + error.NoSpaceLeft => {}, + else => return e, + }; + const sz = std.fmt.count("{}/{}", .{es.cy+1,es.erow.len}); + if (buf.len >= sz) { + _ = std.fmt.bufPrint(buf[buf.len-sz..], "{}/{}", .{es.cy+1,es.erow.len}) catch |e| switch (e) { + error.NoSpaceLeft => {}, + else => return e, + }; } + try wtr.writeAll(buf); try wtr.writeAll("\x1b[m"); // normal colours }