oxwm

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