this repo has no description
1const std = @import("std");
2const assert = std.debug.assert;
3
4const Cell = @import("Cell.zig");
5const Shape = @import("Mouse.zig").Shape;
6const Image = @import("Image.zig");
7const Winsize = @import("main.zig").Winsize;
8const Unicode = @import("Unicode.zig");
9const Method = @import("gwidth.zig").Method;
10
11const Screen = @This();
12
13width: u16 = 0,
14height: u16 = 0,
15
16width_pix: u16 = 0,
17height_pix: u16 = 0,
18
19buf: []Cell = &.{},
20
21cursor_row: u16 = 0,
22cursor_col: u16 = 0,
23cursor_vis: bool = false,
24
25unicode: *const Unicode = undefined,
26
27width_method: Method = .wcwidth,
28
29mouse_shape: Shape = .default,
30cursor_shape: Cell.CursorShape = .default,
31
32pub fn init(alloc: std.mem.Allocator, winsize: Winsize, unicode: *const Unicode) std.mem.Allocator.Error!Screen {
33 const w = winsize.cols;
34 const h = winsize.rows;
35 const self = Screen{
36 .buf = try alloc.alloc(Cell, @as(usize, @intCast(w)) * h),
37 .width = w,
38 .height = h,
39 .width_pix = winsize.x_pixel,
40 .height_pix = winsize.y_pixel,
41 .unicode = unicode,
42 };
43 const base_cell: Cell = .{};
44 @memset(self.buf, base_cell);
45 return self;
46}
47
48pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void {
49 alloc.free(self.buf);
50}
51
52/// writes a cell to a location. 0 indexed
53pub fn writeCell(self: *Screen, col: u16, row: u16, cell: Cell) void {
54 if (col >= self.width or
55 row >= self.height)
56 return;
57 const i = (@as(usize, @intCast(row)) * self.width) + col;
58 assert(i < self.buf.len);
59 self.buf[i] = cell;
60}
61
62pub fn readCell(self: *const Screen, col: u16, row: u16) ?Cell {
63 if (col >= self.width or
64 row >= self.height)
65 return null;
66 const i = (@as(usize, @intCast(row)) * self.width) + col;
67 assert(i < self.buf.len);
68 return self.buf[i];
69}
70
71test "refAllDecls" {
72 std.testing.refAllDecls(@This());
73}