35 Commits

Author SHA1 Message Date
utox39
3bcfff5bdc build: bump version to 0.9.1 2025-08-06 20:57:27 +02:00
utox39
42edc5c24b fix(utils): fix typo 2025-08-06 20:57:10 +02:00
utox39
537a324275 build: bump version to 0.9.0 2025-08-06 00:03:00 +02:00
utox39
224a7d29d8 Merge pull request #11 from utox39/feat/parse-shell-version
Feat/parse shell version
2025-08-06 00:01:34 +02:00
utox39
eb477e5154 refactor(shell): remove the 'allocator' parameter 2025-08-05 23:13:42 +02:00
utox39
2d1c7506d8 feat(shell): parse bash shell version 2025-08-04 02:37:52 +02:00
utox39
428d882591 Merge pull request #10 from utox39/feat/handle-shell-env-var-not-found
Feat/handle shell env var not found
2025-08-04 00:40:29 +02:00
utox39
a1c62a56ae build: bump version to 0.8.1 2025-08-04 00:38:33 +02:00
utox39
5b423d1cef feat(shell): handle 'SHELL' env var not found 2025-08-04 00:35:09 +02:00
utox39
6574945e56 docs(README): update README 2025-08-02 00:25:25 +02:00
utox39
66bb544c0a build: bump version to 0.8.0 2025-08-02 00:22:06 +02:00
utox39
d90c1d2e9a Merge pull request #9 from utox39/feat/adaptive-print
Feat/adaptive print
2025-08-02 00:20:35 +02:00
utox39
0beec60a8a fix(linux-kernel): fix kernel info parsing by trimming null bytes 2025-08-01 06:25:30 +02:00
utox39
7669da5ae8 refactor(formatters): remove unnecessary spaces in the formatted uptime string 2025-08-01 05:55:11 +02:00
utox39
1267a71250 feat: print the ascii art if the width of the terminal is greater than the left alignment (45) + the longest sys info string length 2025-08-01 04:10:00 +02:00
utox39
2aa90c04ff feat: add a utility function to get the length of the longest sys info string 2025-08-01 03:51:11 +02:00
utox39
f6d1c2dc72 feat: add adaptive printing based on terminal width 2025-07-31 01:47:12 +02:00
utox39
9df0845530 feat: add a utility function to retrieve the terminal size 2025-07-31 01:43:40 +02:00
utox39
3b3c29e22d Merge pull request #8 from utox39/fix-linux-gpu/fix-memory-leak
Fix linux gpu/fix memory leak
2025-07-29 17:08:10 +02:00
utox39
a866952f86 build: bump version to 0.7.4 2025-07-29 17:06:40 +02:00
utox39
5d7bdd8041 fix(linux-gpu): fix memory leak 2025-07-29 17:05:30 +02:00
utox39
635dfe00db Merge pull request #7 from utox39/fix-linux-gpu/fix-double-free
Fix linux gpu/fix double free
2025-07-28 23:40:56 +02:00
utox39
064e8e9597 build: bump version to 0.7.3 2025-07-28 23:39:40 +02:00
utox39
785915d833 fix(linux-gpu): fix double free 2025-07-28 23:38:36 +02:00
utox39
e492443a2c build: bump version to 0.7.2 2025-07-27 14:54:36 +02:00
utox39
f1bebb08e9 Merge pull request #6 from utox39/fix/fix-wrong-used-size
fix: fix wrong used size calculation
2025-07-27 14:52:37 +02:00
utox39
f3af8712d2 Merge pull request #5 from utox39/fix-linux/fix-cpuinfo_max_freq
fix(linux-cpu): fix FileNotFound error
2025-07-27 14:50:18 +02:00
utox39
67a2e19ac4 fix: fix wrong used size calculation 2025-07-27 14:42:52 +02:00
utox39
2de5047eb7 fix(linux-cpu): fix FileNotFound error 2025-07-22 22:32:48 +02:00
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
10 changed files with 227 additions and 31 deletions

View File

