Files
zterm/src/cell.zig
Yves Biener 424740d350
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m33s
feat(terminal/osc12): define cursor color through style of cell that describes the cursor position
The `.default` color will reset the cursor color to the terminal's default color
2025-11-19 18:42:13 +01:00

62 lines
2.1 KiB
Zig

//! Cell type containing content and formatting for each character in the terminal screen.
cp: u21 = ' ',
style: Style = .{ .emphasis = &.{} },
pub fn eql(this: Cell, other: Cell) bool {
return this.cp == other.cp and this.style.eql(other.style);
}
pub fn reset(this: *Cell) void {
this.style = .{ .emphasis = &.{} };
this.cp = ' ';
}
pub fn value(this: Cell, writer: *std.Io.Writer) !void {
try this.style.value(writer, this.cp);
}
const std = @import("std");
const Style = @import("style.zig");
const Cell = @This();
test "ascii styled text" {
const cells: [4]Cell = .{
.{ .cp = 'Y', .style = .{ .fg = .green, .bg = .grey, .emphasis = &.{} } },
.{ .cp = 'v', .style = .{ .emphasis = &.{ .bold, .underline } } },
.{ .cp = 'e', .style = .{ .emphasis = &.{.italic} } },
.{ .cp = 's', .style = .{ .fg = .lightgreen, .bg = .black, .emphasis = &.{.underline} } },
};
var writer = std.Io.Writer.Allocating.init(std.testing.allocator);
defer writer.deinit();
for (cells) |cell| {
try cell.value(&writer.writer);
}
try std.testing.expectEqualSlices(
u8,
"\x1b[38;5;10;48;5;8;59mY\x1b[0m\x1b[39;49;59;1;4mv\x1b[0m\x1b[39;49;59;3me\x1b[0m\x1b[38;5;2;48;5;16;59;4ms\x1b[0m",
writer.writer.buffer[0..writer.writer.end],
);
}
test "utf-8 styled text" {
const cells: [4]Cell = .{
.{ .cp = '╭', .style = .{ .fg = .green, .bg = .grey, .emphasis = &.{} } },
.{ .cp = '─', .style = .{ .emphasis = &.{} } },
.{ .cp = '┄', .style = .{ .emphasis = &.{} } },
.{ .cp = '┘', .style = .{ .fg = .lightgreen, .bg = .black, .emphasis = &.{.underline} } },
};
var writer = std.Io.Writer.Allocating.init(std.testing.allocator);
defer writer.deinit();
for (cells) |cell| {
try cell.value(&writer.writer);
}
try std.testing.expectEqualSlices(
u8,
"\x1b[38;5;10;48;5;8;59m╭\x1b[0m\x1b[39;49;59m─\x1b[0m\x1b[39;49;59m┄\x1b[0m\x1b[38;5;2;48;5;16;59;4m┘\x1b[0m",
writer.writer.buffer[0..writer.writer.end],
);
}