diff --git a/src/formatters.zig b/src/formatters.zig index 0407ae9..3ccfa49 100644 --- a/src/formatters.zig +++ b/src/formatters.zig @@ -8,7 +8,13 @@ const Result = union(enum) { string_arraylist: std.array_list.Managed([]u8), }; -pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror!Result{ +pub const FormatterContext = struct { + gpa: std.mem.Allocator, + io: std.Io, + environ: std.process.Environ, +}; + +pub const formatters = [_]*const fn (fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) anyerror!Result{ &getFormattedOsInfo, &getFormattedKernelInfo, &getFormattedUptimeInfo, @@ -26,7 +32,7 @@ pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const &getFormattedCustom, }; -pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{ +pub const default_formatters = [_]*const fn (fmt_ctx: FormatterContext) anyerror!Result{ &getDefaultFormattedOsInfo, &getDefaultFormattedKernelInfo, &getDefaultFormattedUptimeInfo, @@ -54,90 +60,154 @@ pub fn getFormattedUsernameHostname(allocator: std.mem.Allocator, color: []const }); } -pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedKernelInfo(allocator, "Kernel", display.Yellow); +pub fn getDefaultFormattedKernelInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedKernelInfo(fmt_ctx, "Kernel", display.Yellow); } -pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedKernelInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const kernel_info = try detection.system.getKernelInfo(allocator); defer allocator.free(kernel_info.kernel_name); defer allocator.free(kernel_info.kernel_release); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{ key_color, key, display.Reset, kernel_info.kernel_name, kernel_info.kernel_release }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{ + key_color, + key, + display.Reset, + kernel_info.kernel_name, + kernel_info.kernel_release, + }) }; } -pub fn getDefaultFormattedOsInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedOsInfo(allocator, "OS", display.Yellow); +pub fn getDefaultFormattedOsInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedOsInfo(fmt_ctx, "OS", display.Yellow); } -pub fn getFormattedOsInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedOsInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const os_info = try detection.system.getOsInfo(allocator); defer allocator.free(os_info); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, os_info }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ + key_color, + key, + display.Reset, + os_info, + }) }; } -pub fn getDefaultFormattedLocaleInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedLocaleInfo(allocator, "Locale", display.Yellow); +pub fn getDefaultFormattedLocaleInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedLocaleInfo(fmt_ctx, "Locale", display.Yellow); } -pub fn getFormattedLocaleInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { - const locale = try detection.system.getLocale(allocator); +pub fn getFormattedLocaleInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const environ = fmt_ctx.environ; + + const locale = try detection.system.getLocale(allocator, environ); defer allocator.free(locale); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, locale }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ + key_color, + key, + display.Reset, + locale, + }) }; } -pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedUptimeInfo(allocator, "Uptime", display.Yellow); +pub fn getDefaultFormattedUptimeInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedUptimeInfo(fmt_ctx, "Uptime", display.Yellow); } -pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { - const uptime = try detection.system.getSystemUptime(); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes", .{ key_color, key, display.Reset, uptime.days, uptime.hours, uptime.minutes }) }; +pub fn getFormattedUptimeInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const io = fmt_ctx.io; + + const uptime = if (builtin.os.tag == .macos) try detection.system.getSystemUptime(io) else if (builtin.os.tag == .linux) try detection.system.getSystemUptime(); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes", .{ + key_color, + key, + display.Reset, + uptime.days, + uptime.hours, + uptime.minutes, + }) }; } -pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedPackagesInfo(allocator, "Packages", display.Yellow); +pub fn getDefaultFormattedPackagesInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedPackagesInfo(fmt_ctx, "Packages", display.Yellow); } -pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { - const packages_info = try detection.packages.getPackagesInfo(allocator); +pub fn getFormattedPackagesInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const io = fmt_ctx.io; + + const packages_info = try detection.packages.getPackagesInfo(allocator, io); defer allocator.free(packages_info); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, display.Reset, packages_info }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ + key_color, + key, + display.Reset, + packages_info, + }) }; } -pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedShellInfo(allocator, "Shell", display.Yellow); +pub fn getDefaultFormattedShellInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedShellInfo(fmt_ctx, "Shell", display.Yellow); } -pub fn getFormattedShellInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { - const shell = try detection.user.getShell(allocator); +pub fn getFormattedShellInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const io = fmt_ctx.io; + const environ = fmt_ctx.environ; + + const shell = try detection.user.getShell(allocator, io, environ); defer allocator.free(shell); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, shell[0..(shell.len - 1)] }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ + key_color, + key, + display.Reset, + shell[0..(shell.len - 1)], + }) }; } -pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedCpuInfo(allocator, "Cpu", display.Yellow); +pub fn getDefaultFormattedCpuInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedCpuInfo(fmt_ctx, "Cpu", display.Yellow); } -pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedCpuInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const cpu_info = try detection.hardware.getCpuInfo(allocator); defer allocator.free(cpu_info.cpu_name); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, display.Reset, cpu_info.cpu_name, cpu_info.cpu_cores, cpu_info.cpu_max_freq }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ + key_color, + key, + display.Reset, + cpu_info.cpu_name, + cpu_info.cpu_cores, + cpu_info.cpu_max_freq, + }) }; } -pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) !Result { - if (builtin.os.tag == .macos) { - return try getFormattedGpuInfo(allocator, "Gpu", display.Yellow); - } else if (builtin.os.tag == .linux) { - return try getFormattedGpuInfo(allocator, "Gpu", display.Yellow); - } +pub fn getDefaultFormattedGpuInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedGpuInfo(fmt_ctx, "Gpu", display.Yellow); } -pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedGpuInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + if (builtin.os.tag == .macos) { const gpu_info = try detection.hardware.getGpuInfo(allocator); defer allocator.free(gpu_info.gpu_name); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, display.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ + key_color, + key, + display.Reset, + gpu_info.gpu_name, + gpu_info.gpu_cores, + gpu_info.gpu_freq, + }) }; } else if (builtin.os.tag == .linux) { var formatted_gpu_info_list = std.array_list.Managed([]u8).init(allocator); @@ -146,9 +216,21 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co for (gpu_info_list.items) |g| { var formatted_gpu_info: []u8 = undefined; if ((g.gpu_cores == 0) or (g.gpu_freq == 0.0)) { - formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, g.gpu_name }); + formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ + key_color, + key, + display.Reset, + g.gpu_name, + }); } else { - formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, display.Reset, g.gpu_name, g.gpu_cores, g.gpu_freq }); + formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ + key_color, + key, + display.Reset, + g.gpu_name, + g.gpu_cores, + g.gpu_freq, + }); } try formatted_gpu_info_list.append(formatted_gpu_info); allocator.free(g.gpu_name); @@ -159,67 +241,108 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co } } -pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedRamInfo(allocator, "Ram", display.Yellow); +pub fn getDefaultFormattedRamInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedRamInfo(fmt_ctx, "Ram", display.Yellow); } -pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedRamInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const ram_info = if (builtin.os.tag == .macos) try detection.hardware.getRamInfo() else if (builtin.os.tag == .linux) try detection.hardware.getRamInfo(allocator); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, display.Reset, ram_info.ram_usage, ram_info.ram_size, ram_info.ram_usage_percentage }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ + key_color, + key, + display.Reset, + ram_info.ram_usage, + ram_info.ram_size, + ram_info.ram_usage_percentage, + }) }; } -pub fn getDefaultFormattedSwapInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedSwapInfo(allocator, "Swap", display.Yellow); +pub fn getDefaultFormattedSwapInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedSwapInfo(fmt_ctx, "Swap", display.Yellow); } -pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedSwapInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const swap_info = if (builtin.os.tag == .macos) try detection.hardware.getSwapInfo() else if (builtin.os.tag == .linux) try detection.hardware.getSwapInfo(allocator); if (swap_info) |s| { - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, display.Reset, s.swap_usage, s.swap_size, s.swap_usage_percentage }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ + key_color, + key, + display.Reset, + s.swap_usage, + s.swap_size, + s.swap_usage_percentage, + }) }; } else { return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, display.Reset }) }; } } -pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedDiskInfo(allocator, "Disk", display.Yellow); +pub fn getDefaultFormattedDiskInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedDiskInfo(fmt_ctx, "Disk", display.Yellow); } -pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedDiskInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const disk_info = try detection.hardware.getDiskSize("/"); - return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{ key_color, key, disk_info.disk_path, display.Reset, disk_info.disk_usage, disk_info.disk_size, disk_info.disk_usage_percentage }) }; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{ + key_color, + key, + disk_info.disk_path, + display.Reset, + disk_info.disk_usage, + disk_info.disk_size, + disk_info.disk_usage_percentage, + }) }; } -pub fn getDefaultFormattedWindowManagerInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedWindowManagerInfo(allocator, "WM", display.Yellow); +pub fn getDefaultFormattedWindowManagerInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedWindowManagerInfo(fmt_ctx, "WM", display.Yellow); } -pub fn getFormattedWindowManagerInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedWindowManagerInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const wm = try detection.system.getWindowManagerInfo(allocator); defer allocator.free(wm); return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, wm }) }; } -pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedTerminalNameInfo(allocator, "Terminal", display.Yellow); +pub fn getDefaultFormattedTerminalNameInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedTerminalNameInfo(fmt_ctx, "Terminal", display.Yellow); } -pub fn getFormattedTerminalNameInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { - const terminal_name = try detection.user.getTerminalName(allocator); +pub fn getFormattedTerminalNameInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + const environ = fmt_ctx.environ; + + const terminal_name = try detection.user.getTerminalName(allocator, environ); defer allocator.free(terminal_name); return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, terminal_name }) }; } -pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) !Result { - return try getFormattedNetInfo(allocator, "Local IP", display.Yellow); +pub fn getDefaultFormattedNetInfo(fmt_ctx: FormatterContext) !Result { + return try getFormattedNetInfo(fmt_ctx, "Local IP", display.Yellow); } -pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedNetInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + var formatted_net_info_list = std.array_list.Managed([]u8).init(allocator); var net_info_list = try detection.network.getNetInfo(allocator); for (net_info_list.items) |n| { - try formatted_net_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {s}", .{ key_color, key, n.interface_name, display.Reset, n.ipv4_addr })); + try formatted_net_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {s}", .{ + key_color, + key, + n.interface_name, + display.Reset, + n.ipv4_addr, + })); allocator.free(n.interface_name); allocator.free(n.ipv4_addr); } @@ -228,6 +351,8 @@ pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_co return Result{ .string_arraylist = formatted_net_info_list }; } -pub fn getFormattedCustom(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { +pub fn getFormattedCustom(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result { + const allocator = fmt_ctx.gpa; + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ key_color, key, display.Reset }) }; } diff --git a/src/main.zig b/src/main.zig index 62eeccd..6ea8631 100644 --- a/src/main.zig +++ b/src/main.zig @@ -45,9 +45,15 @@ pub fn main(init: std.process.Init) !void { @memset(separtor_buffer, '-'); try modules_list.append(separtor_buffer); + const fmt_ctx = formatters.FormatterContext{ + .gpa = allocator, + .environ = init.minimal.environ, + .io = init.io, + }; + if (modules_types.items.len == 0) { inline for (0..formatters.default_formatters.len) |i| { - const result = try formatters.default_formatters[i](allocator); + const result = try formatters.default_formatters[i](fmt_ctx); switch (result) { .string => |r| try modules_list.append(r), .string_arraylist => |r| { @@ -62,7 +68,7 @@ pub fn main(init: std.process.Init) !void { const rgb = try display.hexColorToRgb(module.key_color); const key_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b }); - const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color); + const result = try formatters.formatters[@intFromEnum(module_type)](fmt_ctx, module.key, key_color); switch (result) { .string => |r| try modules_list.append(r), .string_arraylist => |r| { @@ -73,7 +79,6 @@ pub fn main(init: std.process.Init) !void { } } - // TODO: rename ascii.zig in display.zig // TODO: return the formatted ascii and modules to print instead of directly print them try display.printAsciiAndModules(allocator, io, config.getAsciiPath(conf), modules_list); }