feat(formatters): add FormatterContext
This commit is contained in:
@@ -8,7 +8,13 @@ const Result = union(enum) {
|
|||||||
string_arraylist: std.array_list.Managed([]u8),
|
string_arraylist: std.array_list.Managed([]u8),
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror!Result{
|
pub const FormatterContext = struct {
|
||||||
|
gpa: std.mem.Allocator,
|
||||||
|
io: std.Io,
|
||||||
|
environ: std.process.Environ,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const formatters = [_]*const fn (fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) anyerror!Result{
|
||||||
&getFormattedOsInfo,
|
&getFormattedOsInfo,
|
||||||
&getFormattedKernelInfo,
|
&getFormattedKernelInfo,
|
||||||
&getFormattedUptimeInfo,
|
&getFormattedUptimeInfo,
|
||||||
@@ -26,7 +32,7 @@ pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const
|
|||||||
&getFormattedCustom,
|
&getFormattedCustom,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{
|
pub const default_formatters = [_]*const fn (fmt_ctx: FormatterContext) anyerror!Result{
|
||||||
&getDefaultFormattedOsInfo,
|
&getDefaultFormattedOsInfo,
|
||||||
&getDefaultFormattedKernelInfo,
|
&getDefaultFormattedKernelInfo,
|
||||||
&getDefaultFormattedUptimeInfo,
|
&getDefaultFormattedUptimeInfo,
|
||||||
@@ -54,90 +60,154 @@ pub fn getFormattedUsernameHostname(allocator: std.mem.Allocator, color: []const
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedKernelInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedKernelInfo(allocator, "Kernel", display.Yellow);
|
return try getFormattedKernelInfo(fmt_ctx, "Kernel", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedKernelInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const kernel_info = try detection.system.getKernelInfo(allocator);
|
const kernel_info = try detection.system.getKernelInfo(allocator);
|
||||||
defer allocator.free(kernel_info.kernel_name);
|
defer allocator.free(kernel_info.kernel_name);
|
||||||
defer allocator.free(kernel_info.kernel_release);
|
defer allocator.free(kernel_info.kernel_release);
|
||||||
|
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{ key_color, key, display.Reset, kernel_info.kernel_name, kernel_info.kernel_release }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
kernel_info.kernel_name,
|
||||||
|
kernel_info.kernel_release,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedOsInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedOsInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedOsInfo(allocator, "OS", display.Yellow);
|
return try getFormattedOsInfo(fmt_ctx, "OS", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedOsInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedOsInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const os_info = try detection.system.getOsInfo(allocator);
|
const os_info = try detection.system.getOsInfo(allocator);
|
||||||
defer allocator.free(os_info);
|
defer allocator.free(os_info);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, os_info }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
os_info,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedLocaleInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedLocaleInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedLocaleInfo(allocator, "Locale", display.Yellow);
|
return try getFormattedLocaleInfo(fmt_ctx, "Locale", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedLocaleInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedLocaleInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
const locale = try detection.system.getLocale(allocator);
|
const allocator = fmt_ctx.gpa;
|
||||||
|
const environ = fmt_ctx.environ;
|
||||||
|
|
||||||
|
const locale = try detection.system.getLocale(allocator, environ);
|
||||||
defer allocator.free(locale);
|
defer allocator.free(locale);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, locale }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
locale,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedUptimeInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedUptimeInfo(allocator, "Uptime", display.Yellow);
|
return try getFormattedUptimeInfo(fmt_ctx, "Uptime", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedUptimeInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
const uptime = try detection.system.getSystemUptime();
|
const allocator = fmt_ctx.gpa;
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes", .{ key_color, key, display.Reset, uptime.days, uptime.hours, uptime.minutes }) };
|
const io = fmt_ctx.io;
|
||||||
|
|
||||||
|
const uptime = if (builtin.os.tag == .macos) try detection.system.getSystemUptime(io) else if (builtin.os.tag == .linux) try detection.system.getSystemUptime();
|
||||||
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
uptime.days,
|
||||||
|
uptime.hours,
|
||||||
|
uptime.minutes,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedPackagesInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedPackagesInfo(allocator, "Packages", display.Yellow);
|
return try getFormattedPackagesInfo(fmt_ctx, "Packages", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedPackagesInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
const packages_info = try detection.packages.getPackagesInfo(allocator);
|
const allocator = fmt_ctx.gpa;
|
||||||
|
const io = fmt_ctx.io;
|
||||||
|
|
||||||
|
const packages_info = try detection.packages.getPackagesInfo(allocator, io);
|
||||||
defer allocator.free(packages_info);
|
defer allocator.free(packages_info);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, display.Reset, packages_info }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
packages_info,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedShellInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedShellInfo(allocator, "Shell", display.Yellow);
|
return try getFormattedShellInfo(fmt_ctx, "Shell", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedShellInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedShellInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
const shell = try detection.user.getShell(allocator);
|
const allocator = fmt_ctx.gpa;
|
||||||
|
const io = fmt_ctx.io;
|
||||||
|
const environ = fmt_ctx.environ;
|
||||||
|
|
||||||
|
const shell = try detection.user.getShell(allocator, io, environ);
|
||||||
defer allocator.free(shell);
|
defer allocator.free(shell);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, shell[0..(shell.len - 1)] }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
shell[0..(shell.len - 1)],
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedCpuInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedCpuInfo(allocator, "Cpu", display.Yellow);
|
return try getFormattedCpuInfo(fmt_ctx, "Cpu", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedCpuInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const cpu_info = try detection.hardware.getCpuInfo(allocator);
|
const cpu_info = try detection.hardware.getCpuInfo(allocator);
|
||||||
defer allocator.free(cpu_info.cpu_name);
|
defer allocator.free(cpu_info.cpu_name);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, display.Reset, cpu_info.cpu_name, cpu_info.cpu_cores, cpu_info.cpu_max_freq }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
cpu_info.cpu_name,
|
||||||
|
cpu_info.cpu_cores,
|
||||||
|
cpu_info.cpu_max_freq,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedGpuInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
if (builtin.os.tag == .macos) {
|
return try getFormattedGpuInfo(fmt_ctx, "Gpu", display.Yellow);
|
||||||
return try getFormattedGpuInfo(allocator, "Gpu", display.Yellow);
|
|
||||||
} else if (builtin.os.tag == .linux) {
|
|
||||||
return try getFormattedGpuInfo(allocator, "Gpu", display.Yellow);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedGpuInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
if (builtin.os.tag == .macos) {
|
if (builtin.os.tag == .macos) {
|
||||||
const gpu_info = try detection.hardware.getGpuInfo(allocator);
|
const gpu_info = try detection.hardware.getGpuInfo(allocator);
|
||||||
defer allocator.free(gpu_info.gpu_name);
|
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, display.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
gpu_info.gpu_name,
|
||||||
|
gpu_info.gpu_cores,
|
||||||
|
gpu_info.gpu_freq,
|
||||||
|
}) };
|
||||||
} else if (builtin.os.tag == .linux) {
|
} else if (builtin.os.tag == .linux) {
|
||||||
var formatted_gpu_info_list = std.array_list.Managed([]u8).init(allocator);
|
var formatted_gpu_info_list = std.array_list.Managed([]u8).init(allocator);
|
||||||
|
|
||||||
@@ -146,9 +216,21 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
|||||||
for (gpu_info_list.items) |g| {
|
for (gpu_info_list.items) |g| {
|
||||||
var formatted_gpu_info: []u8 = undefined;
|
var formatted_gpu_info: []u8 = undefined;
|
||||||
if ((g.gpu_cores == 0) or (g.gpu_freq == 0.0)) {
|
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, display.Reset, g.gpu_name });
|
formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
g.gpu_name,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, display.Reset, g.gpu_name, g.gpu_cores, g.gpu_freq });
|
formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
g.gpu_name,
|
||||||
|
g.gpu_cores,
|
||||||
|
g.gpu_freq,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
try formatted_gpu_info_list.append(formatted_gpu_info);
|
try formatted_gpu_info_list.append(formatted_gpu_info);
|
||||||
allocator.free(g.gpu_name);
|
allocator.free(g.gpu_name);
|
||||||
@@ -159,67 +241,108 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedRamInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedRamInfo(allocator, "Ram", display.Yellow);
|
return try getFormattedRamInfo(fmt_ctx, "Ram", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedRamInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const ram_info = if (builtin.os.tag == .macos) try detection.hardware.getRamInfo() else if (builtin.os.tag == .linux) try detection.hardware.getRamInfo(allocator);
|
const ram_info = if (builtin.os.tag == .macos) try detection.hardware.getRamInfo() else if (builtin.os.tag == .linux) try detection.hardware.getRamInfo(allocator);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, display.Reset, ram_info.ram_usage, ram_info.ram_size, ram_info.ram_usage_percentage }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
ram_info.ram_usage,
|
||||||
|
ram_info.ram_size,
|
||||||
|
ram_info.ram_usage_percentage,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedSwapInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedSwapInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedSwapInfo(allocator, "Swap", display.Yellow);
|
return try getFormattedSwapInfo(fmt_ctx, "Swap", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedSwapInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const swap_info = if (builtin.os.tag == .macos) try detection.hardware.getSwapInfo() else if (builtin.os.tag == .linux) try detection.hardware.getSwapInfo(allocator);
|
const swap_info = if (builtin.os.tag == .macos) try detection.hardware.getSwapInfo() else if (builtin.os.tag == .linux) try detection.hardware.getSwapInfo(allocator);
|
||||||
if (swap_info) |s| {
|
if (swap_info) |s| {
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, display.Reset, s.swap_usage, s.swap_size, s.swap_usage_percentage }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
display.Reset,
|
||||||
|
s.swap_usage,
|
||||||
|
s.swap_size,
|
||||||
|
s.swap_usage_percentage,
|
||||||
|
}) };
|
||||||
} else {
|
} else {
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, display.Reset }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, display.Reset }) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedDiskInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedDiskInfo(allocator, "Disk", display.Yellow);
|
return try getFormattedDiskInfo(fmt_ctx, "Disk", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedDiskInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const disk_info = try detection.hardware.getDiskSize("/");
|
const disk_info = try detection.hardware.getDiskSize("/");
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{ key_color, key, disk_info.disk_path, display.Reset, disk_info.disk_usage, disk_info.disk_size, disk_info.disk_usage_percentage }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
disk_info.disk_path,
|
||||||
|
display.Reset,
|
||||||
|
disk_info.disk_usage,
|
||||||
|
disk_info.disk_size,
|
||||||
|
disk_info.disk_usage_percentage,
|
||||||
|
}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedWindowManagerInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedWindowManagerInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedWindowManagerInfo(allocator, "WM", display.Yellow);
|
return try getFormattedWindowManagerInfo(fmt_ctx, "WM", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedWindowManagerInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedWindowManagerInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
const wm = try detection.system.getWindowManagerInfo(allocator);
|
const wm = try detection.system.getWindowManagerInfo(allocator);
|
||||||
defer allocator.free(wm);
|
defer allocator.free(wm);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, wm }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, wm }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedTerminalNameInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedTerminalNameInfo(allocator, "Terminal", display.Yellow);
|
return try getFormattedTerminalNameInfo(fmt_ctx, "Terminal", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedTerminalNameInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedTerminalNameInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
const terminal_name = try detection.user.getTerminalName(allocator);
|
const allocator = fmt_ctx.gpa;
|
||||||
|
const environ = fmt_ctx.environ;
|
||||||
|
|
||||||
|
const terminal_name = try detection.user.getTerminalName(allocator, environ);
|
||||||
defer allocator.free(terminal_name);
|
defer allocator.free(terminal_name);
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, terminal_name }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, display.Reset, terminal_name }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedNetInfo(fmt_ctx: FormatterContext) !Result {
|
||||||
return try getFormattedNetInfo(allocator, "Local IP", display.Yellow);
|
return try getFormattedNetInfo(fmt_ctx, "Local IP", display.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedNetInfo(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
var formatted_net_info_list = std.array_list.Managed([]u8).init(allocator);
|
var formatted_net_info_list = std.array_list.Managed([]u8).init(allocator);
|
||||||
|
|
||||||
var net_info_list = try detection.network.getNetInfo(allocator);
|
var net_info_list = try detection.network.getNetInfo(allocator);
|
||||||
for (net_info_list.items) |n| {
|
for (net_info_list.items) |n| {
|
||||||
try formatted_net_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {s}", .{ key_color, key, n.interface_name, display.Reset, n.ipv4_addr }));
|
try formatted_net_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {s}", .{
|
||||||
|
key_color,
|
||||||
|
key,
|
||||||
|
n.interface_name,
|
||||||
|
display.Reset,
|
||||||
|
n.ipv4_addr,
|
||||||
|
}));
|
||||||
allocator.free(n.interface_name);
|
allocator.free(n.interface_name);
|
||||||
allocator.free(n.ipv4_addr);
|
allocator.free(n.ipv4_addr);
|
||||||
}
|
}
|
||||||
@@ -228,6 +351,8 @@ pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
|||||||
return Result{ .string_arraylist = formatted_net_info_list };
|
return Result{ .string_arraylist = formatted_net_info_list };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedCustom(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedCustom(fmt_ctx: FormatterContext, key: []const u8, key_color: []const u8) !Result {
|
||||||
|
const allocator = fmt_ctx.gpa;
|
||||||
|
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ key_color, key, display.Reset }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ key_color, key, display.Reset }) };
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/main.zig
11
src/main.zig
@@ -45,9 +45,15 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
@memset(separtor_buffer, '-');
|
@memset(separtor_buffer, '-');
|
||||||
try modules_list.append(separtor_buffer);
|
try modules_list.append(separtor_buffer);
|
||||||
|
|
||||||
|
const fmt_ctx = formatters.FormatterContext{
|
||||||
|
.gpa = allocator,
|
||||||
|
.environ = init.minimal.environ,
|
||||||
|
.io = init.io,
|
||||||
|
};
|
||||||
|
|
||||||
if (modules_types.items.len == 0) {
|
if (modules_types.items.len == 0) {
|
||||||
inline for (0..formatters.default_formatters.len) |i| {
|
inline for (0..formatters.default_formatters.len) |i| {
|
||||||
const result = try formatters.default_formatters[i](allocator);
|
const result = try formatters.default_formatters[i](fmt_ctx);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
.string => |r| try modules_list.append(r),
|
.string => |r| try modules_list.append(r),
|
||||||
.string_arraylist => |r| {
|
.string_arraylist => |r| {
|
||||||
@@ -62,7 +68,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
const rgb = try display.hexColorToRgb(module.key_color);
|
const rgb = try display.hexColorToRgb(module.key_color);
|
||||||
const key_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b });
|
const key_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b });
|
||||||
|
|
||||||
const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color);
|
const result = try formatters.formatters[@intFromEnum(module_type)](fmt_ctx, module.key, key_color);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
.string => |r| try modules_list.append(r),
|
.string => |r| try modules_list.append(r),
|
||||||
.string_arraylist => |r| {
|
.string_arraylist => |r| {
|
||||||
@@ -73,7 +79,6 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: rename ascii.zig in display.zig
|
|
||||||
// TODO: return the formatted ascii and modules to print instead of directly print them
|
// TODO: return the formatted ascii and modules to print instead of directly print them
|
||||||
try display.printAsciiAndModules(allocator, io, config.getAsciiPath(conf), modules_list);
|
try display.printAsciiAndModules(allocator, io, config.getAsciiPath(conf), modules_list);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user