a modern tui library written in zig
at v0.4.0 1.9 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 Unicode = @import("Unicode.zig"); 9const Method = @import("gwidth.zig").Method; 10 11const Screen = @This(); 12 13width: usize = 0, 14height: usize = 0, 15 16width_pix: usize = 0, 17height_pix: usize = 0, 18 19buf: []Cell = undefined, 20 21cursor_row: usize = 0, 22cursor_col: usize = 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) !Screen { 33 const w = winsize.cols; 34 const h = winsize.rows; 35 const self = Screen{ 36 .buf = try alloc.alloc(Cell, 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} 47pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void { 48 alloc.free(self.buf); 49} 50 51/// writes a cell to a location. 0 indexed 52pub fn writeCell(self: *Screen, col: usize, row: usize, cell: Cell) void { 53 if (self.width <= col) { 54 // column out of bounds 55 return; 56 } 57 if (self.height <= row) { 58 // height out of bounds 59 return; 60 } 61 const i = (row * self.width) + col; 62 assert(i < self.buf.len); 63 self.buf[i] = cell; 64} 65 66pub fn readCell(self: *Screen, col: usize, row: usize) ?Cell { 67 if (self.width <= col) { 68 // column out of bounds 69 return null; 70 } 71 if (self.height <= row) { 72 // height out of bounds 73 return null; 74 } 75 const i = (row * self.width) + col; 76 assert(i < self.buf.len); 77 return self.buf[i]; 78}