Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126const std = @import("std");const u = @import("utils.zig");
pub fn Param( comptime T: type, comptime name: []const u8, comptime description: []const u8, comptime options: u.Options,) type { if (options.optional == .default_value and options.optional.default_value == null and @typeInfo(T) != .optional) @compileError("a null default requires an optional param type, got: " ++ @typeName(T));
return struct { raw: []const u8, value: T,
pub const info: u.Info = .{ .name = name, .description = u.writeDescription(description, options), .type_text = options.type_text orelse u.typeText(T), .optional = options.optional, .enum_values = options.enum_values, .max_length = options.max_length, .min_length = options.min_length, .max_value = options.max_value, .min_value = options.min_value, }; };}
pub fn String( comptime name: []const u8, comptime description: []const u8, comptime options: u.StringOptions,) type { return Param([]const u8, name, description, .{ .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = value }, }, .type_text = options.type_text, .min_length = options.min_length, .max_length = options.max_length, .quote_default = true, });}
pub fn Flag( comptime name: []const u8, comptime description: []const u8,) type { return struct { is_present: bool,
pub const info: u.Info = .{ .name = name, .description = description, .type_text = u.flag, // this is mostly just a filler, switch is always false if it is not present .optional = .{ .default_value = "false" }, }; };}
pub const Switch = Flag;
pub fn Enum( comptime T: type, comptime description: []const u8, comptime options: u.EnumOptions(T),) type { const name = if (options.name) |value| value else blk: { const string = u.typeName(T); var buffer: [string.len:0]u8 = @splat(0); _ = std.ascii.lowerString(&buffer, string); const copy = buffer; break :blk © }; const Type: std.lang.Type = @typeInfo(T); const E = Type.@"enum";
return Param(T, name, description, .{ .type_text = options.type_text, // I'm not sure if I want field_names or decl_names .enum_values = E.field_names, .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = @tagName(value) }, }, });}
pub fn Float( comptime name: []const u8, comptime description: []const u8, comptime options: u.NumberOptions(f128),) type { return Param(f128, name, description, .{ .type_text = options.type_text, .min_value = options.min, .max_value = options.max, .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = std.fmt.comptimePrint("{d}", .{value}) }, }, });}
pub fn Int( comptime name: []const u8, comptime description: []const u8, comptime options: u.NumberOptions(i128),) type { return Param(i128, name, description, .{ .type_text = options.type_text, .min_value = if (options.min) |min| @floatFromInt(min) else null, .max_value = if (options.max) |max| @floatFromInt(max) else null, .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = std.fmt.comptimePrint("{d}", .{value}) }, }, });}