tony / oxwm

Branch: b0374a4
Switch branches/tags
master
emzywastaken treewide: change function names to camelCase b3ae92e 2026-02-23
45 lines · 1.4 KB Raw Blame History
1 const std = @import("std");
2 const format_util = @import("format.zig");
3
4 pub const Shell = struct {
5 format: []const u8,
6 command: []const u8,
7 interval_secs: u64,
8 color: c_ulong,
9
10 pub fn init(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong) Shell {
11 return .{
12 .format = format,
13 .command = command,
14 .interval_secs = interval_secs,
15 .color = col,
16 };
17 }
18
19 pub fn content(self: *Shell, buffer: []u8) []const u8 {
20 var cmd_output: [256]u8 = undefined;
21 const result = std.process.Child.run(.{
22 .allocator = std.heap.page_allocator,
23 .argv = &.{ "/bin/sh", "-c", self.command },
24 }) catch return buffer[0..0];
25 defer std.heap.page_allocator.free(result.stdout);
26 defer std.heap.page_allocator.free(result.stderr);
27
28 var cmd_len = @min(result.stdout.len, cmd_output.len);
29 @memcpy(cmd_output[0..cmd_len], result.stdout[0..cmd_len]);
30
31 while (cmd_len > 0 and (cmd_output[cmd_len - 1] == '\n' or cmd_output[cmd_len - 1] == '\r')) {
32 cmd_len -= 1;
33 }
34
35 return format_util.substitute(self.format, cmd_output[0..cmd_len], buffer);
36 }
37
38 pub fn interval(self: *Shell) u64 {
39 return self.interval_secs;
40 }
41
42 pub fn getColor(self: *Shell) c_ulong {
43 return self.color;
44 }
45 };