a modern tui library written in zig
17
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(tty): move state into Vaxis struct

Move tty state (*not* termios, though) into Vaxis struct

+35 -51
-30
src/Tty.zig
··· 24 24 25 25 buffered_writer: BufferedWriter, 26 26 27 - state: struct { 28 - /// if we are in the alt screen 29 - alt_screen: bool = false, 30 - /// if we have entered kitty keyboard 31 - kitty_keyboard: bool = false, 32 - bracketed_paste: bool = false, 33 - mouse: bool = false, 34 - pixel_mouse: bool = false, 35 - color_scheme_updates: bool = false, 36 - cursor: struct { 37 - row: usize = 0, 38 - col: usize = 0, 39 - } = .{}, 40 - } = .{}, 41 - 42 27 /// initializes a Tty instance by opening /dev/tty and "making it raw" 43 28 pub fn init() !Tty { 44 29 // Open our tty ··· 56 41 57 42 /// release resources associated with the Tty return it to its original state 58 43 pub fn deinit(self: *Tty) void { 59 - if (self.state.kitty_keyboard) { 60 - _ = self.write(ctlseqs.csi_u_pop) catch {}; 61 - } 62 - if (self.state.mouse) { 63 - _ = self.write(ctlseqs.mouse_reset) catch {}; 64 - } 65 - if (self.state.bracketed_paste) { 66 - _ = self.write(ctlseqs.bp_reset) catch {}; 67 - } 68 - if (self.state.alt_screen) { 69 - _ = self.write(ctlseqs.rmcup) catch {}; 70 - } 71 - if (self.state.color_scheme_updates) { 72 - _ = self.write(ctlseqs.color_scheme_reset) catch {}; 73 - } 74 44 // always show the cursor on exit 75 45 _ = self.write(ctlseqs.show_cursor) catch {}; 76 46 self.flush() catch {};
+35 -21
src/Vaxis.zig
··· 77 77 legacy, 78 78 } = .standard, 79 79 80 + state: struct { 81 + /// if we are in the alt screen 82 + alt_screen: bool = false, 83 + /// if we have entered kitty keyboard 84 + kitty_keyboard: bool = false, 85 + bracketed_paste: bool = false, 86 + mouse: bool = false, 87 + pixel_mouse: bool = false, 88 + color_scheme_updates: bool = false, 89 + cursor: struct { 90 + row: usize = 0, 91 + col: usize = 0, 92 + } = .{}, 93 + } = .{}, 94 + 80 95 /// Initialize Vaxis with runtime options 81 96 pub fn init(alloc: std.mem.Allocator, opts: Options) !Vaxis { 82 97 return .{ ··· 124 139 self.screen_last.deinit(alloc); 125 140 self.screen_last = try InternalScreen.init(alloc, winsize.cols, winsize.rows); 126 141 var tty = self.tty orelse return; 127 - if (tty.state.alt_screen) 142 + if (self.state.alt_screen) 128 143 _ = try tty.write(ctlseqs.home) 129 144 else { 130 145 _ = try tty.buffered_writer.write("\r"); 131 146 var i: usize = 0; 132 - while (i < tty.state.cursor.row) : (i += 1) { 147 + while (i < self.state.cursor.row) : (i += 1) { 133 148 _ = try tty.buffered_writer.write(ctlseqs.ri); 134 149 } 135 150 } ··· 152 167 /// be exited if calling deinit while in the alt screen 153 168 pub fn enterAltScreen(self: *Vaxis) !void { 154 169 if (self.tty) |*tty| { 155 - if (tty.state.alt_screen) return; 170 + if (self.state.alt_screen) return; 156 171 _ = try tty.write(ctlseqs.smcup); 157 172 try tty.flush(); 158 - tty.state.alt_screen = true; 173 + self.state.alt_screen = true; 159 174 } 160 175 } 161 176 162 177 /// exit the alternate screen 163 178 pub fn exitAltScreen(self: *Vaxis) !void { 164 179 if (self.tty) |*tty| { 165 - if (!tty.state.alt_screen) return; 180 + if (!self.state.alt_screen) return; 166 181 _ = try tty.write(ctlseqs.rmcup); 167 182 try tty.flush(); 168 - tty.state.alt_screen = false; 183 + self.state.alt_screen = false; 169 184 } 170 185 } 171 186 ··· 278 293 // this if we have an update to make. We also need to hide cursor 279 294 // and then reshow it if needed 280 295 _ = try tty.write(ctlseqs.hide_cursor); 281 - if (tty.state.alt_screen) 296 + if (self.state.alt_screen) 282 297 _ = try tty.write(ctlseqs.home) 283 298 else { 284 299 _ = try tty.write("\r"); 285 300 var i: usize = 0; 286 - while (i < tty.state.cursor.row) : (i += 1) { 301 + while (i < self.state.cursor.row) : (i += 1) { 287 302 _ = try tty.write(ctlseqs.ri); 288 303 } 289 304 } ··· 353 368 // reposition the cursor, if needed 354 369 if (reposition) { 355 370 reposition = false; 356 - if (tty.state.alt_screen) 371 + if (self.state.alt_screen) 357 372 try std.fmt.format(tty.buffered_writer.writer(), ctlseqs.cup, .{ row + 1, col + 1 }) 358 373 else { 359 374 if (cursor_pos.row == row) { ··· 589 604 cursor_pos.row = row; 590 605 } 591 606 if (self.screen.cursor_vis) { 592 - if (tty.state.alt_screen) { 607 + if (self.state.alt_screen) { 593 608 try std.fmt.format( 594 609 tty.buffered_writer.writer(), 595 610 ctlseqs.cup, ··· 615 630 try std.fmt.format(tty.buffered_writer.writer(), ctlseqs.cuf, .{self.screen.cursor_col}); 616 631 } 617 632 } 618 - self.tty.?.state.cursor.row = self.screen.cursor_row; 619 - self.tty.?.state.cursor.col = self.screen.cursor_col; 633 + self.state.cursor.row = self.screen.cursor_row; 634 + self.state.cursor.col = self.screen.cursor_col; 620 635 _ = try tty.write(ctlseqs.show_cursor); 621 636 } else { 622 - self.tty.?.state.cursor.row = cursor_pos.row; 623 - self.tty.?.state.cursor.col = cursor_pos.col; 637 + self.state.cursor.row = cursor_pos.row; 638 + self.state.cursor.col = cursor_pos.col; 624 639 } 625 640 if (self.screen.mouse_shape != self.screen_last.mouse_shape) { 626 641 try std.fmt.format( ··· 651 666 }, 652 667 ); 653 668 try tty.flush(); 654 - tty.state.kitty_keyboard = true; 669 + self.state.kitty_keyboard = true; 655 670 } 656 671 } 657 672 ··· 696 711 ctlseqs.bp_reset; 697 712 _ = try tty.write(seq); 698 713 try tty.flush(); 699 - tty.state.bracketed_paste = enable; 714 + self.state.bracketed_paste = enable; 700 715 } 701 716 } 702 717 ··· 709 724 pub fn setMouseMode(self: *Vaxis, enable: bool) !void { 710 725 if (self.tty) |*tty| { 711 726 if (enable) { 712 - tty.state.mouse = true; 727 + self.state.mouse = true; 713 728 if (self.caps.sgr_pixels) { 714 729 log.debug("enabling mouse mode: pixel coordinates", .{}); 715 - tty.state.pixel_mouse = true; 730 + self.state.pixel_mouse = true; 716 731 _ = try tty.write(ctlseqs.mouse_set_pixels); 717 732 } else { 718 733 log.debug("enabling mouse mode: cell coordinates", .{}); ··· 728 743 /// Translate pixel mouse coordinates to cell + offset 729 744 pub fn translateMouse(self: Vaxis, mouse: Mouse) Mouse { 730 745 var result = mouse; 731 - const tty = self.tty orelse return result; 732 - if (tty.state.pixel_mouse) { 746 + if (self.state.pixel_mouse) { 733 747 std.debug.assert(mouse.xoffset == 0); 734 748 std.debug.assert(mouse.yoffset == 0); 735 749 const xpos = mouse.col; ··· 884 898 _ = try tty.write(ctlseqs.color_scheme_request); 885 899 _ = try tty.write(ctlseqs.color_scheme_set); 886 900 try tty.flush(); 887 - tty.state.color_scheme_updates = true; 901 + self.state.color_scheme_updates = true; 888 902 }