const std = @import("std");
const c = @cImport({ @cInclude("stdlib.h"); });
const module = @import("../module.zig");

const gpa = std.heap.page_allocator;

fn cb() ![]const u8 {
    const load_nr = 3;
    const loads: []f64 = try gpa.alloc(f64, load_nr);
    _ = c.getloadavg(loads.ptr, load_nr);

    var list = try std.ArrayList(u8).initCapacity(gpa, 4);
    defer list.deinit(gpa);
    var writer = list.writer(gpa);

    var i: u8 = 0;
    while (i < load_nr) : (i += 1) {
        try writer.print("{d:.2}{s}", .{loads[i], if (i != load_nr - 1) ", " else ""});
    }

    return list.toOwnedSlice(gpa);
}

pub const load = module {
    .name = "load",
    .prefix = "Load: ",
    .callback = cb
};
