commit 02eaeb22b7b2af1d7d66046ea6687a6e97d0d552
parent 41663622338bad44dd7fc062e30501dc01f00d95
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Sat, 20 Jan 2024 20:25:36 +0000
Add status bar
Step 95
Diffstat:
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -68,6 +68,8 @@ const EditorState = struct {
     rowoff: usize = 0,
     coloff: usize = 0,
     erow: []ERow = &[_]ERow{},
+    filename: ?[]const u8 = null,
+
     dbglog: std.ArrayList(u8),
 
     fn init(a: std.mem.Allocator) EditorState {
@@ -294,6 +296,7 @@ fn editorOpen(es: *EditorState, filename: []const u8) !void {
         errdefer es.a.free(line);
         try editorAppendRow(es, line);
     }
+    es.filename = filename;
 }
 
 //// Output
@@ -316,6 +319,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 editorDrawStatusBar(es, wtr);
     try std.fmt.format(wtr, "\x1b[{};{}H", .{
         es.cy - es.rowoff + 1,
         es.rx - es.coloff + 1,
@@ -352,11 +356,21 @@ fn editorDrawRows(es: *const EditorState, wtr: anytype) !void {
             }
             try wtr2.writeAll(row);
         }
+        try wtr.writeAll("\r\n");
+    }
+}
 
-        if (y < es.screenrows - 1) {
-            try wtr.writeAll("\r\n");
-        }
+// 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 fname = es.filename orelse "<no file>";
+    try std.fmt.format(wtr2, "{s} - {} lines", .{fname, es.erow.len});
+    for (0..es.screencols) |_| {
+        try wtr2.writeByte(' ');
     }
+    try wtr.writeAll("\x1b[m"); // normal colours
 }
 
 fn editorScroll(es: *EditorState) void {
@@ -460,7 +474,7 @@ fn editorMoveCursor(key: editorKey, es: *EditorState) void {
 
 fn initEditor(es: *EditorState) !void {
     const sz = try getWindowSize();
-    es.screenrows = sz.rows;
+    es.screenrows = sz.rows - 1; // Allow room for status bar
     es.screencols = sz.cols;
 }