12 Commits

Author SHA1 Message Date
utox39
e88586c47a Merge pull request #4 from utox39/feat-linux/gpu-name-parsing
Feat linux/gpu name parsing
2025-07-17 15:39:23 +02:00
utox39
28f94d87ed fix(terminal): fix typo 2025-07-17 15:37:31 +02:00
utox39
0d7f606b7d build: bump to version 0.7.0 2025-07-17 15:35:37 +02:00
utox39
ec0e3ffd18 refactor(linux-gpu): add note and remove debug logs 2025-07-17 15:34:21 +02:00
utox39
f73b1be023 refactor(linux-gpu): use gpu name parser 2025-07-17 15:33:43 +02:00
utox39
3a5a268bbe feat(linux-gpu): parse gpu name 2025-07-17 02:25:26 +02:00
utox39
bdf4a3f7d7 build: bump version to 0.6.2 2025-07-17 02:18:36 +02:00
utox39
68cc541575 refactor(formatters): show the frequency and cores of the GPU only if one of the two is different from zero 2025-07-16 23:57:09 +02:00
utox39
e28a3b96e5 build: bump version to 0.6.1 2025-07-15 02:20:55 +02:00
utox39
01ad39c0b5 fix(config): fix the order of the enum 2025-07-15 02:19:56 +02:00
utox39
a18fd7ade8 style(ascii): add comments 2025-07-15 02:07:07 +02:00
utox39
3e14691e14 docs(README): update README 2025-07-12 02:03:24 +02:00
8 changed files with 52 additions and 12 deletions

View File

@@ -18,7 +18,7 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe
### Linux only
- [libpci](https://github.com/pciutils/pciutils) (linux only)
- [libpci](https://github.com/pciutils/pciutils)
## Installation

View File

@@ -10,7 +10,7 @@
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.6.0",
.version = "0.7.0",
// Together with name, this represents a globally unique package
// identifier. This field is generated by the Zig toolchain when the

View File

@@ -105,11 +105,13 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
if (i < sys_info_len) {
try stdout.print("{s}\n", .{sys_info_items[i]});
} else if (i == sys_info_len + 1) {
// Print the first row of colors
for (0..8) |j| {
try stdout.print("\x1b[48;5;{d}m \x1b[0m", .{j});
}
try stdout.print("\n", .{});
} else if (i == sys_info_len + 2) {
// Print the second row of colors
for (8..16) |j| {
try stdout.print("\x1b[48;5;{d}m \x1b[0m", .{j});
}

View File

@@ -23,9 +23,9 @@ pub const ModuleType = enum {
ram,
swap,
disk,
net,
terminal,
locale,
net, // It must always be the last element of this array
};
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.ArrayList(ModuleType) {

View File

@@ -124,21 +124,28 @@ pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) !Result {
}
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 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) {
var formatted_gpu_info_list = std.ArrayList([]u8).init(allocator);
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 }));
var formatted_gpu_info: []u8 = undefined;
if ((g.gpu_cores == 0) or (g.gpu_freq == 0.0)) {
formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, g.gpu_name });
} else {
formatted_gpu_info = 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 });
}
try formatted_gpu_info_list.append(formatted_gpu_info);
allocator.free(g.gpu_name);
}
gpu_info_list.deinit();
return Result{ .string_arraylist = formatted_gpu_info };
return Result{ .string_arraylist = formatted_gpu_info_list };
}
}

View File

@@ -114,8 +114,13 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
devices.*.device_id,
);
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
defer allocator.free(gpu_name);
const parsed_gpu_name = try parseGpuName(allocator, gpu_name);
try gpu_info_list.append(GpuInfo{
.gpu_name = try allocator.dupe(u8, std.mem.span(name)),
.gpu_name = parsed_gpu_name,
.gpu_cores = 0,
.gpu_freq = 0.0,
});
@@ -133,6 +138,32 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
return gpu_info_list;
}
fn parseGpuName(allocator: 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);
_ = 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);
_ = 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);
_ = std.mem.replace(u8, name, "NVIDIA Corporation", "NVIDIA", parsed_gpu_name);
return parsed_gpu_name;
}
return name;
}
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
// Reads /proc/meminfo
const meminfo_path = "/proc/meminfo";

View File

@@ -24,8 +24,8 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
}
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
const term_progrm = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
return allocator.dupe(u8, "Unknown");
} else return err;
return term_progrm;
return term_program;
}

View File

@@ -27,8 +27,8 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
}
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
const term_progrm = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
return allocator.dupe(u8, "Unknown");
} else return err;
return term_progrm;
return term_program;
}