tony / oxwm

Branch: 19de790
Switch branches/tags
master
tonybanters updates for zig v16 c381491 2026-05-14
44 lines · 1.3 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, io: std.Io, gpa: std.mem.Allocator, buffer: []u8) []const u8 {
20 var cmd_output: [256]u8 = undefined;
21 const result = std.process.run(gpa, io, .{
22 .argv = &.{ "/bin/sh", "-c", self.command },
23 }) catch return buffer[0..0];
24 defer gpa.free(result.stdout);
25 defer gpa.free(result.stderr);
26
27 var cmd_len = @min(result.stdout.len, cmd_output.len);
28 @memcpy(cmd_output[0..cmd_len], result.stdout[0..cmd_len]);
29
30 while (cmd_len > 0 and (cmd_output[cmd_len - 1] == '\n' or cmd_output[cmd_len - 1] == '\r')) {
31 cmd_len -= 1;
32 }
33
34 return format_util.substitute(self.format, cmd_output[0..cmd_len], buffer);
35 }
36
37 pub fn interval(self: *Shell) u64 {
38 return self.interval_secs;
39 }
40
41 pub fn getColor(self: *Shell) c_ulong {
42 return self.color;
43 }
44 };