generated from general-packages/pika-pkg-template
Update pikafetch to version 0.2.0-101pika2, refactor memory allocation and system info retrieval
All checks were successful
PikaOS Package Build & Release (amd64-v3) / build (push) Successful in 57s
All checks were successful
PikaOS Package Build & Release (amd64-v3) / build (push) Successful in 57s
- Incremented version number in changelog to 0.2.0-101pika2. - Transitioned to using an arena allocator, removing the GeneralPurposeAllocator. - Enhanced memory management by eliminating unnecessary deinitialization and free calls. - Updated system information retrieval to improve performance. - Adjusted terminal and shell information retrieval for better compatibility and efficiency.
This commit is contained in:
parent
483604eb2d
commit
cede8a99cd
2
.github/release-nest-v3
vendored
2
.github/release-nest-v3
vendored
@ -1 +1 @@
|
||||
2
|
||||
1
|
@ -1,6 +1,6 @@
|
||||
pikafetch (0.2.0-101pika1) pika; urgency=medium
|
||||
pikafetch (0.2.0-101pika2) pika; urgency=medium
|
||||
|
||||
* Move to zig nightly, remove all use of libc
|
||||
* Move to zig nightly, remove all use of libc, switch to using arena allocator
|
||||
|
||||
-- ferreo <harderthanfire@gmail.com> Wed, 18 Jan 2023 21:48:14 +0000
|
||||
|
||||
|
@ -4,24 +4,14 @@ const printer = @import("display/printer.zig");
|
||||
const terminal = @import("display/terminal.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer {
|
||||
const check = gpa.deinit();
|
||||
if (check == .leak) std.debug.print("Memory leaked\n", .{});
|
||||
}
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
var info = try system_info.SystemInfo.init(allocator);
|
||||
defer info.deinit();
|
||||
|
||||
const term_size = try terminal.getTerminalSize();
|
||||
const formatted_info = try info.formatInfo(allocator);
|
||||
defer {
|
||||
for (formatted_info) |line| {
|
||||
allocator.free(line);
|
||||
}
|
||||
allocator.free(formatted_info);
|
||||
}
|
||||
|
||||
try printer.printDisplay(std.io.getStdOut().writer(), formatted_info, term_size);
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ pub const ShellInfo = struct {
|
||||
|
||||
pub fn getShellInfo(allocator: std.mem.Allocator) !ShellInfo {
|
||||
if (std.process.getEnvVarOwned(allocator, "SHELL")) |shell_path| {
|
||||
defer allocator.free(shell_path);
|
||||
const shell_name = std.fs.path.basename(shell_path);
|
||||
|
||||
if (std.mem.endsWith(u8, shell_name, "login")) {
|
||||
@ -27,7 +26,6 @@ pub fn getShellInfo(allocator: std.mem.Allocator) !ShellInfo {
|
||||
|
||||
while (depth < max_depth) : (depth += 1) {
|
||||
const proc_path = try std.fmt.allocPrint(allocator, "/proc/{d}/comm", .{current_pid});
|
||||
defer allocator.free(proc_path);
|
||||
|
||||
const proc_file = std.fs.openFileAbsolute(proc_path, .{}) catch |err| switch (err) {
|
||||
error.FileNotFound => break,
|
||||
@ -71,7 +69,6 @@ pub fn getShellInfo(allocator: std.mem.Allocator) !ShellInfo {
|
||||
}
|
||||
|
||||
const stat_path = try std.fmt.allocPrint(allocator, "/proc/{d}/stat", .{current_pid});
|
||||
defer allocator.free(stat_path);
|
||||
|
||||
const stat_file = std.fs.openFileAbsolute(stat_path, .{}) catch break;
|
||||
defer stat_file.close();
|
||||
|
@ -5,49 +5,52 @@ const posix = std.posix;
|
||||
const mem = std.mem;
|
||||
|
||||
pub fn getTerminal(allocator: mem.Allocator) ![]const u8 {
|
||||
if (std.process.getEnvVarOwned(allocator, "TERM_PROGRAM")) |term| {
|
||||
return term;
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "KITTY_PID")) |pid| {
|
||||
allocator.free(pid);
|
||||
return allocator.dupe(u8, "kitty");
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "WT_SESSION")) |session| {
|
||||
allocator.free(session);
|
||||
return allocator.dupe(u8, "Windows Terminal");
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "ALACRITTY_SOCKET")) |socket| {
|
||||
allocator.free(socket);
|
||||
return allocator.dupe(u8, "Alacritty");
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "KONSOLE_VERSION")) |version| {
|
||||
allocator.free(version);
|
||||
return allocator.dupe(u8, "Konsole");
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "GNOME_TERMINAL_SCREEN")) |screen| {
|
||||
allocator.free(screen);
|
||||
return allocator.dupe(u8, "GNOME Terminal");
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "LC_TERMINAL")) |term| {
|
||||
return term;
|
||||
} else |_| {}
|
||||
|
||||
if (std.process.getEnvVarOwned(allocator, "TERM")) |term| {
|
||||
if (!std.mem.eql(u8, term, "linux")) {
|
||||
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch null;
|
||||
if (term_program) |term| {
|
||||
return term;
|
||||
}
|
||||
allocator.free(term);
|
||||
} else |_| {}
|
||||
|
||||
if (getTtyName(allocator, posix.STDIN_FILENO)) |tty| {
|
||||
return tty;
|
||||
} else |_| {}
|
||||
const kitty = std.process.getEnvVarOwned(allocator, "KITTY_PID") catch null;
|
||||
if (kitty != null) {
|
||||
return allocator.dupe(u8, "kitty");
|
||||
}
|
||||
|
||||
const wt = std.process.getEnvVarOwned(allocator, "WT_SESSION") catch null;
|
||||
if (wt != null) {
|
||||
return allocator.dupe(u8, "Windows Terminal");
|
||||
}
|
||||
|
||||
const alacritty = std.process.getEnvVarOwned(allocator, "ALACRITTY_SOCKET") catch null;
|
||||
if (alacritty != null) {
|
||||
return allocator.dupe(u8, "Alacritty");
|
||||
}
|
||||
|
||||
const konsole = std.process.getEnvVarOwned(allocator, "KONSOLE_VERSION") catch null;
|
||||
if (konsole != null) {
|
||||
return allocator.dupe(u8, "Konsole");
|
||||
}
|
||||
|
||||
const gnome = std.process.getEnvVarOwned(allocator, "GNOME_TERMINAL_SCREEN") catch null;
|
||||
if (gnome != null) {
|
||||
return allocator.dupe(u8, "GNOME Terminal");
|
||||
}
|
||||
|
||||
const lc_term = std.process.getEnvVarOwned(allocator, "LC_TERMINAL") catch null;
|
||||
if (lc_term) |term| {
|
||||
return term;
|
||||
}
|
||||
|
||||
const term = std.process.getEnvVarOwned(allocator, "TERM") catch null;
|
||||
if (term) |t| {
|
||||
if (!std.mem.eql(u8, t, "linux")) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
const tty = getTtyName(allocator, posix.STDIN_FILENO) catch null;
|
||||
if (tty) |t| {
|
||||
return t;
|
||||
}
|
||||
|
||||
return allocator.dupe(u8, "unknown");
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ const Child = std.process.Child;
|
||||
|
||||
pub fn getWM(allocator: std.mem.Allocator) ![]const u8 {
|
||||
const wayland_display = std.process.getEnvVarOwned(allocator, "WAYLAND_DISPLAY") catch null;
|
||||
defer if (wayland_display) |wd| allocator.free(wd);
|
||||
|
||||
if (wayland_display != null) {
|
||||
if (std.process.getEnvVarOwned(allocator, "XDG_CURRENT_DESKTOP")) |desktop| {
|
||||
@ -15,7 +14,6 @@ pub fn getWM(allocator: std.mem.Allocator) ![]const u8 {
|
||||
}
|
||||
} else {
|
||||
const display = std.process.getEnvVarOwned(allocator, "DISPLAY") catch null;
|
||||
defer if (display) |d| allocator.free(d);
|
||||
|
||||
if (display != null) {
|
||||
if (std.process.getEnvVarOwned(allocator, "XDG_CURRENT_DESKTOP")) |desktop| {
|
||||
|
@ -39,12 +39,6 @@ const GPUInfo = struct {
|
||||
|
||||
pub fn getGPUInfo(allocator: mem.Allocator) ![][]const u8 {
|
||||
var gpus = ArrayList([]const u8).init(allocator);
|
||||
errdefer {
|
||||
for (gpus.items) |gpu| {
|
||||
allocator.free(gpu);
|
||||
}
|
||||
gpus.deinit();
|
||||
}
|
||||
|
||||
var dir = fs.openDirAbsolute("/sys/class/drm", .{ .iterate = true }) catch {
|
||||
const unknown = try allocator.dupe(u8, "Unknown GPU");
|
||||
@ -77,8 +71,6 @@ fn detectGPU(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
}
|
||||
|
||||
const vendor_path = try allocPrint(allocator, "/sys/class/drm/{s}/device/vendor", .{card_name});
|
||||
defer allocator.free(vendor_path);
|
||||
|
||||
const vendor_file = fs.openFileAbsolute(vendor_path, .{}) catch return null;
|
||||
defer vendor_file.close();
|
||||
|
||||
@ -87,8 +79,6 @@ fn detectGPU(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
const vendor_str = mem.trim(u8, vendor_buf[0..vendor_len], "\n\r \t0x");
|
||||
|
||||
const device_path = try allocPrint(allocator, "/sys/class/drm/{s}/device/device", .{card_name});
|
||||
defer allocator.free(device_path);
|
||||
|
||||
const device_file = fs.openFileAbsolute(device_path, .{}) catch return null;
|
||||
defer device_file.close();
|
||||
|
||||
@ -97,7 +87,6 @@ fn detectGPU(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
const device_str = mem.trim(u8, device_buf[0..device_len], "\n\r \t0x");
|
||||
|
||||
const model = try allocPrint(allocator, "(vendor: 0x{s}, device: 0x{s})", .{ vendor_str, device_str });
|
||||
errdefer allocator.free(model);
|
||||
|
||||
const gpu_info = GPUInfo{
|
||||
.vendor = "GPU",
|
||||
@ -107,7 +96,6 @@ fn detectGPU(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
};
|
||||
|
||||
const formatted = try gpu_info.format(allocator);
|
||||
allocator.free(model);
|
||||
return formatted;
|
||||
}
|
||||
|
||||
@ -168,8 +156,6 @@ fn cleanModelName(model: []const u8) []const u8 {
|
||||
|
||||
fn queryHwdb(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
const modalias_path = try allocPrint(allocator, "/sys/class/drm/{s}/device/modalias", .{card_name});
|
||||
defer allocator.free(modalias_path);
|
||||
|
||||
const modalias_file = fs.openFileAbsolute(modalias_path, .{}) catch return null;
|
||||
defer modalias_file.close();
|
||||
|
||||
@ -217,9 +203,7 @@ fn queryHwdb(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
var model_name: ?[]const u8 = null;
|
||||
|
||||
const vendor_pattern = try allocPrint(allocator, "pci:v{s}*", .{vendor_id.?});
|
||||
defer allocator.free(vendor_pattern);
|
||||
const device_pattern = try allocPrint(allocator, "pci:v{s}d{s}*", .{ vendor_id.?, device_id.? });
|
||||
defer allocator.free(device_pattern);
|
||||
|
||||
var it = mem.splitScalar(u8, mapped, '\n');
|
||||
while (it.next()) |line| {
|
||||
@ -256,14 +240,8 @@ fn queryHwdb(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
|
||||
};
|
||||
|
||||
const formatted = try gpu_info.format(allocator);
|
||||
allocator.free(gpu_info.vendor);
|
||||
allocator.free(gpu_info.model);
|
||||
if (vendor_name != null) allocator.free(vendor_name.?);
|
||||
if (model_name != null) allocator.free(model_name.?);
|
||||
return formatted;
|
||||
}
|
||||
|
||||
if (vendor_name != null) allocator.free(vendor_name.?);
|
||||
if (model_name != null) allocator.free(model_name.?);
|
||||
return null;
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ pub const SystemInfo = struct {
|
||||
uptime: u64,
|
||||
memory_total: u64,
|
||||
memory_used: u64,
|
||||
allocator: std.mem.Allocator,
|
||||
host: []const u8,
|
||||
packages: u32,
|
||||
wm: []const u8,
|
||||
@ -45,22 +44,6 @@ pub const SystemInfo = struct {
|
||||
var shell_info: ?[]const u8 = null;
|
||||
var cpu_info: ?[]const u8 = null;
|
||||
|
||||
errdefer {
|
||||
if (username) |u| allocator.free(u);
|
||||
if (hostname_str) |h| allocator.free(h);
|
||||
if (os_name) |o| allocator.free(o);
|
||||
if (kernel_ver) |k| allocator.free(k);
|
||||
if (host) |h| allocator.free(h);
|
||||
if (wm_name) |w| allocator.free(w);
|
||||
if (terminal_name) |t| allocator.free(t);
|
||||
if (gpu_list) |list| {
|
||||
for (list) |g| allocator.free(g);
|
||||
allocator.free(list);
|
||||
}
|
||||
if (shell_info) |s| allocator.free(s);
|
||||
if (cpu_info) |c| allocator.free(c);
|
||||
}
|
||||
|
||||
username = try hostname.getUsername(allocator);
|
||||
hostname_str = try hostname.getHostname(allocator);
|
||||
os_name = try hostname.getOSName(allocator);
|
||||
@ -85,7 +68,6 @@ pub const SystemInfo = struct {
|
||||
.uptime = 0, // TODO: Implement uptime
|
||||
.memory_total = mem_info.total,
|
||||
.memory_used = mem_info.used,
|
||||
.allocator = allocator,
|
||||
.host = host.?,
|
||||
.packages = pkg_count,
|
||||
.wm = wm_name.?,
|
||||
@ -100,30 +82,8 @@ pub const SystemInfo = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *const SystemInfo) void {
|
||||
self.allocator.free(self.username);
|
||||
self.allocator.free(self.hostname);
|
||||
self.allocator.free(self.os_name);
|
||||
self.allocator.free(self.kernel);
|
||||
self.allocator.free(self.host);
|
||||
self.allocator.free(self.wm);
|
||||
self.allocator.free(self.terminal);
|
||||
for (self.gpus) |gpu_info| {
|
||||
self.allocator.free(gpu_info);
|
||||
}
|
||||
self.allocator.free(self.gpus);
|
||||
self.allocator.free(self.shell_name);
|
||||
self.allocator.free(self.cpu_info);
|
||||
}
|
||||
|
||||
pub fn formatInfo(self: *const SystemInfo, output_allocator: std.mem.Allocator) ![]const []const u8 {
|
||||
var info = try std.ArrayList([]const u8).initCapacity(output_allocator, 13 + self.gpus.len);
|
||||
errdefer {
|
||||
for (info.items) |item| {
|
||||
output_allocator.free(item);
|
||||
}
|
||||
info.deinit();
|
||||
}
|
||||
|
||||
const calculations = struct {
|
||||
mem_percentage: u64,
|
||||
@ -209,6 +169,7 @@ pub const SystemInfo = struct {
|
||||
return info.toOwnedSlice();
|
||||
}
|
||||
};
|
||||
|
||||
fn getUsageColor(percentage: u64) []const u8 {
|
||||
return if (percentage < 50)
|
||||
colors.Color.green
|
||||
|
Loading…
x
Reference in New Issue
Block a user