From 20e2ef80d2e810be3d67dc7f9302cad7e9519d44 Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 10 Sep 2025 14:21:22 +0200 Subject: [PATCH 1/6] feat: add custom ascii art --- src/ascii.zig | 19 ++++++++++++------- src/config.zig | 5 +++++ src/main.zig | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ascii.zig b/src/ascii.zig index 0ca4060..72eea58 100644 --- a/src/ascii.zig +++ b/src/ascii.zig @@ -65,18 +65,23 @@ test "parse ffffff" { try std.testing.expect((result.r == 255) and (result.g == 255) and (result.b == 255)); } -pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.array_list.Managed([]u8)) !void { +pub fn printAscii(allocator: std.mem.Allocator, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void { var stdout_buffer: [2048]u8 = undefined; var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); const stdout = &stdout_writer.interface; - // const ascii_art_path = "./assets/ascii/guy_fawks.txt"; - // var file = try std.fs.cwd().openFile(ascii_art_path, .{}); - // defer file.close(); - // const ascii_art_data = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); - // defer allocator.free(ascii_art_data); + var ascii_art_data: []const u8 = undefined; + if (ascii_art_path) |ascii| { + var ascii_file = try std.fs.cwd().openFile(ascii, .{}); + defer ascii_file.close(); + ascii_art_data = try ascii_file.readToEndAlloc(allocator, std.math.maxInt(usize)); + } else { + ascii_art_data = @embedFile("./assets/ascii/guy_fawks.txt"); + } - const ascii_art_data = @embedFile("./assets/ascii/guy_fawks.txt"); + defer if (ascii_art_path != null) { + allocator.free(ascii_art_data); + }; var lines = std.mem.splitScalar(u8, ascii_art_data, '\n'); diff --git a/src/config.zig b/src/config.zig index b690cc2..c081bbd 100644 --- a/src/config.zig +++ b/src/config.zig @@ -9,6 +9,7 @@ pub const Module = struct { }; pub const Config = struct { + ascii_abs_path: ?[]u8 = null, modules: []Module, }; @@ -29,6 +30,10 @@ pub const ModuleType = enum { custom, }; +pub fn getAsciiPath(config: ?std.json.Parsed(Config)) ?[]u8 { + return config.?.value.ascii_abs_path; +} + 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); diff --git a/src/main.zig b/src/main.zig index 102c42d..eca8fc5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -70,5 +70,5 @@ pub fn main() !void { } } - try ascii.printAscii(allocator, sys_info_list); + try ascii.printAscii(allocator, config.getAsciiPath(conf), sys_info_list); } From f7641c24a3ba867ecca995ec9f294317f55eb723 Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 10 Sep 2025 14:26:11 +0200 Subject: [PATCH 2/6] fix(ascii): fix the calculation of the alignment buffer size --- src/ascii.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ascii.zig b/src/ascii.zig index 72eea58..c733bc0 100644 --- a/src/ascii.zig +++ b/src/ascii.zig @@ -115,7 +115,7 @@ pub fn printAscii(allocator: std.mem.Allocator, ascii_art_path: ?[]u8, sys_info_ 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, longest_ascii_art_row_len - (try utils.countCodepoints(ascii_art_items[i])) + spacing); + 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); @memset(alignment_buffer, ' '); if (i < ascii_art_len) { From f4a8c962e6780d0945dbc50d0c62399a81fef543 Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 10 Sep 2025 14:27:46 +0200 Subject: [PATCH 3/6] refactor: rename a variable --- src/main.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.zig b/src/main.zig index eca8fc5..6698619 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,11 +10,11 @@ pub fn main() !void { const allocator = gpa.allocator(); defer _ = gpa.deinit(); - var sys_info_list = std.array_list.Managed([]u8).init(allocator); - defer sys_info_list.deinit(); + var modules_list = std.array_list.Managed([]u8).init(allocator); + defer modules_list.deinit(); errdefer { - for (sys_info_list.items) |info| { + for (modules_list.items) |info| { allocator.free(info); } } @@ -27,7 +27,7 @@ pub fn main() !void { const username = try detection.user.getUsername(allocator); const hostname = try detection.system.getHostname(allocator); - try sys_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{ + try modules_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{ ascii.Yellow, username, ascii.Reset, @@ -40,16 +40,16 @@ pub fn main() !void { const separtor_buffer = try allocator.alloc(u8, username.len + hostname.len + 1); @memset(separtor_buffer, '-'); - try sys_info_list.append(separtor_buffer); + try modules_list.append(separtor_buffer); if (modules_types.items.len == 0) { inline for (0..formatters.default_formatters.len) |i| { const result = try formatters.default_formatters[i](allocator); switch (result) { - .string => |r| try sys_info_list.append(r), + .string => |r| try modules_list.append(r), .string_arraylist => |r| { defer r.deinit(); - try sys_info_list.appendSlice(r.items); + try modules_list.appendSlice(r.items); }, } } @@ -61,14 +61,14 @@ pub fn main() !void { const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color); switch (result) { - .string => |r| try sys_info_list.append(r), + .string => |r| try modules_list.append(r), .string_arraylist => |r| { defer r.deinit(); - try sys_info_list.appendSlice(r.items); + try modules_list.appendSlice(r.items); }, } } } - try ascii.printAscii(allocator, config.getAsciiPath(conf), sys_info_list); + try ascii.printAscii(allocator, config.getAsciiPath(conf), modules_list); } From 3214725e586118f3ec941fb9d798db8bd9976fbd Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 10 Sep 2025 14:29:14 +0200 Subject: [PATCH 4/6] refactor: rename a function --- src/ascii.zig | 2 +- src/main.zig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ascii.zig b/src/ascii.zig index c733bc0..29848b3 100644 --- a/src/ascii.zig +++ b/src/ascii.zig @@ -65,7 +65,7 @@ test "parse ffffff" { try std.testing.expect((result.r == 255) and (result.g == 255) and (result.b == 255)); } -pub fn printAscii(allocator: std.mem.Allocator, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void { +pub fn printAsciiAndModules(allocator: std.mem.Allocator, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void { var stdout_buffer: [2048]u8 = undefined; var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); const stdout = &stdout_writer.interface; diff --git a/src/main.zig b/src/main.zig index 6698619..289dd4a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -70,5 +70,5 @@ pub fn main() !void { } } - try ascii.printAscii(allocator, config.getAsciiPath(conf), modules_list); + try ascii.printAsciiAndModules(allocator, config.getAsciiPath(conf), modules_list); } From ea966cd47426d060f6edd3c1c5f5118846169145 Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 10 Sep 2025 14:29:58 +0200 Subject: [PATCH 5/6] build: bump version to 0.21.0 --- build.zig.zon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig.zon b/build.zig.zon index bc97d57..e96d9ec 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -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.20.0", + .version = "0.21.0", // Together with name, this represents a globally unique package // identifier. This field is generated by the Zig toolchain when the From acc4563d2d6977eaa91efc29d2751a844cacf42f Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 10 Sep 2025 14:46:09 +0200 Subject: [PATCH 6/6] docs(README): update README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 82b0cf2..ee5f803 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ # Zigfetch +![Zig](https://img.shields.io/badge/Zig-%23F7A41D.svg?style=flat&logo=zig&logoColor=white) +![GitHub Release](https://img.shields.io/github/v/release/utox39/zigfetch) +![macOS](https://img.shields.io/badge/mac%20os-000000?style=flat&logo=macos&logoColor=F0F0F0) +![Linux](https://img.shields.io/badge/Linux-FCC624?style=flat&logo=linux&logoColor=black) + +--- + - [Description](#description) - [Requirements](#requirements) - [Installation](#installation)