2024-01-23 03:09:35 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const assert = std.debug.assert;
|
2024-02-11 19:59:33 +01:00
|
|
|
const Style = @import("Cell.zig").Style;
|
|
|
|
const Cell = @import("Cell.zig");
|
2024-04-15 14:14:31 +02:00
|
|
|
const MouseShape = @import("Mouse.zig").Shape;
|
|
|
|
const CursorShape = Cell.CursorShape;
|
2024-01-23 03:09:35 +01:00
|
|
|
|
|
|
|
const log = std.log.scoped(.internal_screen);
|
|
|
|
|
|
|
|
const InternalScreen = @This();
|
|
|
|
|
|
|
|
pub const InternalCell = struct {
|
|
|
|
char: std.ArrayList(u8) = undefined,
|
|
|
|
style: Style = .{},
|
2024-01-25 02:04:12 +01:00
|
|
|
uri: std.ArrayList(u8) = undefined,
|
|
|
|
uri_id: std.ArrayList(u8) = undefined,
|
2024-01-24 13:12:39 +01:00
|
|
|
// if we got skipped because of a wide character
|
|
|
|
skipped: bool = false,
|
2024-04-30 13:49:29 +02:00
|
|
|
default: bool = false,
|
2024-01-23 03:09:35 +01:00
|
|
|
|
|
|
|
pub fn eql(self: InternalCell, cell: Cell) bool {
|
2024-04-30 13:49:29 +02:00
|
|
|
// fastpath when both cells are default
|
|
|
|
if (self.default and cell.default) return true;
|
|
|
|
// this is actually faster than std.meta.eql on the individual items.
|
|
|
|
// Our strings are always small, usually less than 4 bytes so the simd
|
|
|
|
// usage in std.mem.eql has too much overhead vs looping the bytes
|
|
|
|
if (!std.mem.eql(u8, self.char.items, cell.char.grapheme)) return false;
|
|
|
|
if (!Style.eql(self.style, cell.style)) return false;
|
|
|
|
if (!std.mem.eql(u8, self.uri.items, cell.link.uri)) return false;
|
|
|
|
if (!std.mem.eql(u8, self.uri_id.items, cell.link.params)) return false;
|
|
|
|
return true;
|
2024-01-23 03:09:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
width: usize = 0,
|
|
|
|
height: usize = 0,
|
|
|
|
|
|
|
|
buf: []InternalCell = undefined,
|
|
|
|
|
|
|
|
cursor_row: usize = 0,
|
|
|
|
cursor_col: usize = 0,
|
|
|
|
cursor_vis: bool = false,
|
2024-04-15 14:14:31 +02:00
|
|
|
cursor_shape: CursorShape = .default,
|
2024-01-23 03:09:35 +01:00
|
|
|
|
2024-04-15 14:14:31 +02:00
|
|
|
mouse_shape: MouseShape = .default,
|
2024-01-24 20:36:24 +01:00
|
|
|
|
2024-01-23 03:09:35 +01:00
|
|
|
/// sets each cell to the default cell
|
|
|
|
pub fn init(alloc: std.mem.Allocator, w: usize, h: usize) !InternalScreen {
|
2024-01-25 20:20:32 +01:00
|
|
|
var screen = InternalScreen{
|
|
|
|
.buf = try alloc.alloc(InternalCell, w * h),
|
|
|
|
};
|
2024-01-23 03:09:35 +01:00
|
|
|
for (screen.buf, 0..) |_, i| {
|
|
|
|
screen.buf[i] = .{
|
|
|
|
.char = try std.ArrayList(u8).initCapacity(alloc, 1),
|
2024-01-25 02:04:12 +01:00
|
|
|
.uri = std.ArrayList(u8).init(alloc),
|
|
|
|
.uri_id = std.ArrayList(u8).init(alloc),
|
2024-01-23 03:09:35 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
screen.width = w;
|
|
|
|
screen.height = h;
|
|
|
|
return screen;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *InternalScreen, alloc: std.mem.Allocator) void {
|
|
|
|
for (self.buf, 0..) |_, i| {
|
|
|
|
self.buf[i].char.deinit();
|
2024-01-25 02:04:12 +01:00
|
|
|
self.buf[i].uri.deinit();
|
|
|
|
self.buf[i].uri_id.deinit();
|
2024-01-23 03:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
alloc.free(self.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// writes a cell to a location. 0 indexed
|
|
|
|
pub fn writeCell(
|
|
|
|
self: *InternalScreen,
|
|
|
|
col: usize,
|
|
|
|
row: usize,
|
2024-01-25 02:04:12 +01:00
|
|
|
cell: Cell,
|
2024-01-23 03:09:35 +01:00
|
|
|
) void {
|
|
|
|
if (self.width < col) {
|
|
|
|
// column out of bounds
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (self.height < row) {
|
|
|
|
// height out of bounds
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const i = (row * self.width) + col;
|
|
|
|
assert(i < self.buf.len);
|
|
|
|
self.buf[i].char.clearRetainingCapacity();
|
2024-01-25 02:04:12 +01:00
|
|
|
self.buf[i].char.appendSlice(cell.char.grapheme) catch {
|
2024-01-23 03:09:35 +01:00
|
|
|
log.warn("couldn't write grapheme", .{});
|
|
|
|
};
|
2024-01-25 02:04:12 +01:00
|
|
|
self.buf[i].uri.clearRetainingCapacity();
|
|
|
|
self.buf[i].uri.appendSlice(cell.link.uri) catch {
|
|
|
|
log.warn("couldn't write uri", .{});
|
|
|
|
};
|
|
|
|
self.buf[i].uri.clearRetainingCapacity();
|
|
|
|
self.buf[i].uri_id.appendSlice(cell.link.params) catch {
|
|
|
|
log.warn("couldn't write uri_id", .{});
|
|
|
|
};
|
|
|
|
self.buf[i].style = cell.style;
|
2024-04-30 13:49:29 +02:00
|
|
|
self.buf[i].default = cell.default;
|
2024-01-23 03:09:35 +01:00
|
|
|
}
|
2024-04-15 14:15:26 +02:00
|
|
|
|
|
|
|
pub fn readCell(self: *InternalScreen, col: usize, row: usize) ?Cell {
|
|
|
|
if (self.width < col) {
|
|
|
|
// column out of bounds
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (self.height < row) {
|
|
|
|
// height out of bounds
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const i = (row * self.width) + col;
|
|
|
|
assert(i < self.buf.len);
|
|
|
|
return .{
|
|
|
|
.char = .{ .grapheme = self.buf[i].char.items },
|
|
|
|
.style = self.buf[i].style,
|
|
|
|
};
|
|
|
|
}
|