tony / oxwm

Branch: 5660dcb
Switch branches/tags
master
emzywastaken remove explicit tag for `Block.Data` union ed07523 2026-02-15
152 lines · 4.6 KB Raw Blame History
1 const std = @import("std");
2
3 pub const Static = @import("static.zig").Static;
4 pub const Date_Time = @import("datetime.zig").Date_Time;
5 pub const Ram = @import("ram.zig").Ram;
6 pub const Shell = @import("shell.zig").Shell;
7 pub const Battery = @import("battery.zig").Battery;
8 pub const Cpu_Temp = @import("cpu_temp.zig").Cpu_Temp;
9
10 pub const Block = struct {
11 data: Data,
12 last_update: i64,
13 cached_content: [256]u8,
14 cached_len: usize,
15 underline: bool,
16
17 pub const Data = union(enum) {
18 static: Static,
19 datetime: Date_Time,
20 ram: Ram,
21 shell: Shell,
22 battery: Battery,
23 cpu_temp: Cpu_Temp,
24 };
25
26 pub fn init_static(text: []const u8, col: c_ulong, ul: bool) Block {
27 var block = Block{
28 .data = .{ .static = Static.init(text, col) },
29 .last_update = 0,
30 .cached_content = undefined,
31 .cached_len = 0,
32 .underline = ul,
33 };
34 @memcpy(block.cached_content[0..text.len], text);
35 block.cached_len = text.len;
36 return block;
37 }
38
39 pub fn init_datetime(format: []const u8, datetime_format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
40 return .{
41 .data = .{ .datetime = Date_Time.init(format, datetime_format, interval_secs, col) },
42 .last_update = 0,
43 .cached_content = undefined,
44 .cached_len = 0,
45 .underline = ul,
46 };
47 }
48
49 pub fn init_ram(format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
50 return .{
51 .data = .{ .ram = Ram.init(format, interval_secs, col) },
52 .last_update = 0,
53 .cached_content = undefined,
54 .cached_len = 0,
55 .underline = ul,
56 };
57 }
58
59 pub fn init_shell(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
60 return .{
61 .data = .{ .shell = Shell.init(format, command, interval_secs, col) },
62 .last_update = 0,
63 .cached_content = undefined,
64 .cached_len = 0,
65 .underline = ul,
66 };
67 }
68
69 pub fn init_battery(
70 format_charging: []const u8,
71 format_discharging: []const u8,
72 format_full: []const u8,
73 battery_name: []const u8,
74 interval_secs: u64,
75 col: c_ulong,
76 ul: bool,
77 ) Block {
78 return .{
79 .data = .{ .battery = Battery.init(format_charging, format_discharging, format_full, battery_name, interval_secs, col) },
80 .last_update = 0,
81 .cached_content = undefined,
82 .cached_len = 0,
83 .underline = ul,
84 };
85 }
86
87 pub fn init_cpu_temp(
88 format: []const u8,
89 thermal_zone: []const u8,
90 interval_secs: u64,
91 col: c_ulong,
92 ul: bool,
93 ) Block {
94 return .{
95 .data = .{ .cpu_temp = Cpu_Temp.init(format, thermal_zone, interval_secs, col) },
96 .last_update = 0,
97 .cached_content = undefined,
98 .cached_len = 0,
99 .underline = ul,
100 };
101 }
102
103 pub fn update(self: *Block) bool {
104 const interval_secs = self.interval();
105 if (interval_secs == 0) return false;
106
107 const now = std.time.timestamp();
108 if (now - self.last_update < @as(i64, @intCast(interval_secs))) {
109 return false;
110 }
111
112 self.last_update = now;
113
114 const result = switch (self.data) {
115 .static => |*s| s.content(&self.cached_content),
116 .datetime => |*d| d.content(&self.cached_content),
117 .ram => |*r| r.content(&self.cached_content),
118 .shell => |*s| s.content(&self.cached_content),
119 .battery => |*b| b.content(&self.cached_content),
120 .cpu_temp => |*c| c.content(&self.cached_content),
121 };
122
123 self.cached_len = result.len;
124 return true;
125 }
126
127 pub fn interval(self: *Block) u64 {
128 return switch (self.data) {
129 .static => |*s| s.interval(),
130 .datetime => |*d| d.interval(),
131 .ram => |*r| r.interval(),
132 .shell => |*s| s.interval(),
133 .battery => |*b| b.interval(),
134 .cpu_temp => |*c| c.interval(),
135 };
136 }
137
138 pub fn color(self: *const Block) c_ulong {
139 return switch (self.data) {
140 .static => |s| s.color,
141 .datetime => |d| d.color,
142 .ram => |r| r.color,
143 .shell => |s| s.color,
144 .battery => |b| b.color,
145 .cpu_temp => |c| c.color,
146 };
147 }
148
149 pub fn get_content(self: *const Block) []const u8 {
150 return self.cached_content[0..self.cached_len];
151 }
152 };