@@ -69,7 +69,7 @@ $ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
## Roadtrip ## Roadtrip
- [ ] Add ASCII art for each operating system and Linux distro - [ ] Add ASCII art for each operating system and Linux distro
- [ ] Add GPU info for Linux - [x] Add GPU info for Linux
- [ ] Add packages info for Linux - [ ] Add packages info for Linux
- [x] Add user customization - [x] Add user customization
- [ ] Add support for Windows - [ ] Add support for Windows

View File

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

View File

@@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const utils = @import("./utils.zig");
pub const Reset = "\x1b[0m"; pub const Reset = "\x1b[0m";
pub const Bold = "\x1b[1m"; pub const Bold = "\x1b[1m";
@@ -89,18 +90,31 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
const ascii_art_items = ascii_art_content_list.items; const ascii_art_items = ascii_art_content_list.items;
const sys_info_items = sys_info_list.items; const sys_info_items = sys_info_list.items;
const terminal_size = try utils.getTerminalSize();
const terminal_width: usize = @intCast(terminal_size.width);
const left_alignment: usize = 45;
const longest_sys_info_string_len = utils.getLongestSysInfoStringLen(sys_info_items);
const can_print_ascii_art: bool = terminal_width > left_alignment + longest_sys_info_string_len;
const ascii_art_len: usize = ascii_art_items.len; const ascii_art_len: usize = ascii_art_items.len;
const sys_info_len: usize = sys_info_items.len; const sys_info_len: usize = sys_info_items.len;
const max_len: usize = if (ascii_art_len > sys_info_len) ascii_art_len else sys_info_len;
// NOTE: sys_info_len + 3 to be able to print the colors
const max_len: usize = if ((ascii_art_len > sys_info_len) and can_print_ascii_art) ascii_art_len else sys_info_len + 3;
var i: usize = 0; var i: usize = 0;
while (i < max_len) : (i += 1) { while (i < max_len) : (i += 1) {
// Print the ascii art if the width of the terminal is greater than the left alignment (45) + the longest sys info string length
if (can_print_ascii_art) {
if (i < ascii_art_len) { if (i < ascii_art_len) {
try stdout.print("{s:<40} \t", .{ascii_art_items[i]}); try stdout.print("{s:<45}", .{ascii_art_items[i]});
} else { } else {
try stdout.print("{s:<40}", .{""}); try stdout.print("{s:<45}", .{""});
} }
try bw.flush(); try bw.flush();
}
if (i < sys_info_len) { if (i < sys_info_len) {
try stdout.print("{s}\n", .{sys_info_items[i]}); try stdout.print("{s}\n", .{sys_info_items[i]});

View File

@@ -51,6 +51,7 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
// Parsing /proc/cpuinfo // Parsing /proc/cpuinfo
var model_name: ?[]const u8 = null; var model_name: ?[]const u8 = null;
var cpu_max_freq_mhz_str: ?[]const u8 = null;
var lines = std.mem.splitScalar(u8, cpuinfo_data, '\n'); var lines = std.mem.splitScalar(u8, cpuinfo_data, '\n');
while (lines.next()) |line| { while (lines.next()) |line| {
@@ -60,14 +61,36 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
_ = parts.next(); // discards the key _ = parts.next(); // discards the key
if (parts.next()) |value| { if (parts.next()) |value| {
model_name = std.mem.trim(u8, value, " "); model_name = std.mem.trim(u8, value, " ");
break;
} }
} else if (std.mem.startsWith(u8, trimmed, "cpu MHz") and cpu_max_freq_mhz_str == null) {
var parts = std.mem.splitScalar(u8, trimmed, ':');
_ = parts.next(); // discard the key
if (parts.next()) |value| {
cpu_max_freq_mhz_str = std.mem.trim(u8, value, " ");
} }
} }
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq if ((model_name != null) and (cpu_max_freq_mhz_str != null)) {
break;
}
}
var cpu_max_freq: f32 = 0.0;
const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
var cmf_exists: bool = true;
// Checks if /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq exists
_ = std.fs.accessAbsolute(cpuinfo_max_freq_path, .{ .mode = .read_only }) catch |err| {
if (err == std.posix.AccessError.FileNotFound) {
cmf_exists = false;
}
};
if (cmf_exists) {
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{}); var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{});
defer file2.close(); defer file2.close();
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize)); const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(cpuinfo_max_freq_data); defer allocator.free(cpuinfo_max_freq_data);
@@ -75,7 +98,13 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq // Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r"); const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r");
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed); const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
const cpu_max_freq: f32 = cpu_max_freq_khz / 1_000_000; cpu_max_freq = cpu_max_freq_khz / 1_000_000;
} else {
if (cpu_max_freq_mhz_str != null) {
const cpu_max_freq_mhz: f32 = try std.fmt.parseFloat(f32, cpu_max_freq_mhz_str.?);
cpu_max_freq = cpu_max_freq_mhz / 1_000;
}
}
return CpuInfo{ return CpuInfo{
.cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"), .cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"),
@@ -114,8 +143,20 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
devices.*.device_id, devices.*.device_id,
); );
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
const maybe_parsed_gpu_name = try parseGpuName(allocator, gpu_name);
var parsed_gpu_name: []u8 = undefined;
if (maybe_parsed_gpu_name != null) {
allocator.free(gpu_name);
parsed_gpu_name = maybe_parsed_gpu_name.?;
} else {
parsed_gpu_name = gpu_name;
}
try gpu_info_list.append(GpuInfo{ 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_cores = 0,
.gpu_freq = 0.0, .gpu_freq = 0.0,
}); });
@@ -133,6 +174,32 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
return gpu_info_list; 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 null;
}
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo { pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
// Reads /proc/meminfo // Reads /proc/meminfo
const meminfo_path = "/proc/meminfo"; const meminfo_path = "/proc/meminfo";
@@ -260,7 +327,7 @@ pub fn getDiskSize(disk_path: []const u8) !DiskInfo {
} }
const total_size = stat.f_blocks * stat.f_frsize; const total_size = stat.f_blocks * stat.f_frsize;
const free_size = stat.f_bavail * stat.f_frsize; const free_size = stat.f_bfree * stat.f_frsize;
const used_size = total_size - free_size; const used_size = total_size - free_size;
const used_size_percentage = (used_size * 100) / total_size; const used_size_percentage = (used_size * 100) / total_size;

