diff --git a/src/main.zig b/src/main.zig index 0cab76d..da1e36e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,8 @@ const std = @import("std"); const builtin = @import("builtin"); const detection = @import("detection.zig").os_module; const ascii = @import("ascii.zig"); +const config = @import("config.zig"); +const formatters = @import("formatters.zig"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -17,19 +19,22 @@ pub fn main() !void { } } - var buf1: [1024]u8 = undefined; - var buf2: [1024]u8 = undefined; + const conf = try config.readConfigFile(allocator); + defer if (conf) |c| c.deinit(); + + const modules_types = try config.getModulesTypes(allocator, conf); + defer modules_types.deinit(); const username = try detection.user.getUsername(allocator); const hostname = try detection.system.getHostname(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}{s}{s}@{s}{s}{s}", .{ + try sys_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{ ascii.Yellow, username, ascii.Reset, ascii.Magenta, hostname, ascii.Reset, - }))); + })); allocator.free(hostname); allocator.free(username); @@ -37,82 +42,28 @@ pub fn main() !void { @memset(separtor_buffer, '-'); try sys_info_list.append(separtor_buffer); - const kernel_info = try detection.system.getKernelInfo(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Kernel:{s} {s}", .{ ascii.Yellow, ascii.Reset, try kernel_info.toStr(&buf2) }))); - allocator.free(kernel_info.kernel_name); - allocator.free(kernel_info.kernel_release); + if (modules_types.items.len == 0) { + inline for (0..formatters.formatters.len) |i| { + try sys_info_list.append(try formatters.default_formatters[i](allocator)); + } + } else if (conf) |c| { + for (modules_types.items, c.value.modules) |module_type, module| { + var buf: [32]u8 = undefined; + const rgb = try ascii.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 os_info = try detection.system.getOsInfo(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}OS:{s} {s}", .{ ascii.Yellow, ascii.Reset, os_info }))); - allocator.free(os_info); + if (module_type == config.ModuleType.net) { + var formatted_net_info_list = try formatters.getFormattedNetInfo(allocator, module.key, key_color); + defer formatted_net_info_list.deinit(); - const locale = try detection.system.getLocale(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Locale:{s} {s}", .{ ascii.Yellow, ascii.Reset, locale }))); - allocator.free(locale); + try sys_info_list.appendSlice(formatted_net_info_list.items); - const uptime = try detection.system.getSystemUptime(); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Uptime:{s} {s}", .{ ascii.Yellow, ascii.Reset, try uptime.toStr(&buf2) }))); + continue; + } - if (builtin.os.tag == .macos) { - const packages_info = try detection.packages.getPackagesInfo(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Packages:{s}{s}", .{ ascii.Yellow, ascii.Reset, packages_info }))); - allocator.free(packages_info); - } else if (builtin.os.tag == .linux) { - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Packages:{s} WIP", .{ ascii.Yellow, ascii.Reset }))); + try sys_info_list.append(try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color)); + } } - const shell = try detection.user.getShell(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Shell:{s} {s}", .{ ascii.Yellow, ascii.Reset, shell[0..(shell.len - 1)] }))); - allocator.free(shell); - - const cpu_info = try detection.hardware.getCpuInfo(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Cpu:{s} {s}", .{ ascii.Yellow, ascii.Reset, try cpu_info.toStr(&buf2) }))); - allocator.free(cpu_info.cpu_name); - - if (builtin.os.tag == .macos) { - const gpu_info = try detection.hardware.getGpuInfo(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Gpu:{s} {s}", .{ ascii.Yellow, ascii.Reset, try gpu_info.toStr(&buf2) }))); - allocator.free(gpu_info.gpu_name); - } else if (builtin.os.tag == .linux) { - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Gpu:{s} WIP", .{ ascii.Yellow, ascii.Reset }))); - } - - var ram_info = detection.hardware.RamInfo{ - .ram_size = 0.0, - .ram_usage = 0.0, - .ram_usage_percentage = 0, - }; - if (builtin.os.tag == .macos) { - ram_info = try detection.hardware.getRamInfo(); - } else if (builtin.os.tag == .linux) { - ram_info = try detection.hardware.getRamInfo(allocator); - } - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Ram:{s} {s}", .{ ascii.Yellow, ascii.Reset, try ram_info.toStr(&buf2) }))); - - 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| { - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Swap:{s} {s}", .{ ascii.Yellow, ascii.Reset, try s.toStr(&buf2) }))); - } else { - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Swap:{s} Disabled", .{ ascii.Yellow, ascii.Reset }))); - } - - const disk_info = try detection.hardware.getDiskSize("/"); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Disk{s} {s}", .{ ascii.Yellow, ascii.Reset, try disk_info.toStr(&buf2) }))); - - const terminal_name = try detection.user.getTerminalName(allocator); - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Terminal:{s} {s}", .{ ascii.Yellow, ascii.Reset, terminal_name }))); - allocator.free(terminal_name); - - const net_info_list = try detection.network.getNetInfo(allocator); - for (net_info_list.items) |n| { - try sys_info_list.append(try allocator.dupe(u8, try std.fmt.bufPrint(&buf1, "{s}Local IP {s}{s}", .{ ascii.Yellow, ascii.Reset, try n.toStr(&buf2) }))); - allocator.free(n.interface_name); - allocator.free(n.ipv4_addr); - } - net_info_list.deinit(); - - @memset(&buf1, 0); - @memset(&buf2, 0); - try ascii.printAscii(allocator, sys_info_list); }