| 1 | const std = @import("std"); |
| 2 | const config_mod = @import("../../config/config.zig"); |
| 3 | |
| 4 | pub const Static = @import("static.zig").Static; |
| 5 | pub const DateTime = @import("datetime.zig").DateTime; |
| 6 | pub const Ram = @import("ram.zig").Ram; |
| 7 | pub const Shell = @import("shell.zig").Shell; |
| 8 | pub const Battery = @import("battery.zig").Battery; |
| 9 | pub const CpuTemp = @import("cpu_temp.zig").CpuTemp; |
| 10 | |
| 11 | pub const Block = struct { |
| 12 | data: Data, |
| 13 | last_update: i64, |
| 14 | cached_content: [256]u8, |
| 15 | cached_len: usize, |
| 16 | underline: bool, |
| 17 | click: ?config_mod.ClickAction = null, |
| 18 | x_start: i32 = 0, |
| 19 | x_end: i32 = 0, |
| 20 | |
| 21 | pub const Data = union(enum) { |
| 22 | static: Static, |
| 23 | datetime: DateTime, |
| 24 | ram: Ram, |
| 25 | shell: Shell, |
| 26 | battery: Battery, |
| 27 | cpu_temp: CpuTemp, |
| 28 | }; |
| 29 | |
| 30 | pub fn initStatic(text: []const u8, col: c_ulong, ul: bool) Block { |
| 31 | var block = Block{ |
| 32 | .data = .{ .static = Static.init(text, col) }, |
| 33 | .last_update = 0, |
| 34 | .cached_content = undefined, |
| 35 | .cached_len = 0, |
| 36 | .underline = ul, |
| 37 | }; |
| 38 | @memcpy(block.cached_content[0..text.len], text); |
| 39 | block.cached_len = text.len; |
| 40 | return block; |
| 41 | } |
| 42 | |
| 43 | pub fn initDatetime(format: []const u8, datetime_format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block { |
| 44 | return .{ |
| 45 | .data = .{ .datetime = DateTime.init(format, datetime_format, interval_secs, col) }, |
| 46 | .last_update = 0, |
| 47 | .cached_content = undefined, |
| 48 | .cached_len = 0, |
| 49 | .underline = ul, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | pub fn initRam(format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block { |
| 54 | return .{ |
| 55 | .data = .{ .ram = Ram.init(format, interval_secs, col) }, |
| 56 | .last_update = 0, |
| 57 | .cached_content = undefined, |
| 58 | .cached_len = 0, |
| 59 | .underline = ul, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | pub fn initShell(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block { |
| 64 | return .{ |
| 65 | .data = .{ .shell = Shell.init(format, command, interval_secs, col) }, |
| 66 | .last_update = 0, |
| 67 | .cached_content = undefined, |
| 68 | .cached_len = 0, |
| 69 | .underline = ul, |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | pub fn initBattery( |
| 74 | format_charging: []const u8, |
| 75 | format_discharging: []const u8, |
| 76 | format_full: []const u8, |
| 77 | battery_name: []const u8, |
| 78 | interval_secs: u64, |
| 79 | col: c_ulong, |
| 80 | ul: bool, |
| 81 | ) Block { |
| 82 | return .{ |
| 83 | .data = .{ .battery = Battery.init(format_charging, format_discharging, format_full, battery_name, interval_secs, col) }, |
| 84 | .last_update = 0, |
| 85 | .cached_content = undefined, |
| 86 | .cached_len = 0, |
| 87 | .underline = ul, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | pub fn initCpuTemp( |
| 92 | format: []const u8, |
| 93 | thermal_zone: []const u8, |
| 94 | interval_secs: u64, |
| 95 | col: c_ulong, |
| 96 | ul: bool, |
| 97 | ) Block { |
| 98 | return .{ |
| 99 | .data = .{ .cpu_temp = CpuTemp.init(format, thermal_zone, interval_secs, col) }, |
| 100 | .last_update = 0, |
| 101 | .cached_content = undefined, |
| 102 | .cached_len = 0, |
| 103 | .underline = ul, |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | pub fn update(self: *Block, io: std.Io, gpa: std.mem.Allocator) bool { |
| 108 | const interval_secs = self.interval(); |
| 109 | if (interval_secs == 0) return false; |
| 110 | |
| 111 | const now = std.Io.Timestamp.now(io, .real).toSeconds(); |
| 112 | if (now - self.last_update < @as(i64, @intCast(interval_secs))) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | self.last_update = now; |
| 117 | |
| 118 | const result = switch (self.data) { |
| 119 | .static => |*s| s.content(&self.cached_content), |
| 120 | .datetime => |*d| d.content(&self.cached_content), |
| 121 | .ram => |*r| r.content(io, &self.cached_content), |
| 122 | .shell => |*s| s.content(io, gpa, &self.cached_content), |
| 123 | .battery => |*b| b.content(io, &self.cached_content), |
| 124 | .cpu_temp => |*c| c.content(io, &self.cached_content), |
| 125 | }; |
| 126 | |
| 127 | self.cached_len = result.len; |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | pub fn interval(self: *Block) u64 { |
| 132 | return switch (self.data) { |
| 133 | .static => |*s| s.interval(), |
| 134 | .datetime => |*d| d.interval(), |
| 135 | .ram => |*r| r.interval(), |
| 136 | .shell => |*s| s.interval(), |
| 137 | .battery => |*b| b.interval(), |
| 138 | .cpu_temp => |*c| c.interval(), |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | pub fn color(self: *const Block) c_ulong { |
| 143 | return switch (self.data) { |
| 144 | .static => |s| s.color, |
| 145 | .datetime => |d| d.color, |
| 146 | .ram => |r| r.color, |
| 147 | .shell => |s| s.color, |
| 148 | .battery => |b| b.color, |
| 149 | .cpu_temp => |c| c.color, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | pub fn getContent(self: *const Block) []const u8 { |
| 154 | return self.cached_content[0..self.cached_len]; |
| 155 | } |
| 156 | }; |