View File

@@ -70,8 +70,8 @@ pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
} }
return KernelInfo{ return KernelInfo{
.kernel_name = try allocator.dupe(u8, &uts.sysname), .kernel_name = try allocator.dupe(u8, std.mem.sliceTo(&uts.sysname, 0)),
.kernel_release = try allocator.dupe(u8, &uts.release), .kernel_release = try allocator.dupe(u8, std.mem.sliceTo(&uts.release, 0)),
}; };
} }

View File

@@ -6,7 +6,9 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
} }
pub fn getShell(allocator: std.mem.Allocator) ![]u8 { pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
const shell = try std.process.getEnvVarOwned(allocator, "SHELL"); const shell = std.process.getEnvVarOwned(allocator, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
return allocator.dupe(u8, "Unknown");
} else return err;
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator); var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
defer allocator.free(shell); defer allocator.free(shell);
@@ -20,12 +22,29 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
_ = try child.wait(); _ = try child.wait();
if (std.mem.indexOf(u8, shell, "bash") != null) {
const bash_version = parseBashVersion(output);
defer allocator.free(output);
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
}
return output; return output;
} }
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
if (end_index == null) return null;
const version_keyword = "version ";
const version_keyword_index = std.mem.indexOf(u8, shell_version_output[0..end_index.?], version_keyword);
if (version_keyword_index == null) return null;
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
}
pub fn getTerminalName(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"); return allocator.dupe(u8, "Unknown");
} else return err; } else return err;
return term_progrm; return term_program;
} }

View File

