Merge pull request #16 from utox39/feat/add-username-hostname-color

Feat/add username hostname color
This commit is contained in:
utox39
2025-09-13 20:07:32 +02:00
committed by GitHub
4 changed files with 31 additions and 11 deletions

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.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

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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);