diff --git a/build.zig.zon b/build.zig.zon index e00987f..fa15cab 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.21.1", + .version = "0.22.0", // Together with name, this represents a globally unique package // identifier. This field is generated by the Zig toolchain when the diff --git a/src/config.zig b/src/config.zig index c081bbd..2524335 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const builtin = @import("builtin"); const ascii = @import("ascii.zig"); pub const Module = struct { @@ -10,6 +9,7 @@ pub const Module = struct { pub const Config = struct { ascii_abs_path: ?[]u8 = null, + username_hostname_color: ?[]u8 = null, modules: []Module, }; @@ -31,7 +31,15 @@ pub const ModuleType = enum { }; pub fn getAsciiPath(config: ?std.json.Parsed(Config)) ?[]u8 { - return config.?.value.ascii_abs_path; + if (config) |c| { + return c.value.ascii_abs_path; + } else return null; +} + +pub fn getUsernameHostnameColor(config: ?std.json.Parsed(Config)) ?[]u8 { + if (config) |c| { + return c.value.username_hostname_color; + } else return null; } pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.array_list.Managed(ModuleType) { diff --git a/src/formatters.zig b/src/formatters.zig index c944810..cf466e7 100644 --- a/src/formatters.zig +++ b/src/formatters.zig @@ -41,6 +41,17 @@ pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyer &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}", .{ + color, + username, + ascii.Reset, + color, + hostname, + ascii.Reset, + }); +} + pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result { return try getFormattedKernelInfo(allocator, "Kernel", ascii.Yellow); } diff --git a/src/main.zig b/src/main.zig index 289dd4a..d7af439 100644 --- a/src/main.zig +++ b/src/main.zig @@ -27,14 +27,15 @@ pub fn main() !void { const username = try detection.user.getUsername(allocator); const hostname = try detection.system.getHostname(allocator); - try modules_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{ - ascii.Yellow, - username, - ascii.Reset, - ascii.Yellow, - hostname, - ascii.Reset, - })); + + const username_hostname_color = if (config.getUsernameHostnameColor(conf)) |color| blk: { + var buf: [32]u8 = undefined; + const rgb = try ascii.hexColorToRgb(color); + const formatted_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b }); + break :blk formatted_color; + } else ascii.Yellow; + + try modules_list.append(try formatters.getFormattedUsernameHostname(allocator, username_hostname_color, username, hostname)); allocator.free(hostname); allocator.free(username);