@@ -418,7 +418,7 @@ pub fn getDiskSize(disk_path: []const u8) !DiskInfo {
} }
const total_size = stat.f_blocks * stat.f_frsize; const total_size = stat.f_blocks * stat.f_frsize;
const free_size = stat.f_bavail * stat.f_frsize; const free_size = stat.f_bfree * stat.f_frsize;
const used_size = total_size - free_size; const used_size = total_size - free_size;
const used_size_percentage = (used_size * 100) / total_size; const used_size_percentage = (used_size * 100) / total_size;

View File

@@ -9,7 +9,9 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
} }
pub fn getShell(allocator: std.mem.Allocator) ![]u8 { pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
const shell = try std.process.getEnvVarOwned(allocator, "SHELL"); const shell = std.process.getEnvVarOwned(allocator, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
return allocator.dupe(u8, "Unknown");
} else return err;
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator); var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
defer allocator.free(shell); defer allocator.free(shell);
@@ -23,12 +25,29 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
_ = try child.wait(); _ = try child.wait();
if (std.mem.indexOf(u8, shell, "bash") != null) {
const bash_version = parseBashVersion(output);
defer allocator.free(output);
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
}
return output; return output;
} }
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
if (end_index == null) return null;
const version_keyword = "version ";
const version_keyword_index = std.mem.indexOf(u8, shell_version_output[0..end_index.?], version_keyword);
if (version_keyword_index == null) return null;
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
}
pub fn getTerminalName(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"); return allocator.dupe(u8, "Unknown");
} else return err; } else return err;
return term_progrm; return term_program;
} }

77
src/utils.zig Normal file
View File

@@ -0,0 +1,77 @@
const std = @import("std");
const builtin = @import("builtin");
pub const TermSize = struct {
width: u16,
height: u16,
};
pub fn getTerminalSize() !TermSize {
// https://github.com/softprops/zig-termsize (https://github.com/softprops/zig-termsize/blob/main/src/main.zig)
const stdout = std.io.getStdOut();
switch (builtin.os.tag) {
.windows => {
var buf: std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
switch (std.os.windows.kernel32.GetConsoleScreenBufferInfo(stdout.handle, &buf)) {
std.os.windows.TRUE => return TermSize{
.width = @intCast(buf.srWindow.Right - buf.srWindow.Left + 1),
.height = @intCast(buf.srWindow.Bottom - buf.srWindow.Top + 1),
},
else => return error.GetConsoleScreenBufferInfoFailed,
}
},
.linux, .macos => {
var buf: std.posix.winsize = undefined;
switch (std.posix.errno(
std.posix.system.ioctl(
stdout.handle,
std.posix.T.IOCGWINSZ,
@intFromPtr(&buf),
),
)) {
std.posix.E.SUCCESS => return TermSize{
.width = buf.col,
.height = buf.row,
},
else => return error.IoctlFailed,
}
},
else => return error.UnsupportedOperatingSystem,
}
}
test "getTerminalSize" {
const terminal_size = try getTerminalSize();
std.debug.print("Height: {}, Width {}\n", .{ terminal_size.height, terminal_size.width });
try std.testing.expect((terminal_size.height > 0) and (terminal_size.width > 0));
}
pub fn getLongestSysInfoStringLen(strings: []const []const u8) usize {
const ansi_reset = "\x1b[0m";
var longest_len: usize = 0;
// Ignore the username@host and the separator
for (strings[2..]) |s| {
const ansi_restet_index = std.mem.indexOf(u8, s, ansi_reset);
var start: usize = 0;
if (ansi_restet_index != null) {
// `start` is the index of the last character of the ANSI reset escape sequence + 1
start = ansi_restet_index.? + ansi_reset.len + 1;
}
longest_len = @max(longest_len, s[start..].len);
}
return longest_len;
}
test "getLongestSysInfoStringLen" {
const strings = [_][]const u8{ "", "", "test", "test-test", "test1" };
try std.testing.expectEqual(strings[3].len, getLongestSysInfoStringLen(strings[0..]));
}