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