From 2858050579cda19df7d0397d8acf2f289d6f3821 Mon Sep 17 00:00:00 2001
From: Jeeves <guydoodlesdev@gmail.com>
Date: Mon, 18 Mar 2024 12:32:14 -0600
Subject: [PATCH] add basic error handling

---
 src/main.zig | 40 ++++++++++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/src/main.zig b/src/main.zig
index 64c95d9..ce12f02 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -12,6 +12,9 @@ const memory = @import("modules/memory.zig");
 const loadavg = @import("modules/loadavg.zig");
 const Module = @import("module.zig");
 
+const bg_left_color = Color{ .r = 0, .g = 0, .b = 0 };
+const bg_right_color = Color{ .r = 0.2, .g = 0.2, .b = 0.2 };
+
 pub fn main() !void {
     var arena = heap.ArenaAllocator.init(heap.page_allocator);
     defer arena.deinit();
@@ -34,11 +37,28 @@ pub fn main() !void {
     };
 
     while (true) {
+        // const start_time = std.time.milliTimestamp();
+
         try stdout.print("[", .{});
+        const modules_len = 1 / @as(f32, @floatFromInt(modules.len));
         var bg = Color{ .r = 0.0, .g = 0.0, .b = 0.0 };
         for (modules, 0..) |module, idx| {
-            _ = idx;
-            var output = try module.getJson();
+            // const module_start_time = std.time.nanoTimestamp();
+
+            var output = module.getJson() catch {
+                const output = Module.JSON{
+                    .full_text = " [error] ",
+                    .background = try bg.getString(arena.allocator()),
+                    .color = try bg.add(Color{ .r = 1, .g = 0.3, .b = 0.3 }).getString(arena.allocator()),
+                    .separator = false,
+                    .separator_block_width = 0,
+                };
+                try json.stringify(output, .{ .emit_null_optional_fields = false }, stdout);
+                try stdout.print(",", .{});
+                const progress = @as(f32, @floatFromInt(idx + 1)) * modules_len;
+                bg = bg_left_color.mix(bg_right_color, progress);
+                continue;
+            };
             output.full_text = try std.fmt.allocPrint(arena.allocator(), " {s} ", .{output.full_text});
             output.background = try bg.getString(arena.allocator());
             var color = bg.add(Color{ .r = 0.6, .g = 0.6, .b = 0.6 });
@@ -48,14 +68,21 @@ pub fn main() !void {
             try json.stringify(output, .{ .emit_null_optional_fields = false }, stdout);
             try stdout.print(",", .{});
 
-            bg.r += 0.025;
-            bg.g += 0.025;
-            bg.b += 0.025;
+            const progress = @as(f32, @floatFromInt(idx + 1)) * modules_len;
+            // std.debug.print("\n{d}\n", .{progress});
+            bg = bg_left_color.mix(bg_right_color, progress);
+
+            // const module_end_time = std.time.nanoTimestamp();
+            // std.debug.print("\nmodule {d}: finished in {d}ns", .{ idx, module_end_time - module_start_time });
         }
         try stdout.print("],\n", .{});
 
+        // std.debug.print("\n", .{});
         try bw.flush();
         _ = arena.reset(.retain_capacity);
+
+        // const end_time = std.time.milliTimestamp();
+        // std.debug.print("\nFinished in {d}ms", .{end_time - start_time});
         std.time.sleep(1000_000_000);
     }
 }
@@ -99,7 +126,8 @@ pub const Color = struct {
     pub fn mix(a: *const Color, b: Color, t: f32) Color {
         const va = a.getVector();
         const vb = b.getVector();
-        return Color.fromVector(std.math.lerp(va, vb, t));
+        const vt = VecRGB{ t, t, t };
+        return Color.fromVector(@mulAdd(VecRGB, vb - va, vt, va));
     }
 
     fn getVector(self: *const Color) VecRGB {