From 7993cab4dfe459d97333fb164e420f4c4bf1d3f1 Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 11 Jul 2025 14:16:35 +0200 Subject: [PATCH 01/10] feat(linux): add gpu name info --- src/linux/hardware.zig | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index 0db343c..75a247a 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -1,6 +1,7 @@ const std = @import("std"); const c_unistd = @cImport(@cInclude("unistd.h")); const c_statvfs = @cImport(@cInclude("sys/statvfs.h")); +const c_libpci = @cImport(@cInclude("pci/pci.h")); /// Struct representing CPU informations pub const CpuInfo = struct { @@ -9,6 +10,13 @@ pub const CpuInfo = struct { cpu_max_freq: f32, }; +/// Struct representing GPU informations +pub const GpuInfo = struct { + gpu_name: []u8, + gpu_cores: i32, + gpu_freq: f64, +}; + /// Struct representing RAM usage informations pub const RamInfo = struct { ram_size: f64, @@ -76,6 +84,45 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo { }; } +pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo { + var gpu_info = GpuInfo{ + .gpu_name = try allocator.dupe(u8, "Unknown"), + .gpu_cores = 0, + .gpu_freq = 0.0, + }; + + const display_controller = 0x03; + + const pacc = c_libpci.pci_alloc(); + defer c_libpci.pci_cleanup(pacc); + c_libpci.pci_init(pacc); + c_libpci.pci_scan_bus(pacc); + + var devices = pacc.*.devices; + while (devices != null) : (devices = devices.*.next) { + // TODO: handle return value + _ = c_libpci.pci_fill_info(devices, c_libpci.PCI_FILL_IDENT | c_libpci.PCI_FILL_CLASS); + + if ((devices.*.device_class >> 8) == display_controller) { + var name_buffer: [256]u8 = undefined; + + const name = c_libpci.pci_lookup_name( + pacc, + &name_buffer, + name_buffer.len, + c_libpci.PCI_LOOKUP_VENDOR | c_libpci.PCI_LOOKUP_DEVICE, + devices.*.vendor_id, + devices.*.device_id, + ); + + allocator.free(gpu_info.gpu_name); + gpu_info.gpu_name = try allocator.dupe(u8, std.mem.span(name)); + } + } + + return gpu_info; +} + pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo { // Reads /proc/meminfo const meminfo_path = "/proc/meminfo"; From 5faf60e53d31c6b5789d1b858d9eb332a16c9017 Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 11 Jul 2025 14:21:37 +0200 Subject: [PATCH 02/10] refactor(formatters): remove gpu information detection behavior based on os --- src/formatters.zig | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/formatters.zig b/src/formatters.zig index 2fbd487..e115384 100644 --- a/src/formatters.zig +++ b/src/formatters.zig @@ -113,13 +113,9 @@ pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) ![]u8 { } pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { - if (builtin.os.tag == .macos) { - const gpu_info = try detection.hardware.getGpuInfo(allocator); - defer allocator.free(gpu_info.gpu_name); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }); - } else if (builtin.os.tag == .linux) { - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset }); - } + const gpu_info = try detection.hardware.getGpuInfo(allocator); + defer allocator.free(gpu_info.gpu_name); + return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }); } pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) ![]u8 { From a52dd145daae297e26c5dbbdff0836ba35e06f23 Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 11 Jul 2025 14:22:25 +0200 Subject: [PATCH 03/10] build(linux): link with libpci --- build.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/build.zig b/build.zig index ab63d92..c39d595 100644 --- a/build.zig +++ b/build.zig @@ -29,6 +29,7 @@ pub fn build(b: *std.Build) void { if (target.result.os.tag == .linux) { exe.linkLibC(); + exe.linkSystemLibrary("pci"); } // This declares intent for the executable to be installed into the From 90abbcb1fdc40ee97e0136cdf26d455daf9698eb Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 11 Jul 2025 16:27:07 +0200 Subject: [PATCH 04/10] feat(linux-gpu): handle the return value of the pci_fill_info function --- src/linux/hardware.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index 75a247a..0e79a1d 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -100,8 +100,11 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo { var devices = pacc.*.devices; while (devices != null) : (devices = devices.*.next) { - // TODO: handle return value - _ = c_libpci.pci_fill_info(devices, c_libpci.PCI_FILL_IDENT | c_libpci.PCI_FILL_CLASS); + // NOTE: for references: https://github.com/pciutils/pciutils/blob/3ec74c71c01878f92e751f15bb8febe720c3ab40/lib/access.c#L194 + const known_fields = c_libpci.pci_fill_info(devices, c_libpci.PCI_FILL_IDENT | c_libpci.PCI_FILL_CLASS); + if (known_fields <= 0) { + return error.NoLibpciFieldsFound; + } if ((devices.*.device_class >> 8) == display_controller) { var name_buffer: [256]u8 = undefined; From c660fc903f682d49e51c1c8bfa6e3fd5cd90d1c7 Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 11 Jul 2025 21:54:17 +0200 Subject: [PATCH 05/10] feat(linux-gpu): handle multiple gpu --- src/linux/hardware.zig | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index 0e79a1d..f8f15b6 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -84,12 +84,8 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo { }; } -pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo { - var gpu_info = GpuInfo{ - .gpu_name = try allocator.dupe(u8, "Unknown"), - .gpu_cores = 0, - .gpu_freq = 0.0, - }; +pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) { + var gpu_info_list = std.ArrayList(GpuInfo).init(allocator); const display_controller = 0x03; @@ -118,12 +114,23 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo { devices.*.device_id, ); - allocator.free(gpu_info.gpu_name); - gpu_info.gpu_name = try allocator.dupe(u8, std.mem.span(name)); + gpu_info_list.append(GpuInfo{ + .gpu_name = try allocator.dupe(u8, std.mem.span(name)), + .gpu_cores = 0, + .gpu_freq = 0.0, + }); } } - return gpu_info; + if (gpu_info_list.items.len == 0) { + return GpuInfo{ + .gpu_name = undefined, + .gpu_cores = 0, + .gpu_freq = 0.0, + }; + } + + return gpu_info_list; } pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo { From 9af26702cceef637a372fedfe7ce3e92d77f4b86 Mon Sep 17 00:00:00 2001 From: utox39 Date: Sat, 12 Jul 2025 01:19:53 +0200 Subject: [PATCH 06/10] fix(linux-gpu): fix ignored error union --- src/linux/hardware.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index f8f15b6..0693545 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -114,7 +114,7 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) { devices.*.device_id, ); - gpu_info_list.append(GpuInfo{ + try gpu_info_list.append(GpuInfo{ .gpu_name = try allocator.dupe(u8, std.mem.span(name)), .gpu_cores = 0, .gpu_freq = 0.0, From 5b63b30a4fb1d35bc6590d5fbdf7ec7528b31d34 Mon Sep 17 00:00:00 2001 From: utox39 Date: Sat, 12 Jul 2025 01:23:33 +0200 Subject: [PATCH 07/10] fix(linux-gpu): fix wrong return type --- src/linux/hardware.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index 0693545..dc44579 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -123,11 +123,11 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) { } if (gpu_info_list.items.len == 0) { - return GpuInfo{ - .gpu_name = undefined, + try gpu_info_list.append(GpuInfo{ + .gpu_name = try allocator.dupe(u8, "Unknown"), .gpu_cores = 0, .gpu_freq = 0.0, - }; + }); } return gpu_info_list; From de214a4a89fec3f759d277e685e80dea26bac8f2 Mon Sep 17 00:00:00 2001 From: utox39 Date: Sat, 12 Jul 2025 01:42:15 +0200 Subject: [PATCH 08/10] feat(formatters): add a tagged union to handle formatters with different signatures --- src/formatters.zig | 148 ++++++++++++++++++++++++++------------------- src/main.zig | 27 +++++---- 2 files changed, 102 insertions(+), 73 deletions(-) diff --git a/src/formatters.zig b/src/formatters.zig index e115384..3cd902a 100644 --- a/src/formatters.zig +++ b/src/formatters.zig @@ -3,7 +3,12 @@ const std = @import("std"); const ascii = @import("ascii.zig"); const detection = @import("detection.zig").os_module; -pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror![]u8{ +const Result = union(enum) { + string: []u8, + string_arraylist: std.ArrayList([]u8), +}; + +pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror!Result{ &getFormattedOsInfo, &getFormattedKernelInfo, &getFormattedUptimeInfo, @@ -14,11 +19,12 @@ pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const &getFormattedRamInfo, &getFormattedSwapInfo, &getFormattedDiskInfo, + &getFormattedNetInfo, &getFormattedTerminalNameInfo, &getFormattedLocaleInfo, }; -pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror![]u8{ +pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{ &getDefaultFormattedOsInfo, &getDefaultFormattedKernelInfo, &getDefaultFormattedUptimeInfo, @@ -29,141 +35,159 @@ pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyer &getDefaultFormattedRamInfo, &getDefaultFormattedSwapInfo, &getDefaultFormattedDiskInfo, + &getDefaultFormattedNetInfo, &getDefaultFormattedTerminalNameInfo, &getDefaultFormattedLocaleInfo, }; -pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result { return try getFormattedKernelInfo(allocator, "Kernel", ascii.Yellow); } -pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const kernel_info = try detection.system.getKernelInfo(allocator); defer allocator.free(kernel_info.kernel_name); defer allocator.free(kernel_info.kernel_release); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{ key_color, key, ascii.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, ascii.Reset, kernel_info.kernel_name, kernel_info.kernel_release }) }; } -pub fn getDefaultFormattedOsInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedOsInfo(allocator: std.mem.Allocator) !Result { return try getFormattedOsInfo(allocator, "OS", ascii.Yellow); } -pub fn getFormattedOsInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedOsInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const os_info = try detection.system.getOsInfo(allocator); defer allocator.free(os_info); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, os_info }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, os_info }) }; } -pub fn getDefaultFormattedLocaleInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedLocaleInfo(allocator: std.mem.Allocator) !Result { return try getFormattedLocaleInfo(allocator, "Locale", ascii.Yellow); } -pub fn getFormattedLocaleInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedLocaleInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const locale = try detection.system.getLocale(allocator); defer allocator.free(locale); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, locale }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, locale }) }; } -pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) !Result { return try getFormattedUptimeInfo(allocator, "Uptime", ascii.Yellow); } -pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const uptime = try detection.system.getSystemUptime(); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes ", .{ key_color, key, ascii.Reset, uptime.days, uptime.hours, uptime.minutes }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes ", .{ key_color, key, ascii.Reset, uptime.days, uptime.hours, uptime.minutes }) }; } -pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result { return try getFormattedPackagesInfo(allocator, "Packages", ascii.Yellow); } -pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { if (builtin.os.tag == .macos) { const packages_info = try detection.packages.getPackagesInfo(allocator); defer allocator.free(packages_info); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, ascii.Reset, packages_info }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, ascii.Reset, packages_info }) }; } else if (builtin.os.tag == .linux) { - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset }) }; } } -pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) !Result { return try getFormattedShellInfo(allocator, "Shell", ascii.Yellow); } -pub fn getFormattedShellInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedShellInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const shell = try detection.user.getShell(allocator); defer allocator.free(shell); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, shell[0..(shell.len - 1)] }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, shell[0..(shell.len - 1)] }) }; } -pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) !Result { return try getFormattedCpuInfo(allocator, "Cpu", ascii.Yellow); } -pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const cpu_info = try detection.hardware.getCpuInfo(allocator); defer allocator.free(cpu_info.cpu_name); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.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, ascii.Reset, cpu_info.cpu_name, cpu_info.cpu_cores, cpu_info.cpu_max_freq }) }; } -pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) ![]u8 { - return try getFormattedGpuInfo(allocator, "Gpu", ascii.Yellow); -} - -pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { - const gpu_info = try detection.hardware.getGpuInfo(allocator); - defer allocator.free(gpu_info.gpu_name); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }); -} - -pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) ![]u8 { - return try getFormattedRamInfo(allocator, "Ram", ascii.Yellow); -} - -pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { - const ram_info = if (builtin.os.tag == .macos) try detection.hardware.getRamInfo() else if (builtin.os.tag == .linux) try detection.hardware.getRamInfo(allocator); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, ram_info.ram_usage, ram_info.ram_size, ram_info.ram_usage_percentage }); -} - -pub fn getDefaultFormattedSwapInfo(allocator: std.mem.Allocator) ![]u8 { - return try getFormattedSwapInfo(allocator, "Swap", ascii.Yellow); -} - -pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { - 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 try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, s.swap_usage, s.swap_size, s.swap_usage_percentage }); - } else { - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, ascii.Reset }); +pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) !Result { + if (builtin.os.tag == .macos) { + return try getFormattedGpuInfo(allocator, "Gpu", ascii.Yellow); + } else if (builtin.os.tag == .linux) { + return try getFormattedGpuInfo(allocator, "Gpu", ascii.Yellow); } } -pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { + var formatted_gpu_info = std.ArrayList([]u8).init(allocator); + + if (builtin.os.tag == .macos) { + const gpu_info = try detection.hardware.getGpuInfo(allocator); + defer allocator.free(gpu_info.gpu_name); + return try Result{ .string = std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }) }; + } else if (builtin.os.tag == .linux) { + const gpu_info_list = try detection.hardware.getGpuInfo(allocator); + for (gpu_info_list.items) |g| { + try formatted_gpu_info.append(try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, g.gpu_name, g.gpu_cores, g.gpu_freq })); + allocator.free(g.gpu_name); + } + gpu_info_list.deinit(); + + return Result{ .string_arraylist = formatted_gpu_info }; + } +} + +pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) !Result { + return try getFormattedRamInfo(allocator, "Ram", ascii.Yellow); +} + +pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { + 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, ascii.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", ascii.Yellow); +} + +pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { + 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, ascii.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, ascii.Reset }) }; + } +} + +pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) !Result { return try getFormattedDiskInfo(allocator, "Disk", ascii.Yellow); } -pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const disk_info = try detection.hardware.getDiskSize("/"); - return try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{ key_color, key, disk_info.disk_path, ascii.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, ascii.Reset, disk_info.disk_usage, disk_info.disk_size, disk_info.disk_usage_percentage }) }; } -pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) !Result { return try getFormattedTerminalNameInfo(allocator, "Terminal", ascii.Yellow); } -pub fn getFormattedTerminalNameInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 { +pub fn getFormattedTerminalNameInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { const terminal_name = try detection.user.getTerminalName(allocator); defer allocator.free(terminal_name); - return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, terminal_name }); + return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, terminal_name }) }; } -pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) ![]u8 { +pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) !Result { return try getFormattedNetInfo(allocator, "Local IP", ascii.Yellow); } -pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !std.ArrayList([]u8) { +pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result { var formatted_net_info_list = std.ArrayList([]u8).init(allocator); var net_info_list = try detection.network.getNetInfo(allocator); @@ -174,5 +198,5 @@ pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_co } net_info_list.deinit(); - return formatted_net_info_list; + return Result{ .string_arraylist = formatted_net_info_list }; } diff --git a/src/main.zig b/src/main.zig index 04d3c28..3ec50d0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -43,8 +43,15 @@ pub fn main() !void { try sys_info_list.append(separtor_buffer); if (modules_types.items.len == 0) { - inline for (0..formatters.formatters.len) |i| { - try sys_info_list.append(try formatters.default_formatters[i](allocator)); + inline for (0..formatters.default_formatters.len) |i| { + const result = try formatters.default_formatters[i](allocator); + switch (result) { + .string => |r| try sys_info_list.append(r), + .string_arraylist => |r| { + defer r.deinit(); + try sys_info_list.appendSlice(r.items); + }, + } } } else if (conf) |c| { for (modules_types.items, c.value.modules) |module_type, module| { @@ -52,16 +59,14 @@ pub fn main() !void { 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 }); - 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(); - - try sys_info_list.appendSlice(formatted_net_info_list.items); - - continue; + const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color); + switch (result) { + .string => |r| try sys_info_list.append(r), + .string_arraylist => |r| { + defer r.deinit(); + try sys_info_list.appendSlice(r.items); + }, } - - try sys_info_list.append(try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color)); } } From 707f94bd8b2a7bf2f9b15164a8038bdd93396154 Mon Sep 17 00:00:00 2001 From: utox39 Date: Sat, 12 Jul 2025 01:47:53 +0200 Subject: [PATCH 09/10] fix(formatters): fix wrong return type --- src/formatters.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formatters.zig b/src/formatters.zig index 3cd902a..336da1f 100644 --- a/src/formatters.zig +++ b/src/formatters.zig @@ -129,7 +129,7 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co if (builtin.os.tag == .macos) { const gpu_info = try detection.hardware.getGpuInfo(allocator); defer allocator.free(gpu_info.gpu_name); - return try Result{ .string = std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.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, ascii.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }) }; } else if (builtin.os.tag == .linux) { const gpu_info_list = try detection.hardware.getGpuInfo(allocator); for (gpu_info_list.items) |g| { From 073d512b67587a59faa9331ea1ab4690bf8e7161 Mon Sep 17 00:00:00 2001 From: utox39 Date: Sat, 12 Jul 2025 01:56:02 +0200 Subject: [PATCH 10/10] docs(README): update README --- README.md | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9d578b2..da7415e 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Zigfetch -- [Description](#description) -- [Requirements](#requirements) -- [Installation](#installation) -- [Usage](#usage) - - [Configuration](#configuration) -- [Roadtrip](#roadtrip) -- [Contributing](#contributing) +- [Description](#description) +- [Requirements](#requirements) +- [Installation](#installation) +- [Usage](#usage) + - [Configuration](#configuration) +- [Roadtrip](#roadtrip) +- [Contributing](#contributing) ## Description @@ -14,7 +14,11 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe ## Requirements -- [zig v0.14.0](https://ziglang.org/) +- \>= [zig v0.14.0](https://ziglang.org/) + +### Linux only + +- [libpci](https://github.com/pciutils/pciutils) (linux only) ## Installation @@ -43,20 +47,20 @@ $ zigfetch > [!IMPORTANT] > Currently, Zig does not have a built-in library for JSON validation via JSON schema, so it is very important to follow the pattern shown in the default configuration file ([config.json](https://github.com/utox39/zigfetch/blob/main/config.json)) to avoid errors -- Create the config folder +- Create the config folder ```console $ mkdir -p ~/.config/zigfetch ``` -- Create the config file +- Create the config file ```console $ cd ~/.config/zigfetch $ touch config.json ``` -- Or copy the default config (preferred way) +- Or copy the default config (preferred way) ```console $ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json @@ -64,11 +68,12 @@ $ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json ## Roadtrip -- [ ] Add ASCII art for each operating system and Linux distro -- [ ] Add GPU info for Linux -- [ ] Add packages info for Linux -- [x] Add user customization -- [ ] Add support for Windows +- [ ] Add ASCII art for each operating system and Linux distro +- [ ] Add GPU info for Linux +- [ ] Add packages info for Linux +- [x] Add user customization +- [ ] Add support for Windows ## Contributing + If you would like to contribute to this project just create a pull request which I will try to review as soon as possible.