feat: remove toStr methods

This commit is contained in:
utox39
2025-05-30 18:01:00 +02:00
parent 5019f177c1
commit ff810a1656
7 changed files with 9 additions and 97 deletions

View File

@@ -42,9 +42,7 @@ pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key
defer allocator.free(kernel_info.kernel_name);
defer allocator.free(kernel_info.kernel_release);
var buf: [1024]u8 = undefined;
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, try kernel_info.toStr(&buf) });
return 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 {
@@ -73,8 +71,7 @@ pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) ![]u8 {
pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
const uptime = try detection.system.getSystemUptime();
var buf: [1024]u8 = undefined;
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, try uptime.toStr(&buf) });
return 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 {
@@ -108,10 +105,7 @@ pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) ![]u8 {
pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
const cpu_info = try detection.hardware.getCpuInfo(allocator);
defer allocator.free(cpu_info.cpu_name);
var buf: [1024]u8 = undefined;
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, try cpu_info.toStr(&buf) });
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 });
}
pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) ![]u8 {
@@ -122,10 +116,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);
var buf: [1024]u8 = undefined;
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, try gpu_info.toStr(&buf) });
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 });
}
@@ -136,20 +127,8 @@ pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) ![]u8 {
}
pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
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);
}
var buf: [1024]u8 = undefined;
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, try ram_info.toStr(&buf) });
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 {
@@ -157,10 +136,9 @@ pub fn getDefaultFormattedSwapInfo(allocator: std.mem.Allocator) ![]u8 {
}
pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
var buf: [1024]u8 = undefined;
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} {s}", .{ key_color, key, ascii.Reset, try s.toStr(&buf) });
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, s.swap_usage, s.swap_usage, s.swap_usage_percentage });
} else {
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, ascii.Reset });
}
@@ -172,10 +150,7 @@ pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) ![]u8 {
pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
const disk_info = try detection.hardware.getDiskSize("/");
var buf: [1024]u8 = undefined;
return try std.fmt.allocPrint(allocator, "{s}{s}{s} {s}", .{ key_color, key, ascii.Reset, try disk_info.toStr(&buf) });
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 });
}
pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) ![]u8 {
@@ -195,14 +170,11 @@ pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) ![]u8 {
pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !std.ArrayList([]u8) {
var formatted_net_info_list = std.ArrayList([]u8).init(allocator);
var buf: [1024]u8 = undefined;
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}", .{ key_color, key, ascii.Reset, try n.toStr(&buf) }));
try formatted_net_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {s}", .{ key_color, key, n.interface_name, ascii.Reset, n.ipv4_addr }));
allocator.free(n.interface_name);
allocator.free(n.ipv4_addr);
@memset(&buf, 0);
}
net_info_list.deinit();

View File

@@ -7,10 +7,6 @@ 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, "{s} ({}) @ {d:.2} GHz", .{ self.cpu_name, self.cpu_cores, self.cpu_max_freq });
}
};
/// Struct representing RAM usage informations
@@ -18,10 +14,6 @@ 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, "{d:.2} / {d:.2} GiB ({}%)", .{ self.ram_usage, self.ram_size, self.ram_usage_percentage });
}
};
/// Struct representing Swap usage informations
@@ -29,10 +21,6 @@ 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, "{d:.2} / {d:.2} GiB ({}%)", .{ self.swap_usage, self.swap_size, self.swap_usage_percentage });
}
};
/// Struct representing Disk usage informations
@@ -41,10 +29,6 @@ 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, "({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 {

View File

@@ -9,10 +9,6 @@ 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, "({s}): {s}", .{ self.interface_name, self.ipv4_addr });
}
};
pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) {

View File

@@ -7,20 +7,12 @@ pub const SystemUptime = struct {
days: i8,
hours: i8,
minutes: i8,
pub fn toStr(self: SystemUptime, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{} days, {} hours, {} minutes", .{ self.days, self.hours, self.minutes });
}
};
/// Struct representing Kernel informations
pub const KernelInfo = struct {
kernel_name: []u8,
kernel_release: []u8,
pub fn toStr(self: KernelInfo, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{s} {s}", .{ self.kernel_name, self.kernel_release });
}
};
pub fn getHostname(allocator: std.mem.Allocator) ![]u8 {

View File

@@ -11,10 +11,6 @@ pub const CpuInfo = struct {
cpu_name: []u8,
cpu_cores: i32,
cpu_max_freq: f64,
pub fn toStr(self: CpuInfo, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{s} ({}) @ {d:.2} GHz", .{ self.cpu_name, self.cpu_cores, self.cpu_max_freq });
}
};
/// Struct representing GPU informations
@@ -22,10 +18,6 @@ pub const GpuInfo = struct {
gpu_name: []u8,
gpu_cores: i32,
gpu_freq: f64,
pub fn toStr(self: GpuInfo, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{s} ({}) @ {d:.2} GHz", .{ self.gpu_name, self.gpu_cores, self.gpu_freq });
}
};
/// Struct representing RAM usage informations
@@ -33,10 +25,6 @@ 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, "{d:.2} / {d:.2} GiB ({}%)", .{ self.ram_usage, self.ram_size, self.ram_usage_percentage });
}
};
/// Struct representing Swap usage informations
@@ -44,10 +32,6 @@ pub const SwapInfo = struct {
swap_size: f64,
swap_usage: f64,
swap_usage_percentage: u64,
pub fn toStr(self: SwapInfo, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{d:.2} / {d:.2} GiB ({}%)", .{ self.swap_usage, self.swap_size, self.swap_usage_percentage });
}
};
/// Struct representing Disk usage informations
@@ -56,10 +40,6 @@ 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, "({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 {

View File

@@ -9,10 +9,6 @@ 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, "({s}): {s}", .{ self.interface_name, self.ipv4_addr });
}
};
pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) {

View File

@@ -7,20 +7,12 @@ pub const SystemUptime = struct {
days: i8,
hours: i8,
minutes: i8,
pub fn toStr(self: SystemUptime, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{} days, {} hours, {} minutes", .{ self.days, self.hours, self.minutes });
}
};
/// Struct representing Kernel informations
pub const KernelInfo = struct {
kernel_name: []u8,
kernel_release: []u8,
pub fn toStr(self: KernelInfo, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, "{s} {s}", .{ self.kernel_name, self.kernel_release });
}
};
/// Returns the hostname.