refactor: rename all 'allocator' args to 'gpa'
This commit is contained in:
@@ -44,8 +44,8 @@ pub fn getUsernameHostnameColor(config: ?std.json.Parsed(Config)) ?[]u8 {
|
||||
} else return null;
|
||||
}
|
||||
|
||||
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.array_list.Managed(ModuleType) {
|
||||
var modules_list = std.array_list.Managed(ModuleType).init(allocator);
|
||||
pub fn getModulesTypes(gpa: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.array_list.Managed(ModuleType) {
|
||||
var modules_list = std.array_list.Managed(ModuleType).init(gpa);
|
||||
|
||||
if (config) |c| {
|
||||
for (c.value.modules) |module| {
|
||||
@@ -62,12 +62,12 @@ pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Co
|
||||
return modules_list;
|
||||
}
|
||||
|
||||
pub fn readConfigFile(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ) !?std.json.Parsed(Config) {
|
||||
const home = try std.process.Environ.getAlloc(environ, allocator, "HOME");
|
||||
defer allocator.free(home);
|
||||
pub fn readConfigFile(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ) !?std.json.Parsed(Config) {
|
||||
const home = try std.process.Environ.getAlloc(environ, gpa, "HOME");
|
||||
defer gpa.free(home);
|
||||
|
||||
const config_abs_path = try std.mem.concat(allocator, u8, &.{ home, "/.config/zigfetch/config.json" });
|
||||
defer allocator.free(config_abs_path);
|
||||
const config_abs_path = try std.mem.concat(gpa, u8, &.{ home, "/.config/zigfetch/config.json" });
|
||||
defer gpa.free(config_abs_path);
|
||||
|
||||
const config_file = std.Io.Dir.openFileAbsolute(io, config_abs_path, .{ .mode = .read_only }) catch |err| switch (err) {
|
||||
error.FileNotFound => return null,
|
||||
@@ -77,8 +77,8 @@ pub fn readConfigFile(allocator: std.mem.Allocator, io: std.Io, environ: std.pro
|
||||
|
||||
const file_size = (try config_file.stat(io)).size;
|
||||
|
||||
const config_data = try utils.readFile(allocator, io, config_file, file_size);
|
||||
defer allocator.free(config_data);
|
||||
const config_data = try utils.readFile(gpa, io, config_file, file_size);
|
||||
defer gpa.free(config_data);
|
||||
|
||||
return try std.json.parseFromSlice(Config, allocator, config_data, .{ .allocate = .alloc_always });
|
||||
return try std.json.parseFromSlice(Config, gpa, config_data, .{ .allocate = .alloc_always });
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ test "parse ffffff" {
|
||||
try std.testing.expect((result.r == 255) and (result.g == 255) and (result.b == 255));
|
||||
}
|
||||
|
||||
pub fn printAsciiAndModules(allocator: std.mem.Allocator, io: std.Io, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void {
|
||||
pub fn printAsciiAndModules(gpa: std.mem.Allocator, io: std.Io, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void {
|
||||
var stdout_buffer: [2048]u8 = undefined;
|
||||
var stdout_file_writer: std.Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
|
||||
const stdout = &stdout_file_writer.interface;
|
||||
@@ -75,18 +75,18 @@ pub fn printAsciiAndModules(allocator: std.mem.Allocator, io: std.Io, ascii_art_
|
||||
const ascii_file = try std.Io.Dir.cwd().openFile(io, ascii, .{ .mode = .read_only });
|
||||
defer ascii_file.close(io);
|
||||
const file_size = (try ascii_file.stat(io)).size;
|
||||
ascii_art_data = try utils.readFile(allocator, io, ascii_file, file_size);
|
||||
ascii_art_data = try utils.readFile(gpa, io, ascii_file, file_size);
|
||||
} else {
|
||||
ascii_art_data = @embedFile("./assets/ascii/guy_fawks.txt");
|
||||
}
|
||||
|
||||
defer if (ascii_art_path != null) {
|
||||
allocator.free(ascii_art_data);
|
||||
gpa.free(ascii_art_data);
|
||||
};
|
||||
|
||||
var lines = std.mem.splitScalar(u8, ascii_art_data, '\n');
|
||||
|
||||
var ascii_art_content_list = std.array_list.Managed([]const u8).init(allocator);
|
||||
var ascii_art_content_list = std.array_list.Managed([]const u8).init(gpa);
|
||||
defer ascii_art_content_list.deinit();
|
||||
|
||||
while (lines.next()) |line| {
|
||||
@@ -116,7 +116,7 @@ pub fn printAsciiAndModules(allocator: std.mem.Allocator, io: std.Io, ascii_art_
|
||||
while (i < max_len) : (i += 1) {
|
||||
// Print the ascii art if the width of the terminal is greater than the spacing (5) + the longest ascii art row length + the longest sys info string length
|
||||
if (can_print_ascii_art) {
|
||||
const alignment_buffer = try allocator.alloc(u8, if (i < ascii_art_len) longest_ascii_art_row_len - (try utils.countCodepoints(ascii_art_items[i])) + spacing else longest_ascii_art_row_len + spacing);
|
||||
const alignment_buffer = try gpa.alloc(u8, if (i < ascii_art_len) longest_ascii_art_row_len - (try utils.countCodepoints(ascii_art_items[i])) + spacing else longest_ascii_art_row_len + spacing);
|
||||
@memset(alignment_buffer, ' ');
|
||||
|
||||
if (i < ascii_art_len) {
|
||||
@@ -125,7 +125,7 @@ pub fn printAsciiAndModules(allocator: std.mem.Allocator, io: std.Io, ascii_art_
|
||||
try stdout.print("{s}", .{alignment_buffer});
|
||||
}
|
||||
|
||||
allocator.free(alignment_buffer);
|
||||
gpa.free(alignment_buffer);
|
||||
|
||||
try stdout.flush();
|
||||
}
|
||||
@@ -151,6 +151,6 @@ pub fn printAsciiAndModules(allocator: std.mem.Allocator, io: std.Io, ascii_art_
|
||||
}
|
||||
|
||||
for (sys_info_list.items) |item| {
|
||||
allocator.free(item);
|
||||
gpa.free(item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ pub const default_formatters = [_]*const fn (fmt_ctx: FormatterContext) anyerror
|
||||
&getDefaultFormattedLocaleInfo,
|
||||
};
|
||||
|
||||
pub fn getFormattedUsernameHostname(allocator: std.mem.Allocator, color: []const u8, username: []const u8, hostname: []const u8) ![]u8 {
|
||||
return try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{
|
||||
pub fn getFormattedUsernameHostname(gpa: std.mem.Allocator, color: []const u8, username: []const u8, hostname: []const u8) ![]u8 {
|
||||
return try std.fmt.allocPrint(gpa, "{s}{s}{s}@{s}{s}{s}", .{
|
||||
color,
|
||||
username,
|
||||
display.Reset,
|
||||
|
||||
@@ -40,7 +40,7 @@ pub const DiskInfo = struct {
|
||||
disk_usage_percentage: u8,
|
||||
};
|
||||
|
||||
pub fn getCpuInfo(allocator: std.mem.Allocator, io: std.Io) !CpuInfo {
|
||||
pub fn getCpuInfo(gpa: std.mem.Allocator, io: std.Io) !CpuInfo {
|
||||
const cpu_cores = c_unistd.sysconf(c_unistd._SC_NPROCESSORS_ONLN);
|
||||
|
||||
// Reads /proc/cpuinfo
|
||||
@@ -54,8 +54,8 @@ pub fn getCpuInfo(allocator: std.mem.Allocator, io: std.Io) !CpuInfo {
|
||||
//
|
||||
// Only the first section (core 0) will be parsed
|
||||
// 512 is more than enough
|
||||
const cpuinfo_data = try utils.readFile(allocator, io, cpuinfo_file, 512);
|
||||
defer allocator.free(cpuinfo_data);
|
||||
const cpuinfo_data = try utils.readFile(gpa, io, cpuinfo_file, 512);
|
||||
defer gpa.free(cpuinfo_data);
|
||||
|
||||
// Parsing /proc/cpuinfo
|
||||
var model_name: ?[]const u8 = null;
|
||||
@@ -100,8 +100,8 @@ pub fn getCpuInfo(allocator: std.mem.Allocator, io: std.Io) !CpuInfo {
|
||||
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
||||
const maxfreq_file = try std.Io.Dir.cwd().openFile(io, cpuinfo_max_freq_path, .{ .mode = .read_only });
|
||||
defer maxfreq_file.close(io);
|
||||
const maxfreq_data = try utils.readFile(allocator, io, maxfreq_file, 32);
|
||||
defer allocator.free(maxfreq_data);
|
||||
const maxfreq_data = try utils.readFile(gpa, io, maxfreq_file, 32);
|
||||
defer gpa.free(maxfreq_data);
|
||||
|
||||
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
||||
const trimmed = std.mem.trim(u8, maxfreq_data, " \n\r");
|
||||
@@ -115,14 +115,14 @@ pub fn getCpuInfo(allocator: std.mem.Allocator, io: std.Io) !CpuInfo {
|
||||
}
|
||||
|
||||
return CpuInfo{
|
||||
.cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"),
|
||||
.cpu_name = try gpa.dupe(u8, model_name orelse "Unknown"),
|
||||
.cpu_cores = @as(i32, @intCast(cpu_cores)),
|
||||
.cpu_max_freq = cpu_max_freq,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getGpuInfo(allocator: std.mem.Allocator) !std.array_list.Managed(GpuInfo) {
|
||||
var gpu_info_list = std.array_list.Managed(GpuInfo).init(allocator);
|
||||
pub fn getGpuInfo(gpa: std.mem.Allocator) !std.array_list.Managed(GpuInfo) {
|
||||
var gpu_info_list = std.array_list.Managed(GpuInfo).init(gpa);
|
||||
|
||||
const display_controller = 0x03;
|
||||
|
||||
@@ -151,13 +151,13 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.array_list.Managed(GpuInfo)
|
||||
devices.*.device_id,
|
||||
);
|
||||
|
||||
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
|
||||
const gpu_name = try gpa.dupe(u8, std.mem.span(name));
|
||||
|
||||
const maybe_parsed_gpu_name = try parseGpuName(allocator, gpu_name);
|
||||
const maybe_parsed_gpu_name = try parseGpuName(gpa, gpu_name);
|
||||
var parsed_gpu_name: []u8 = undefined;
|
||||
|
||||
if (maybe_parsed_gpu_name != null) {
|
||||
allocator.free(gpu_name);
|
||||
gpa.free(gpu_name);
|
||||
parsed_gpu_name = maybe_parsed_gpu_name.?;
|
||||
} else {
|
||||
parsed_gpu_name = gpu_name;
|
||||
@@ -173,7 +173,7 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.array_list.Managed(GpuInfo)
|
||||
|
||||
if (gpu_info_list.items.len == 0) {
|
||||
try gpu_info_list.append(GpuInfo{
|
||||
.gpu_name = try allocator.dupe(u8, "Unknown"),
|
||||
.gpu_name = try gpa.dupe(u8, "Unknown"),
|
||||
.gpu_cores = 0,
|
||||
.gpu_freq = 0.0,
|
||||
});
|
||||
@@ -182,24 +182,24 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.array_list.Managed(GpuInfo)
|
||||
return gpu_info_list;
|
||||
}
|
||||
|
||||
fn parseGpuName(allocator: std.mem.Allocator, name: []u8) !?[]u8 {
|
||||
fn parseGpuName(gpa: std.mem.Allocator, name: []u8) !?[]u8 {
|
||||
// NOTE: for references: https://github.com/pciutils/pciutils/blob/master/pci.ids
|
||||
|
||||
if (std.mem.startsWith(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]")) {
|
||||
const size = std.mem.replacementSize(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]", "AMD");
|
||||
const parsed_gpu_name = try allocator.alloc(u8, size);
|
||||
const parsed_gpu_name = try gpa.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]", "AMD", parsed_gpu_name);
|
||||
|
||||
return parsed_gpu_name;
|
||||
} else if (std.mem.startsWith(u8, name, "Intel Corporation")) {
|
||||
const size = std.mem.replacementSize(u8, name, "Intel Corporation", "Intel");
|
||||
const parsed_gpu_name = try allocator.alloc(u8, size);
|
||||
const parsed_gpu_name = try gpa.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, name, "Intel Corporation", "Intel", parsed_gpu_name);
|
||||
|
||||
return parsed_gpu_name;
|
||||
} else if (std.mem.startsWith(u8, name, "NVIDIA Corporation")) {
|
||||
const size = std.mem.replacementSize(u8, name, "NVIDIA Corporation", "NVIDIA");
|
||||
const parsed_gpu_name = try allocator.alloc(u8, size);
|
||||
const parsed_gpu_name = try gpa.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, name, "NVIDIA Corporation", "NVIDIA", parsed_gpu_name);
|
||||
|
||||
return parsed_gpu_name;
|
||||
@@ -208,7 +208,7 @@ fn parseGpuName(allocator: std.mem.Allocator, name: []u8) !?[]u8 {
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn getRamInfo(allocator: std.mem.Allocator, io: std.Io) !RamInfo {
|
||||
pub fn getRamInfo(gpa: std.mem.Allocator, io: std.Io) !RamInfo {
|
||||
// Reads /proc/meminfo
|
||||
const meminfo_path = "/proc/meminfo";
|
||||
const meminfo_file = try std.Io.Dir.cwd().openFile(io, meminfo_path, .{ .mode = .read_only });
|
||||
@@ -220,8 +220,8 @@ pub fn getRamInfo(allocator: std.mem.Allocator, io: std.Io) !RamInfo {
|
||||
//
|
||||
// We only need to read the first few lines
|
||||
// 512 is more than enough
|
||||
const meminfo_data = try utils.readFile(allocator, io, meminfo_file, 512);
|
||||
defer allocator.free(meminfo_data);
|
||||
const meminfo_data = try utils.readFile(gpa, io, meminfo_file, 512);
|
||||
defer gpa.free(meminfo_data);
|
||||
|
||||
// Parsing /proc/meminfo
|
||||
var total_mem: f64 = 0.0;
|
||||
@@ -277,7 +277,7 @@ pub fn getRamInfo(allocator: std.mem.Allocator, io: std.Io) !RamInfo {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getSwapInfo(allocator: std.mem.Allocator, io: std.Io) !?SwapInfo {
|
||||
pub fn getSwapInfo(gpa: std.mem.Allocator, io: std.Io) !?SwapInfo {
|
||||
// Reads /proc/meminfo
|
||||
const meminfo_path = "/proc/meminfo";
|
||||
const meminfo_file = try std.Io.Dir.cwd().openFile(io, meminfo_path, .{ .mode = .read_only });
|
||||
@@ -289,8 +289,8 @@ pub fn getSwapInfo(allocator: std.mem.Allocator, io: std.Io) !?SwapInfo {
|
||||
//
|
||||
// We only need to read the first few lines
|
||||
// 512 is ok
|
||||
const meminfo_data = try utils.readFile(allocator, io, meminfo_file, 512);
|
||||
defer allocator.free(meminfo_data);
|
||||
const meminfo_data = try utils.readFile(gpa, io, meminfo_file, 512);
|
||||
defer gpa.free(meminfo_data);
|
||||
|
||||
// Parsing /proc/meminfo
|
||||
var total_swap: f64 = 0.0;
|
||||
|
||||
@@ -11,8 +11,8 @@ pub const NetInfo = struct {
|
||||
ipv4_addr: []u8,
|
||||
};
|
||||
|
||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||
var net_info_list = std.array_list.Managed(NetInfo).init(allocator);
|
||||
pub fn getNetInfo(gpa: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||
var net_info_list = std.array_list.Managed(NetInfo).init(gpa);
|
||||
|
||||
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
||||
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
||||
@@ -35,8 +35,8 @@ pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo)
|
||||
const ip_str = c_inet.inet_ntop(c_inet.AF_INET, &addr_in.sin_addr, &ip_buf, c_inet.INET_ADDRSTRLEN);
|
||||
if (ip_str) |ip| {
|
||||
try net_info_list.append(NetInfo{
|
||||
.interface_name = try allocator.dupe(u8, std.mem.span(ifa.ifa_name)),
|
||||
.ipv4_addr = try allocator.dupe(u8, std.mem.span(ip)),
|
||||
.interface_name = try gpa.dupe(u8, std.mem.span(ifa.ifa_name)),
|
||||
.ipv4_addr = try gpa.dupe(u8, std.mem.span(ip)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
const std = @import("std");
|
||||
const utils = @import("../utils.zig");
|
||||
|
||||
pub fn getPackagesInfo(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ) ![]const u8 {
|
||||
var packages_info = std.array_list.Managed(u8).init(allocator);
|
||||
pub fn getPackagesInfo(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ) ![]const u8 {
|
||||
var packages_info = std.array_list.Managed(u8).init(gpa);
|
||||
defer packages_info.deinit();
|
||||
|
||||
const flatpak_packages = countFlatpakPackages(io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const nix_packages = countNixPackages(allocator, io, environ) catch 0;
|
||||
const dpkg_packages = countDpkgPackages(allocator, io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const nix_packages = countNixPackages(gpa, io, environ) catch 0;
|
||||
const dpkg_packages = countDpkgPackages(gpa, io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const pacman_packages = countPacmanPackages(io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const xbps_packages = countXbpsPackages(allocator, io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const xbps_packages = countXbpsPackages(gpa, io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
|
||||
var buffer: [32]u8 = undefined;
|
||||
|
||||
@@ -33,7 +33,7 @@ pub fn getPackagesInfo(allocator: std.mem.Allocator, io: std.Io, environ: std.pr
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " Xbps: {d}", .{xbps_packages}));
|
||||
}
|
||||
|
||||
return try allocator.dupe(u8, packages_info.items);
|
||||
return try gpa.dupe(u8, packages_info.items);
|
||||
}
|
||||
|
||||
fn countFlatpakPackages(io: std.Io) !usize {
|
||||
@@ -96,35 +96,35 @@ fn countFlatpakRuntimes(io: std.Io) !usize {
|
||||
return counter;
|
||||
}
|
||||
|
||||
fn countNixPackages(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ) !usize {
|
||||
fn countNixPackages(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ) !usize {
|
||||
// `/run/current-system` is a sym-link, so we need to obtein the real path
|
||||
const real_path = try std.Io.Dir.realPathFileAbsoluteAlloc(io, "/run/current-system", allocator);
|
||||
defer allocator.free(real_path);
|
||||
const real_path = try std.Io.Dir.realPathFileAbsoluteAlloc(io, "/run/current-system", gpa);
|
||||
defer gpa.free(real_path);
|
||||
|
||||
var hash: [32]u8 = undefined;
|
||||
std.crypto.hash.Blake3.hash(real_path, &hash, .{});
|
||||
const hash_hex = try std.fmt.allocPrint(allocator, "{x}", .{hash});
|
||||
defer allocator.free(hash_hex);
|
||||
const hash_hex = try std.fmt.allocPrint(gpa, "{x}", .{hash});
|
||||
defer gpa.free(hash_hex);
|
||||
|
||||
var count: usize = 0;
|
||||
|
||||
// Inspired by https://github.com/fastfetch-cli/fastfetch/blob/608382109cda6623e53f318e8aced54cf8e5a042/src/detection/packages/packages_nix.c#L81
|
||||
count = checkNixCache(allocator, io, environ, hash_hex) catch |err| switch (err) {
|
||||
count = checkNixCache(gpa, io, environ, hash_hex) catch |err| switch (err) {
|
||||
error.FileNotFound, error.InvalidCache => {
|
||||
// nix-store --query --requisites /run/current-system | wc -l
|
||||
const result = try std.process.run(allocator, io, .{ .argv = &[_][]const u8{
|
||||
const result = try std.process.run(gpa, io, .{ .argv = &[_][]const u8{
|
||||
"sh",
|
||||
"-c",
|
||||
"nix-store --query --requisites /run/current-system | wc -l",
|
||||
} });
|
||||
|
||||
const result_trimmed = std.mem.trim(u8, result.stdout, "\n");
|
||||
defer allocator.free(result.stdout);
|
||||
defer allocator.free(result.stderr);
|
||||
defer gpa.free(result.stdout);
|
||||
defer gpa.free(result.stderr);
|
||||
|
||||
count = try std.fmt.parseInt(usize, result_trimmed, 10);
|
||||
|
||||
try writeNixCache(allocator, io, environ, hash_hex, count);
|
||||
try writeNixCache(gpa, io, environ, hash_hex, count);
|
||||
|
||||
return count;
|
||||
},
|
||||
@@ -134,31 +134,31 @@ fn countNixPackages(allocator: std.mem.Allocator, io: std.Io, environ: std.proce
|
||||
return count;
|
||||
}
|
||||
|
||||
fn getNixCachePath(allocator: std.mem.Allocator, environ: std.process.Environ) ![]const u8 {
|
||||
const cache_dir_path = try getUnixCachePath(allocator, environ);
|
||||
defer allocator.free(cache_dir_path);
|
||||
return try std.fs.path.join(allocator, &.{ cache_dir_path, "zigfetch", "nix" });
|
||||
fn getNixCachePath(gpa: std.mem.Allocator, environ: std.process.Environ) ![]const u8 {
|
||||
const cache_dir_path = try getUnixCachePath(gpa, environ);
|
||||
defer gpa.free(cache_dir_path);
|
||||
return try std.fs.path.join(gpa, &.{ cache_dir_path, "zigfetch", "nix" });
|
||||
}
|
||||
|
||||
fn getUnixCachePath(allocator: std.mem.Allocator, environ: std.process.Environ) ![]const u8 {
|
||||
var cache_dir_path = std.process.Environ.getAlloc(environ, allocator, "XDG_CACHE_HOME") catch try allocator.dupe(u8, "");
|
||||
fn getUnixCachePath(gpa: std.mem.Allocator, environ: std.process.Environ) ![]const u8 {
|
||||
var cache_dir_path = std.process.Environ.getAlloc(environ, gpa, "XDG_CACHE_HOME") catch try gpa.dupe(u8, "");
|
||||
if (cache_dir_path.len == 0) {
|
||||
allocator.free(cache_dir_path);
|
||||
const home = try std.process.Environ.getAlloc(environ, allocator, "HOME");
|
||||
defer allocator.free(home);
|
||||
cache_dir_path = try std.fs.path.join(allocator, &.{ home, ".cache" });
|
||||
gpa.free(cache_dir_path);
|
||||
const home = try std.process.Environ.getAlloc(environ, gpa, "HOME");
|
||||
defer gpa.free(home);
|
||||
cache_dir_path = try std.fs.path.join(gpa, &.{ home, ".cache" });
|
||||
}
|
||||
|
||||
return cache_dir_path;
|
||||
}
|
||||
|
||||
fn writeNixCache(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ, hash: []const u8, count: usize) !void {
|
||||
const nix_cache_dir_path = try getNixCachePath(allocator, environ);
|
||||
defer allocator.free(nix_cache_dir_path);
|
||||
fn writeNixCache(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ, hash: []const u8, count: usize) !void {
|
||||
const nix_cache_dir_path = try getNixCachePath(gpa, environ);
|
||||
defer gpa.free(nix_cache_dir_path);
|
||||
|
||||
std.Io.Dir.accessAbsolute(io, nix_cache_dir_path, .{ .read = true }) catch {
|
||||
const cache_dir_path = try getUnixCachePath(allocator, environ);
|
||||
defer allocator.free(cache_dir_path);
|
||||
const cache_dir_path = try getUnixCachePath(gpa, environ);
|
||||
defer gpa.free(cache_dir_path);
|
||||
|
||||
const cache_dir = try std.Io.Dir.openDirAbsolute(io, cache_dir_path, .{});
|
||||
try cache_dir.createDirPath(io, "zigfetch/nix");
|
||||
@@ -169,22 +169,22 @@ fn writeNixCache(allocator: std.mem.Allocator, io: std.Io, environ: std.process.
|
||||
var cache_file = try nix_cache_dir.createFile(io, "nix_cache", .{ .truncate = true });
|
||||
defer cache_file.close(io);
|
||||
|
||||
const cache_content = try std.fmt.allocPrint(allocator, "{s}\n{d}\n", .{ hash, count });
|
||||
defer allocator.free(cache_content);
|
||||
const cache_content = try std.fmt.allocPrint(gpa, "{s}\n{d}\n", .{ hash, count });
|
||||
defer gpa.free(cache_content);
|
||||
try cache_file.writePositionalAll(io, cache_content, 0);
|
||||
}
|
||||
|
||||
fn checkNixCache(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ, hash: []const u8) !usize {
|
||||
const cache_dir_path = try getNixCachePath(allocator, environ);
|
||||
defer allocator.free(cache_dir_path);
|
||||
fn checkNixCache(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ, hash: []const u8) !usize {
|
||||
const cache_dir_path = try getNixCachePath(gpa, environ);
|
||||
defer gpa.free(cache_dir_path);
|
||||
var cache_dir = try std.Io.Dir.openDirAbsolute(io, cache_dir_path, .{});
|
||||
defer cache_dir.close(io);
|
||||
|
||||
var cache_file = try cache_dir.openFile(io, "nix_cache", .{ .mode = .read_only });
|
||||
defer cache_file.close(io);
|
||||
const cache_size = (try cache_file.stat(io)).size;
|
||||
const cache_content = try utils.readFile(allocator, io, cache_file, cache_size);
|
||||
defer allocator.free(cache_content);
|
||||
const cache_content = try utils.readFile(gpa, io, cache_file, cache_size);
|
||||
defer gpa.free(cache_content);
|
||||
|
||||
var cache_iter = std.mem.splitScalar(u8, cache_content, '\n');
|
||||
|
||||
@@ -197,14 +197,14 @@ fn checkNixCache(allocator: std.mem.Allocator, io: std.Io, environ: std.process.
|
||||
return std.fmt.parseInt(usize, cache_iter.next().?, 10);
|
||||
}
|
||||
|
||||
fn countDpkgPackages(allocator: std.mem.Allocator, io: std.Io) !usize {
|
||||
fn countDpkgPackages(gpa: std.mem.Allocator, io: std.Io) !usize {
|
||||
const dpkg_status_path = "/var/lib/dpkg/status";
|
||||
const dpkg_file = try std.Io.Dir.openFileAbsolute(io, dpkg_status_path, .{ .mode = .read_only });
|
||||
defer dpkg_file.close(io);
|
||||
const file_size = (try dpkg_file.stat(io)).size;
|
||||
|
||||
const content = try utils.readFile(allocator, io, dpkg_file, file_size);
|
||||
defer allocator.free(content);
|
||||
const content = try utils.readFile(gpa, io, dpkg_file, file_size);
|
||||
defer gpa.free(content);
|
||||
|
||||
var count: usize = 0;
|
||||
var iter = std.mem.splitSequence(u8, content, "\n\n");
|
||||
@@ -222,7 +222,7 @@ fn countPacmanPackages(io: std.Io) !usize {
|
||||
return try utils.countEntries(io, "/var/lib/pacman/local") - 1;
|
||||
}
|
||||
|
||||
fn countXbpsPackages(allocator: std.mem.Allocator, io: std.Io) !usize {
|
||||
fn countXbpsPackages(gpa: std.mem.Allocator, io: std.Io) !usize {
|
||||
var dir = try std.Io.Dir.openDirAbsolute(io, "/var/db/xbps/", .{ .iterate = true });
|
||||
defer dir.close(io);
|
||||
|
||||
@@ -236,8 +236,8 @@ fn countXbpsPackages(allocator: std.mem.Allocator, io: std.Io) !usize {
|
||||
defer pkgdb_file.close(io);
|
||||
const file_size = (try pkgdb_file.stat(io)).size;
|
||||
|
||||
const content = try utils.readFile(allocator, io, pkgdb_file, file_size);
|
||||
defer allocator.free(content);
|
||||
const content = try utils.readFile(gpa, io, pkgdb_file, file_size);
|
||||
defer gpa.free(content);
|
||||
|
||||
var file_iter = std.mem.splitSequence(u8, content, "<string>installed</string>");
|
||||
// TODO: find a way to avoid this loop
|
||||
|
||||
@@ -16,18 +16,18 @@ pub const KernelInfo = struct {
|
||||
kernel_release: []u8,
|
||||
};
|
||||
|
||||
pub fn getHostname(allocator: std.mem.Allocator) ![]u8 {
|
||||
pub fn getHostname(gpa: std.mem.Allocator) ![]u8 {
|
||||
var buf: [std.posix.HOST_NAME_MAX]u8 = undefined;
|
||||
const hostnameEnv = try std.posix.gethostname(&buf);
|
||||
|
||||
const hostname = try allocator.dupe(u8, hostnameEnv);
|
||||
const hostname = try gpa.dupe(u8, hostnameEnv);
|
||||
|
||||
return hostname;
|
||||
}
|
||||
|
||||
pub fn getLocale(allocator: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const locale = std.process.Environ.getAlloc(environ, allocator, "LANG") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
pub fn getLocale(gpa: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const locale = std.process.Environ.getAlloc(environ, gpa, "LANG") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return gpa.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
return locale;
|
||||
}
|
||||
@@ -64,25 +64,25 @@ pub fn getSystemUptime() !SystemUptime {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
||||
pub fn getKernelInfo(gpa: std.mem.Allocator) !KernelInfo {
|
||||
var uts: c_utsname.struct_utsname = undefined;
|
||||
if (c_utsname.uname(&uts) != 0) {
|
||||
return error.UnameFailed;
|
||||
}
|
||||
|
||||
return KernelInfo{
|
||||
.kernel_name = try allocator.dupe(u8, std.mem.sliceTo(&uts.sysname, 0)),
|
||||
.kernel_release = try allocator.dupe(u8, std.mem.sliceTo(&uts.release, 0)),
|
||||
.kernel_name = try gpa.dupe(u8, std.mem.sliceTo(&uts.sysname, 0)),
|
||||
.kernel_release = try gpa.dupe(u8, std.mem.sliceTo(&uts.release, 0)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getOsInfo(allocator: std.mem.Allocator, io: std.Io) ![]u8 {
|
||||
pub fn getOsInfo(gpa: std.mem.Allocator, io: std.Io) ![]u8 {
|
||||
const os_release_path = "/etc/os-release";
|
||||
const os_release_file = try std.Io.Dir.cwd().openFile(io, os_release_path, .{ .mode = .read_only });
|
||||
defer os_release_file.close(io);
|
||||
const size = (try os_release_file.stat(io)).size;
|
||||
const os_release_data = try utils.readFile(allocator, io, os_release_file, size);
|
||||
defer allocator.free(os_release_data);
|
||||
const os_release_data = try utils.readFile(gpa, io, os_release_file, size);
|
||||
defer gpa.free(os_release_data);
|
||||
|
||||
var pretty_name: ?[]const u8 = null;
|
||||
|
||||
@@ -98,10 +98,10 @@ pub fn getOsInfo(allocator: std.mem.Allocator, io: std.Io) ![]u8 {
|
||||
}
|
||||
}
|
||||
|
||||
return try allocator.dupe(u8, pretty_name orelse "Unknown");
|
||||
return try gpa.dupe(u8, pretty_name orelse "Unknown");
|
||||
}
|
||||
|
||||
pub fn getWindowManagerInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u8 {
|
||||
pub fn getWindowManagerInfo(gpa: std.mem.Allocator, io: std.Io) ![]const u8 {
|
||||
var dir = try std.Io.Dir.cwd().openDir(io, "/proc/", .{ .iterate = true });
|
||||
defer dir.close(io);
|
||||
|
||||
@@ -121,8 +121,8 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u
|
||||
defer file.close(io);
|
||||
|
||||
// NOTE: https://stackoverflow.com/questions/23534263/what-is-the-maximum-allowed-limit-on-the-length-of-a-process-name
|
||||
const proc_name = try utils.readFile(allocator, io, file, 16);
|
||||
defer allocator.free(proc_name);
|
||||
const proc_name = try utils.readFile(gpa, io, file, 16);
|
||||
defer gpa.free(proc_name);
|
||||
|
||||
const proc_name_trimmed = std.mem.trim(u8, proc_name, "\n");
|
||||
|
||||
@@ -143,7 +143,7 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u
|
||||
|
||||
inline for (supported_wms) |wm| {
|
||||
if (std.ascii.eqlIgnoreCase(wm, proc_name_trimmed)) {
|
||||
break :outer try allocator.dupe(u8, proc_name_trimmed);
|
||||
break :outer try gpa.dupe(u8, proc_name_trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,5 +151,5 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u
|
||||
break :outer null;
|
||||
};
|
||||
|
||||
return wm_name orelse allocator.dupe(u8, "Unknown");
|
||||
return wm_name orelse gpa.dupe(u8, "Unknown");
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn getUsername(allocator: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
return try std.process.Environ.getAlloc(environ, allocator, "USER");
|
||||
pub fn getUsername(gpa: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
return try std.process.Environ.getAlloc(environ, gpa, "USER");
|
||||
}
|
||||
|
||||
pub fn getShell(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ) ![]u8 {
|
||||
const shell = std.process.Environ.getAlloc(environ, allocator, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
pub fn getShell(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ) ![]u8 {
|
||||
const shell = std.process.Environ.getAlloc(environ, gpa, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return gpa.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
|
||||
defer allocator.free(shell);
|
||||
defer gpa.free(shell);
|
||||
|
||||
const result = try std.process.run(allocator, io, .{ .argv = &[_][]const u8{ shell, "--version" } });
|
||||
const result = try std.process.run(gpa, io, .{ .argv = &[_][]const u8{ shell, "--version" } });
|
||||
const result_stdout = result.stdout;
|
||||
|
||||
if (std.mem.indexOf(u8, shell, "bash") != null) {
|
||||
const bash_version = parseBashVersion(result_stdout);
|
||||
defer allocator.free(result_stdout);
|
||||
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
|
||||
defer gpa.free(result_stdout);
|
||||
return try std.fmt.allocPrint(gpa, "{s} {s}", .{ "bash", bash_version.? });
|
||||
}
|
||||
|
||||
return result_stdout;
|
||||
@@ -34,9 +34,9 @@ fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
|
||||
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.? + 1];
|
||||
}
|
||||
|
||||
pub fn getTerminalName(allocator: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const term_program = std.process.Environ.getAlloc(environ, allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
pub fn getTerminalName(gpa: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const term_program = std.process.Environ.getAlloc(environ, gpa, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return gpa.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
return term_program;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ pub const DiskInfo = struct {
|
||||
disk_usage_percentage: u8,
|
||||
};
|
||||
|
||||
pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
||||
pub fn getCpuInfo(gpa: std.mem.Allocator) !CpuInfo {
|
||||
var size: usize = 0;
|
||||
|
||||
// First call to sysctlbyname to get the size of the string
|
||||
@@ -50,8 +50,8 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
||||
return error.FailedToGetCpuNameSize;
|
||||
}
|
||||
|
||||
const cpu_name: []u8 = try allocator.alloc(u8, size - 1);
|
||||
errdefer allocator.free(cpu_name);
|
||||
const cpu_name: []u8 = try gpa.alloc(u8, size - 1);
|
||||
errdefer gpa.free(cpu_name);
|
||||
|
||||
// Second call to sysctlbyname to get the CPU name
|
||||
if (c_sysctl.sysctlbyname("machdep.cpu.brand_string", cpu_name.ptr, &size, null, 0) != 0) {
|
||||
@@ -66,8 +66,8 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
||||
}
|
||||
|
||||
// Get cpu architecture
|
||||
const arch: []u8 = try getCpuArch(allocator);
|
||||
defer allocator.free(arch);
|
||||
const arch: []u8 = try getCpuArch(gpa);
|
||||
defer gpa.free(arch);
|
||||
|
||||
var cpu_freq_mhz: f64 = 0.0;
|
||||
|
||||
@@ -82,22 +82,22 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
||||
return CpuInfo{ .cpu_name = cpu_name, .cpu_cores = n_cpu, .cpu_max_freq = cpu_freq_ghz };
|
||||
}
|
||||
|
||||
fn getCpuArch(allocator: std.mem.Allocator) ![]u8 {
|
||||
fn getCpuArch(gpa: std.mem.Allocator) ![]u8 {
|
||||
var size: usize = 0;
|
||||
|
||||
if (c_sysctl.sysctlbyname("hw.machine", null, &size, null, 0) != 0) {
|
||||
return error.SysctlbynameFailed;
|
||||
}
|
||||
|
||||
const machine: []u8 = try allocator.alloc(u8, size);
|
||||
const machine: []u8 = try gpa.alloc(u8, size);
|
||||
|
||||
if (c_sysctl.sysctlbyname("hw.machine", machine.ptr, &size, null, 0) != 0) {
|
||||
return error.SysctlbynameFailed;
|
||||
}
|
||||
|
||||
defer allocator.free(machine);
|
||||
defer gpa.free(machine);
|
||||
|
||||
return allocator.dupe(u8, std.mem.sliceTo(machine, 0));
|
||||
return gpa.dupe(u8, std.mem.sliceTo(machine, 0));
|
||||
}
|
||||
|
||||
fn getCpuFreqAppleSilicon() !f64 {
|
||||
@@ -184,11 +184,11 @@ pub fn getCpuFreqIntel() f64 {
|
||||
return freq / 1_000_000.0;
|
||||
}
|
||||
|
||||
pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo {
|
||||
pub fn getGpuInfo(gpa: std.mem.Allocator) !GpuInfo {
|
||||
// TODO: add support for non-Apple Silicon Macs
|
||||
|
||||
var gpu_info = GpuInfo{
|
||||
.gpu_name = try allocator.dupe(u8, "Unknown"),
|
||||
.gpu_name = try gpa.dupe(u8, "Unknown"),
|
||||
.gpu_cores = 0,
|
||||
.gpu_freq = 0.0,
|
||||
};
|
||||
@@ -236,11 +236,11 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo {
|
||||
|
||||
if (c_iokit.CFDictionaryGetValueIfPresent(@as(c_iokit.CFDictionaryRef, @ptrCast(properties_ptr)), model_key, &name_ref) == c_iokit.TRUE) {
|
||||
if (c_iokit.CFGetTypeID(name_ref) == c_iokit.CFStringGetTypeID()) {
|
||||
const accel_name = utils.cfTypeRefToZigString(allocator, name_ref) catch {
|
||||
const accel_name = utils.cfTypeRefToZigString(gpa, name_ref) catch {
|
||||
return gpu_info;
|
||||
};
|
||||
|
||||
allocator.free(gpu_info.gpu_name);
|
||||
gpa.free(gpu_info.gpu_name);
|
||||
gpu_info.gpu_name = accel_name;
|
||||
}
|
||||
}
|
||||
@@ -262,8 +262,8 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo {
|
||||
}
|
||||
|
||||
// Get cpu architecture
|
||||
const arch: []u8 = try getCpuArch(allocator);
|
||||
defer allocator.free(arch);
|
||||
const arch: []u8 = try getCpuArch(gpa);
|
||||
defer gpa.free(arch);
|
||||
|
||||
var gpu_freq_mhz: f64 = 0.0;
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ pub const NetInfo = struct {
|
||||
ipv4_addr: []u8,
|
||||
};
|
||||
|
||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||
var net_info_list = std.array_list.Managed(NetInfo).init(allocator);
|
||||
pub fn getNetInfo(gpa: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||
var net_info_list = std.array_list.Managed(NetInfo).init(gpa);
|
||||
|
||||
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
||||
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
||||
@@ -35,8 +35,8 @@ pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo)
|
||||
const ip_str = c_inet.inet_ntop(c_inet.AF_INET, &addr_in.sin_addr, &ip_buf, c_inet.INET_ADDRSTRLEN);
|
||||
if (ip_str) |ip| {
|
||||
try net_info_list.append(NetInfo{
|
||||
.interface_name = try allocator.dupe(u8, std.mem.span(ifa.ifa_name)),
|
||||
.ipv4_addr = try allocator.dupe(u8, std.mem.span(ip)),
|
||||
.interface_name = try gpa.dupe(u8, std.mem.span(ifa.ifa_name)),
|
||||
.ipv4_addr = try gpa.dupe(u8, std.mem.span(ip)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const std = @import("std");
|
||||
const utils = @import("../utils.zig");
|
||||
|
||||
pub fn getPackagesInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u8 {
|
||||
var packages_info = std.array_list.Managed(u8).init(allocator);
|
||||
pub fn getPackagesInfo(gpa: std.mem.Allocator, io: std.Io) ![]const u8 {
|
||||
var packages_info = std.array_list.Managed(u8).init(gpa);
|
||||
defer packages_info.deinit();
|
||||
|
||||
const homebrew_packages = countHomebrewPackages(io) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
@@ -23,7 +23,7 @@ pub fn getPackagesInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u8 {
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " macports: {d}", .{macports_packages}));
|
||||
}
|
||||
|
||||
return try allocator.dupe(u8, packages_info.items);
|
||||
return try gpa.dupe(u8, packages_info.items);
|
||||
}
|
||||
|
||||
fn countHomebrewPackages(io: std.Io) !usize {
|
||||
|
||||
@@ -16,18 +16,18 @@ pub const KernelInfo = struct {
|
||||
};
|
||||
|
||||
/// Returns the hostname.
|
||||
pub fn getHostname(allocator: std.mem.Allocator) ![]u8 {
|
||||
pub fn getHostname(gpa: std.mem.Allocator) ![]u8 {
|
||||
var buf: [std.posix.HOST_NAME_MAX]u8 = undefined;
|
||||
const hostnameEnv = try std.posix.gethostname(&buf);
|
||||
|
||||
const hostname = try allocator.dupe(u8, hostnameEnv);
|
||||
const hostname = try gpa.dupe(u8, hostnameEnv);
|
||||
|
||||
return hostname;
|
||||
}
|
||||
|
||||
pub fn getLocale(allocator: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const locale = std.process.Environ.getAlloc(environ, allocator, "LANG") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
pub fn getLocale(gpa: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const locale = std.process.Environ.getAlloc(environ, gpa, "LANG") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return gpa.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
return locale;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ pub fn getSystemUptime(io: std.Io) !SystemUptime {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
||||
pub fn getKernelInfo(gpa: std.mem.Allocator) !KernelInfo {
|
||||
var size: usize = 0;
|
||||
|
||||
// --- KERNEL NAME ---
|
||||
@@ -80,8 +80,8 @@ pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
||||
return error.FailedToGetKernelNameSize;
|
||||
}
|
||||
|
||||
const kernel_type: []u8 = try allocator.alloc(u8, size - 1);
|
||||
errdefer allocator.free(kernel_type);
|
||||
const kernel_type: []u8 = try gpa.alloc(u8, size - 1);
|
||||
errdefer gpa.free(kernel_type);
|
||||
|
||||
// Second call to sysctlbyname to get the kernel name
|
||||
if (c_sysctl.sysctlbyname("kern.ostype", kernel_type.ptr, &size, null, 0) != 0) {
|
||||
@@ -94,8 +94,8 @@ pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
||||
return error.FailedToGetKernelReleaseSize;
|
||||
}
|
||||
|
||||
const os_release: []u8 = try allocator.alloc(u8, size - 1);
|
||||
errdefer allocator.free(os_release);
|
||||
const os_release: []u8 = try gpa.alloc(u8, size - 1);
|
||||
errdefer gpa.free(os_release);
|
||||
|
||||
// Second call to sysctlbyname to get the kernel release
|
||||
if (c_sysctl.sysctlbyname("kern.osrelease", os_release.ptr, &size, null, 0) != 0) {
|
||||
@@ -108,7 +108,7 @@ pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getOsInfo(allocator: std.mem.Allocator) ![]u8 {
|
||||
pub fn getOsInfo(gpa: std.mem.Allocator) ![]u8 {
|
||||
var size: usize = 0;
|
||||
|
||||
// First call to sysctlbyname to get the size of the string
|
||||
@@ -116,20 +116,20 @@ pub fn getOsInfo(allocator: std.mem.Allocator) ![]u8 {
|
||||
return error.FailedToGetCpuNameSize;
|
||||
}
|
||||
|
||||
const os_version: []u8 = try allocator.alloc(u8, size - 1);
|
||||
defer allocator.free(os_version);
|
||||
const os_version: []u8 = try gpa.alloc(u8, size - 1);
|
||||
defer gpa.free(os_version);
|
||||
|
||||
// Second call to sysctlbyname to get the os version
|
||||
if (c_sysctl.sysctlbyname("kern.osproductversion", os_version.ptr, &size, null, 0) != 0) {
|
||||
return error.FailedToGetOsVersion;
|
||||
}
|
||||
|
||||
const os_info = try std.fmt.allocPrint(allocator, "macOS {s}", .{os_version});
|
||||
const os_info = try std.fmt.allocPrint(gpa, "macOS {s}", .{os_version});
|
||||
|
||||
return os_info;
|
||||
}
|
||||
|
||||
pub fn getWindowManagerInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
pub fn getWindowManagerInfo(gpa: std.mem.Allocator) ![]const u8 {
|
||||
var name = [_]c_int{ c_sysctl.CTL_KERN, c_sysctl.KERN_PROC, c_sysctl.KERN_PROC_ALL };
|
||||
var size: usize = 0;
|
||||
|
||||
@@ -138,8 +138,8 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
return error.SysctlFailed;
|
||||
}
|
||||
|
||||
const buffer: []u8 = try allocator.alloc(u8, size);
|
||||
defer allocator.free(buffer);
|
||||
const buffer: []u8 = try gpa.alloc(u8, size);
|
||||
defer gpa.free(buffer);
|
||||
|
||||
// Second call to retrieve process data
|
||||
if (c_sysctl.sysctl(&name, name.len, buffer.ptr, &size, null, 0) != 0) {
|
||||
@@ -173,8 +173,8 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
var pathbuf: [c_libproc.PROC_PIDPATHINFO_MAXSIZE]u8 = undefined;
|
||||
// c_libproc.proc_pidpath saves the process name in `pathbuf` and returns the len
|
||||
const path_len = @as(usize, @intCast(c_libproc.proc_pidpath(pid, &pathbuf, pathbuf.len)));
|
||||
const proc_pathname = if (path_len > 0) try allocator.dupe(u8, pathbuf[0..@intCast(path_len)]) else try allocator.dupe(u8, "unknown");
|
||||
defer allocator.free(proc_pathname);
|
||||
const proc_pathname = if (path_len > 0) try gpa.dupe(u8, pathbuf[0..@intCast(path_len)]) else try gpa.dupe(u8, "unknown");
|
||||
defer gpa.free(proc_pathname);
|
||||
|
||||
inline for (supported_wms) |wm| {
|
||||
if (std.ascii.endsWithIgnoreCase(proc_pathname, wm)) {
|
||||
@@ -183,7 +183,7 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
else
|
||||
proc_pathname;
|
||||
|
||||
break :outer try allocator.dupe(u8, basename);
|
||||
break :outer try gpa.dupe(u8, basename);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,5 +191,5 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
break :outer null;
|
||||
};
|
||||
|
||||
return wm_name orelse allocator.dupe(u8, "Quartz Compositor");
|
||||
return wm_name orelse gpa.dupe(u8, "Quartz Compositor");
|
||||
}
|
||||
|
||||
@@ -3,24 +3,24 @@ const std = @import("std");
|
||||
/// Returns the current logged-in user's username.
|
||||
/// Uses the environment variable `USER`.
|
||||
/// The caller is responsible for freeing the allocated memory.
|
||||
pub fn getUsername(allocator: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
return try std.process.Environ.getAlloc(environ, allocator, "USER");
|
||||
pub fn getUsername(gpa: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
return try std.process.Environ.getAlloc(environ, gpa, "USER");
|
||||
}
|
||||
|
||||
pub fn getShell(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ) ![]u8 {
|
||||
const shell = std.process.Environ.getAlloc(environ, allocator, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
pub fn getShell(gpa: std.mem.Allocator, io: std.Io, environ: std.process.Environ) ![]u8 {
|
||||
const shell = std.process.Environ.getAlloc(environ, gpa, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return gpa.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
|
||||
defer allocator.free(shell);
|
||||
defer gpa.free(shell);
|
||||
|
||||
const result = try std.process.run(allocator, io, .{ .argv = &[_][]const u8{ shell, "--version" } });
|
||||
const result = try std.process.run(gpa, io, .{ .argv = &[_][]const u8{ shell, "--version" } });
|
||||
const result_stdout = result.stdout;
|
||||
|
||||
if (std.mem.indexOf(u8, shell, "bash") != null) {
|
||||
const bash_version = parseBashVersion(result_stdout);
|
||||
defer allocator.free(result_stdout);
|
||||
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
|
||||
defer gpa.free(result_stdout);
|
||||
return try std.fmt.allocPrint(gpa, "{s} {s}", .{ "bash", bash_version.? });
|
||||
}
|
||||
|
||||
return result_stdout;
|
||||
@@ -37,9 +37,9 @@ fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
|
||||
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
|
||||
}
|
||||
|
||||
pub fn getTerminalName(allocator: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const term_program = std.process.Environ.getAlloc(environ, allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
pub fn getTerminalName(gpa: std.mem.Allocator, environ: std.process.Environ) ![]u8 {
|
||||
const term_program = std.process.Environ.getAlloc(environ, gpa, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return gpa.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
return term_program;
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@ const std = @import("std");
|
||||
const c_iokit = @cImport(@cInclude("IOKit/IOKitLib.h"));
|
||||
|
||||
/// Converts a CFTypeRef casted to CFStringRef to a Zig string.
|
||||
pub fn cfTypeRefToZigString(allocator: std.mem.Allocator, cf_type_ref: c_iokit.CFTypeRef) ![]u8 {
|
||||
pub fn cfTypeRefToZigString(gpa: std.mem.Allocator, cf_type_ref: c_iokit.CFTypeRef) ![]u8 {
|
||||
const cf_string: c_iokit.CFStringRef = @ptrFromInt(@intFromPtr(cf_type_ref));
|
||||
|
||||
const length = c_iokit.CFStringGetLength(cf_string);
|
||||
const max_size = c_iokit.CFStringGetMaximumSizeForEncoding(length, c_iokit.kCFStringEncodingUTF8) + 1;
|
||||
const max_size_usize = @as(usize, @intCast(max_size));
|
||||
|
||||
const buffer = try allocator.alloc(u8, max_size_usize);
|
||||
errdefer allocator.free(buffer);
|
||||
const buffer = try gpa.alloc(u8, max_size_usize);
|
||||
errdefer gpa.free(buffer);
|
||||
|
||||
if (c_iokit.CFStringGetCString(cf_string, buffer.ptr, @as(c_iokit.CFIndex, @intCast(buffer.len)), c_iokit.kCFStringEncodingUTF8) == c_iokit.FALSE) {
|
||||
return error.StringConversionFailed;
|
||||
@@ -21,5 +21,5 @@ pub fn cfTypeRefToZigString(allocator: std.mem.Allocator, cf_type_ref: c_iokit.C
|
||||
actual_len += 1;
|
||||
}
|
||||
|
||||
return allocator.realloc(buffer, actual_len);
|
||||
return gpa.realloc(buffer, actual_len);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ const formatters = @import("formatters.zig");
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const io = init.io;
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
// TODO: rename all the paramenters 'allocator' in 'gpa'
|
||||
const allocator = gpa.allocator();
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
|
||||
@@ -115,15 +115,15 @@ test "getLongestAsciiArtRowLen" {
|
||||
try std.testing.expectEqual(40, try getLongestAsciiArtRowLen(rows[0..]));
|
||||
}
|
||||
|
||||
pub fn readFile(allocator: std.mem.Allocator, io: std.Io, file: std.Io.File, size: usize) ![]const u8 {
|
||||
var file_buf = try allocator.alloc(u8, size);
|
||||
defer allocator.free(file_buf);
|
||||
pub fn readFile(gpa: std.mem.Allocator, io: std.Io, file: std.Io.File, size: usize) ![]const u8 {
|
||||
var file_buf = try gpa.alloc(u8, size);
|
||||
defer gpa.free(file_buf);
|
||||
|
||||
const read = try file.readPositionalAll(io, file_buf, 0);
|
||||
|
||||
const data = file_buf[0..read];
|
||||
|
||||
return allocator.dupe(u8, data);
|
||||
return gpa.dupe(u8, data);
|
||||
}
|
||||
|
||||
pub fn countEntries(io: std.Io, dir_path: []const u8) !usize {
|
||||
|
||||
Reference in New Issue
Block a user