a modern tui library written in zig
at v0.1.0 1.4 kB view raw
1const std = @import("std"); 2const assert = std.debug.assert; 3 4const Cell = @import("cell.zig").Cell; 5const Shape = @import("Mouse.zig").Shape; 6const Image = @import("Image.zig"); 7const Winsize = @import("Tty.zig").Winsize; 8 9const log = std.log.scoped(.screen); 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 25/// true when we measure cells with unicode 26unicode: bool = false, 27 28mouse_shape: Shape = .default, 29 30pub fn init(alloc: std.mem.Allocator, winsize: Winsize) !Screen { 31 const w = winsize.cols; 32 const h = winsize.rows; 33 var self = Screen{ 34 .buf = try alloc.alloc(Cell, w * h), 35 .width = w, 36 .height = h, 37 .width_pix = winsize.x_pixel, 38 .height_pix = winsize.y_pixel, 39 }; 40 for (self.buf, 0..) |_, i| { 41 self.buf[i] = .{}; 42 } 43 return self; 44} 45pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void { 46 alloc.free(self.buf); 47} 48 49/// writes a cell to a location. 0 indexed 50pub fn writeCell(self: *Screen, col: usize, row: usize, cell: Cell) void { 51 if (self.width < col) { 52 // column out of bounds 53 return; 54 } 55 if (self.height < row) { 56 // height out of bounds 57 return; 58 } 59 const i = (row * self.width) + col; 60 assert(i < self.buf.len); 61 self.buf[i] = cell; 62}