const std = @import("std");
const Module = @import("../module.zig");
const Self = @This();

module: Module,

pub fn init(allocator: std.mem.Allocator) !Self {
    _ = allocator;
    return .{
        .module = .{ .getBlockFn = getBlock },
    };
}

pub fn getBlock(module: *Module, allocator: std.mem.Allocator) !Module.Block {
    _ = module;
    // const self: *const Self = @fieldParentPtr("module", module);

    const child = try std.process.Child.run(.{
        .argv = &[_][]const u8{ "amixer", "sget", "Master" },
        .allocator = allocator,
    });

    if (std.mem.indexOfScalar(u8, child.stdout, '[')) |volume_idx| {
        if (std.mem.indexOfScalarPos(u8, child.stdout, volume_idx, '%')) |percent_idx| {
            const volume = try std.fmt.parseInt(u8, child.stdout[volume_idx + 1 .. percent_idx], 10);
            return .{
                .full_text = try std.fmt.allocPrint(allocator, "{d: >2}%", .{volume}),
            };
        } else return .{ .full_text = "?" };
    } else return .{ .full_text = "?" };
}
