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

fn cb() ![]const u8 {
    var file = try std.fs.cwd().openFile("/proc/uptime", .{});
    defer file.close();

    var buf: [64]u8 = undefined;
    const bytes_read = try file.readAll(&buf);
    const data = buf[0..bytes_read];

    var tok = std.mem.tokenizeAny(u8, data, " ");
    const first_tok = tok.next() orelse return error.InvalidFormat;
    const uptime_seconds = try std.fmt.parseFloat(f64, first_tok);

    return try utils.secs_to_time(@intFromFloat(uptime_seconds));
}

pub const uptime = module {
    .name = "uptime",
    .prefix = "Uptime: ",
    .callback = cb
};
