WIP: container rendering for borders + container element rendering

This commit is contained in:
2025-02-03 19:55:33 +01:00
parent 0bf79dc236
commit 2bfacc0e98
7 changed files with 189 additions and 219 deletions

View File

@@ -1,16 +1,32 @@
const std = @import("std");
pub const Color = union(enum) {
default,
index: u8,
ansi: enum(u32) {
reset = 0,
black = 30,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
bright_black = 90,
bright_red,
bright_green,
bright_yellow,
bright_blue,
bright_magenta,
bright_cyan,
bright_white,
},
rgb: [3]u8,
pub fn eql(a: Color, b: Color) bool {
switch (a) {
.default => return b == .default,
.index => |a_idx| {
.ansi => |a_idx| {
switch (b) {
.index => |b_idx| return a_idx == b_idx,
.ansi => |b_idx| return a_idx == b_idx,
else => return false,
}
},
@@ -25,6 +41,30 @@ pub const Color = union(enum) {
}
}
pub fn write(this: Color, writer: anytype, comptime coloring: enum { fg, bg, ul }) !void {
switch (this) {
.ansi => |index| blk: {
if (index == .reset)
break :blk switch (coloring) {
.fg => try std.fmt.format(writer, "39", .{}),
.bg => try std.fmt.format(writer, "49", .{}),
.ul => try std.fmt.format(writer, "59", .{}),
};
break :blk switch (coloring) {
.fg => try std.fmt.format(writer, "38;5;{d}", .{@intFromEnum(index)}),
.bg => try std.fmt.format(writer, "48;5;{d}", .{@intFromEnum(index)}),
.ul => try std.fmt.format(writer, "58;5;{d}", .{@intFromEnum(index)}),
};
},
.rgb => |rgb| switch (coloring) {
.fg => try std.fmt.format(writer, "38;2;{d};{d};{d}", .{ rgb[0], rgb[1], rgb[2] }),
.bg => try std.fmt.format(writer, "48;2;{d};{d};{d}", .{ rgb[0], rgb[1], rgb[2] }),
.ul => try std.fmt.format(writer, "58;2;{d};{d};{d}", .{ rgb[0], rgb[1], rgb[2] }),
},
}
}
pub fn rgbFromUint(val: u24) Color {
const r_bits = val & 0b11111111_00000000_00000000;
const g_bits = val & 0b00000000_11111111_00000000;