From 94190aa1a7c622194b88e0d1f94ce3af8463681b Mon Sep 17 00:00:00 2001 From: Jeeves Date: Sat, 16 Mar 2024 15:51:52 -0600 Subject: [PATCH] now with color! --- src/main.zig | 78 +++++++++++++++++++++++++++++++++++++++-- src/modules/loadavg.zig | 1 + 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 1ff61b7..1a242d1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -35,9 +35,21 @@ pub fn main() !void { while (true) { try stdout.print("[", .{}); - for (modules) |module| { - try json.stringify(try module.getJson(), .{ .emit_null_optional_fields = false }, stdout); + var bg = Color{ .r = 0.0, .g = 0.0, .b = 0.0 }; + for (modules, 0..) |module, idx| { + _ = idx; + var output = try module.getJson(); + 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 }); + if (output.color == null) output.color = try color.getString(arena.allocator()); + output.separator = false; + output.separator_block_width = 0; + 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; } try stdout.print("],\n", .{}); @@ -46,3 +58,65 @@ pub fn main() !void { std.time.sleep(1000_000_000); } } + +pub const Color = struct { + r: f32, + g: f32, + b: f32, + + pub fn init(string: []const u8) !Color { + var r = try std.fmt.parseInt(u8, string[1..2], 16); + var g = try std.fmt.parseInt(u8, string[3..4], 16); + var b = try std.fmt.parseInt(u8, string[5..6], 16); + return .{ + .r = @as(f32, @floatFromInt(r)) / 255, + .g = @as(f32, @floatFromInt(g)) / 255, + .b = @as(f32, @floatFromInt(b)) / 255, + }; + } + + pub fn getString(self: *const Color, allocator: std.mem.Allocator) ![]const u8 { + const r = @as(u8, @intFromFloat(self.r * 255)); + const g = @as(u8, @intFromFloat(self.g * 255)); + const b = @as(u8, @intFromFloat(self.b * 255)); + return try std.fmt.allocPrint(allocator, "#{s}{s}{s}", .{ + std.fmt.fmtSliceHexLower(&[_]u8{r}), + std.fmt.fmtSliceHexLower(&[_]u8{g}), + std.fmt.fmtSliceHexLower(&[_]u8{b}), + }); + } + + pub fn add(a: *const Color, b: Color) Color { + return .{ + .r = std.math.clamp(a.r + b.r, 0.0, 1.0), + .g = std.math.clamp(a.g + b.g, 0.0, 1.0), + .b = std.math.clamp(a.b + b.b, 0.0, 1.0), + }; + } + + pub fn mix(a: *const Color, b: Color, t: f32) Color { + return .{ + .r = std.math.lerp(a.r, b.r, t), + .g = std.math.lerp(a.g, b.g, t), + .b = std.math.lerp(a.b, b.b, t), + }; + } +}; + +pub fn getBgColor(idx: usize, override: ?[]const u8) []const u8 { + if (override) |o| return o; + return switch (idx % 2) { + 0 => "#002b36", + 1 => "#0e1b15", + else => "#002b36", + }; +} + +pub fn getFgColor(idx: usize, override: ?[]const u8) []const u8 { + if (override) |o| return o; + return switch (idx % 2) { + // 0 => "#93a1a1", + // 1 => "#d6c2d6", + else => "#", + }; +} diff --git a/src/modules/loadavg.zig b/src/modules/loadavg.zig index 0eb65b2..1cd5da0 100644 --- a/src/modules/loadavg.zig +++ b/src/modules/loadavg.zig @@ -25,5 +25,6 @@ pub fn getJson(module: *const Module) !Module.JSON { return .{ .full_text = try std.fmt.allocPrint(self.module.allocator, "{d:0>1.2}", .{loadavg}), + .color = if (loadavg > 1.95) "#ff0000" else null, }; }