From 4252a9f10a4bd7fc5e62ee7242ce22136a249d68 Mon Sep 17 00:00:00 2001 From: utox39 Date: Thu, 1 May 2025 19:07:09 +0200 Subject: [PATCH] feat(linux): add toStr method to each struct --- src/linux/hardware.zig | 16 ++++++++++++++++ src/linux/network.zig | 4 ++++ src/linux/system.zig | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index 6864308..e0ce989 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -6,18 +6,30 @@ pub const CpuInfo = struct { cpu_name: []u8, cpu_cores: i32, cpu_max_freq: f32, + + pub fn toStr(self: CpuInfo, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Cpu: {s} ({}) @ {d:.2} GHz", .{ self.cpu_name, self.cpu_cores, self.cpu_max_freq }); + } }; pub const RamInfo = struct { ram_size: f64, ram_usage: f64, ram_usage_percentage: u8, + + pub fn toStr(self: RamInfo, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Ram: {d:.2} / {d:.2} GiB ({}%)", .{ self.ram_usage, self.ram_size, self.ram_usage_percentage }); + } }; pub const SwapInfo = struct { swap_size: f64, swap_usage: f64, swap_usage_percentage: u8, + + pub fn toStr(self: SwapInfo, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Swap: {d:.2} / {d:.2} GiB ({}%)", .{ self.swap_usage, self.swap_size, self.swap_usage_percentage }); + } }; pub const DiskInfo = struct { @@ -25,6 +37,10 @@ pub const DiskInfo = struct { disk_size: f64, disk_usage: f64, disk_usage_percentage: u8, + + pub fn toStr(self: DiskInfo, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Disk ({s}): {d:.2} / {d:.2} GB ({}%)", .{ self.disk_path, self.disk_usage, self.disk_size, self.disk_usage_percentage }); + } }; pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo { diff --git a/src/linux/network.zig b/src/linux/network.zig index b8da949..b8cfef4 100644 --- a/src/linux/network.zig +++ b/src/linux/network.zig @@ -8,6 +8,10 @@ const c_socket = @cImport(@cInclude("sys/socket.h")); pub const NetInfo = struct { interface_name: []u8, ipv4_addr: []u8, + + pub fn toStr(self: NetInfo, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Local IP ({s}): {s}", .{ self.interface_name, self.ipv4_addr }); + } }; pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) { diff --git a/src/linux/system.zig b/src/linux/system.zig index e6068cc..0e682c8 100644 --- a/src/linux/system.zig +++ b/src/linux/system.zig @@ -7,11 +7,19 @@ pub const SystemUptime = struct { days: i8, hours: i8, minutes: i8, + + pub fn toStr(self: SystemUptime, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Uptime: {} days, {} hours, {} minutes", .{ self.days, self.hours, self.minutes }); + } }; pub const KernelInfo = struct { kernel_name: []u8, kernel_release: []u8, + + pub fn toStr(self: KernelInfo, buf: []u8) ![]u8 { + return std.fmt.bufPrint(buf, "Kernel: {s} {s}", .{ self.kernel_name, self.kernel_release }); + } }; pub fn getHostname(allocator: std.mem.Allocator) ![]u8 {