a modern tui library written in zig

view: implemented an easy mechanism for vertical and horizontal scrolling

- Implemented View as an easy way to use the existing Screen and Window APIs for rendering Cells that don't fit within a Window. Basically, a user renders the oversized Cell content to a View, then renders a part of that View to a Window.
- Created the `view.zig` example.

authored by 00JCIV00 and committed by rockorager.dev 94bec1ec a1b43d24

Changed files
+426 -1
examples
src
+2 -1
build.zig
··· 51 51 52 52 // Examples 53 53 const Example = enum { 54 + aio, 54 55 cli, 55 56 image, 56 57 main, ··· 58 59 table, 59 60 text_input, 60 61 vaxis, 62 + view, 61 63 vt, 62 64 xev, 63 - aio, 64 65 }; 65 66 const example_option = b.option(Example, "example", "Example to run (default: text_input)") orelse .text_input; 66 67 const example_step = b.step("example", "Run example");
+270
examples/view.zig
··· 1 + const std = @import("std"); 2 + const log = std.log.scoped(.main); 3 + const mem = std.mem; 4 + const process = std.process; 5 + 6 + const vaxis = @import("vaxis"); 7 + const View = vaxis.View; 8 + const Cell = vaxis.Cell; 9 + const border = vaxis.widgets.border; 10 + 11 + 12 + const Event = union(enum) { 13 + key_press: vaxis.Key, 14 + winsize: vaxis.Winsize, 15 + }; 16 + 17 + pub fn main() !void { 18 + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 19 + defer { 20 + const deinit_status = gpa.deinit(); 21 + if (deinit_status == .leak) { 22 + log.err("memory leak", .{}); 23 + } 24 + } 25 + const alloc = gpa.allocator(); 26 + 27 + var x: usize = 0; 28 + var y: usize = 0; 29 + var h: usize = 0; 30 + var w: usize = 0; 31 + defer log.info( 32 + \\Map Size: {d}x{d} 33 + \\Screen Size: {d}x{d} 34 + \\Position: {d}, {d} 35 + , .{ 36 + map_width, map_height, 37 + w, h, 38 + x, y, 39 + } 40 + ); 41 + 42 + var tty = try vaxis.Tty.init(); 43 + defer tty.deinit(); 44 + 45 + var buffered_writer = tty.bufferedWriter(); 46 + const writer = buffered_writer.writer().any(); 47 + 48 + // Initialize Vaxis 49 + var vx = try vaxis.init(alloc, .{ 50 + .kitty_keyboard_flags = .{ .report_events = true }, 51 + }); 52 + defer vx.deinit(alloc, tty.anyWriter()); 53 + var loop: vaxis.Loop(Event) = .{ 54 + .vaxis = &vx, 55 + .tty = &tty, 56 + }; 57 + try loop.init(); 58 + try loop.start(); 59 + defer loop.stop(); 60 + try vx.enterAltScreen(writer); 61 + try buffered_writer.flush(); 62 + try vx.queryTerminal(tty.anyWriter(), 20 * std.time.ns_per_s); 63 + // Initialize a View 64 + var map_view = try View.init(alloc, &vx.unicode, .{ .max_width = map_width, .max_height = map_height }); 65 + defer map_view.deinit(); 66 + w = map_view.screen.width; 67 + h = map_view.screen.height; 68 + var map_buf: [map_width * map_height]u8 = undefined; 69 + _ = mem.replace(u8, world_map, "\n", "", map_buf[0..]); 70 + _ = try map_view.printSegment(.{ .text = map_buf[0..] }, .{ .wrap = .grapheme }); 71 + 72 + while (true) { 73 + const event = loop.nextEvent(); 74 + switch (event) { 75 + .key_press => |key| { 76 + if (key.matches('c', .{ .ctrl = true })) break; 77 + if (key.matches(vaxis.Key.left, .{})) x -|= 1; 78 + if (key.matches(vaxis.Key.right, .{})) x +|= 1; 79 + if (key.matches(vaxis.Key.up, .{})) y -|= 1; 80 + if (key.matches(vaxis.Key.down, .{})) y +|= 1; 81 + }, 82 + .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), 83 + } 84 + x = @min(x, map_width -| 1); 85 + y = @min(y, map_height -| 1); 86 + 87 + const win = vx.window(); 88 + win.clear(); 89 + try map_view.toWin( 90 + &win, 91 + .{ 92 + .x = x, 93 + .y = y, 94 + //.width = .{ .max = 10 }, 95 + //.height = .{ .max = 10 }, 96 + } 97 + ); 98 + 99 + // Render the screen 100 + try vx.render(writer); 101 + try buffered_writer.flush(); 102 + } 103 + } 104 + 105 + const _world_map = 106 + \\ +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN+ 107 + \\ W................................................................................................E 108 + \\ W................................................................................................E 109 + \\ W............................ @:::::::::@ ..................... .............................E 110 + \\ W..... . ..... ::: ...::::::: ........ ...... %@%...........:*%@ . ..............E 111 + \\ W..@:::::::::::::::::::: ::: ::::@ @@ ..... %#%%%%%%%%%%%%...........................* .......E 112 + \\ W ::::@::::::::::::: .. :: ... * ....... . %%% %%%%%%%%%%%%%..................... ..* .........E 113 + \\ W .....@::::::::::::.=::::: ........... @% @. %% %%%-...................:%% @. ..........E 114 + \\ W...... :::%@@:@:::::::::@ :-........... @%%%%%%%%%%%%%%%%........................ .. ..........E 115 + \\ W......@::::@@@.=-:::::=: ............. %@%%@# %%%%%%%..%%%.........@............ @=............E 116 + \\ W..... :::::::::::::: ................. %%% @ % ........................... . . .............E 117 + \\ W..... #:::::::::::@ .................. @=====@ .........................@ .= ............E 118 + \\ W...... :::::@ @ ................... ============== .... ..................... .. .............E 119 + \\ W....... .::=.. :# ................. ================ ...... ............... .................E 120 + \\ W......... ::::... @-............... ================- ....@ ...-... . ....@ .. ...............E 121 + \\ W............. :=.. % ...............==================+ ......@. .... ... .. @ ..............E 122 + \\ W............... ######@ ............ ========@=@@@======@........ @.... . .. ................E 123 + \\ W................ ######### ................. ===========@............... .@ ..@ =..............E 124 + \\ W............... #############= ............. ========== ................. . ..*. . --- .......E 125 + \\ W................ ###@@@#@#####%.............. ======== .................... @.@.... .*-=- ......E 126 + \\ W................. ##@##@@#@### .............. ========-. -...................... -- + ........E 127 + \\ W................... ##########................=======% *= .................... @-------- .......E 128 + \\ W................... #######* ................-====== .-+....................#-@--@-@#--- ......E 129 + \\ W................... ####### .................. ====- ........................@----------- ......E 130 + \\ W................... ###### ....................@-* .......................... = ----:..... .E 131 + \\ W....................####@ ......................................................... - .... -@ E 132 + \\ W.................... ###.................................................................%-@ ...E 133 + \\ W..................... #@........................................................................E 134 + \\ W...................... ......................................................................E 135 + \\ +SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS+ 136 + \\ 137 + ; 138 + 139 + const world_map = 140 + \\ +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN+ 141 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 142 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 143 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 144 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 145 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 146 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 147 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 148 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 149 + \\ W........................................................................................................................................................ ............................................................................................................................................................................................................................................E 150 + \\ W................................................................................................................................ ................................................................................................................................................................................................................................E 151 + \\ W........................................................................................................................... @@@@@@@@%-::::---::::::=#@@@@.... ................................................................................................................................................................................................................................E 152 + \\ W..................................................................................................................... .@@@@:::::::::::::::::::::::::::::::::::::-@@ ...............................................................................................................................................................................................................................E 153 + \\ W.................................................................................................................... %@@@#:::::::::::::::::::::::::::::::::::::@@- ...................................................................................... .........................................................................................................................E 154 + \\ W................................................................................................................... @:::::::::::::::::::::::::::::::::::::::::@ .................................................................................... .@....@@: .......................................................................................................................E 155 + \\ W............................................................................................... .... +:::::::::::::::::::::::::::::::::- ........................................................................... .. #@-.................@ ......................................................................................................E 156 + \\ W......................................................................................... +@@@@@@@@@@ .... @:::::::::::::::::::::::::::::::@: ........................................................................ @@...........................%%%%*....%%@@= ..................................................................................E 157 + \\ W........................ ... .................... @:::::::::::::@ ........ @::::::::::::::::::::::::::::+% ............................... ....................... @%%%%@@@%%%............................................-@@ %@@*******@@ .. ............................................................E 158 + \\ W................... *@@#+++++++++@@%=== ======== ... @@::@ +::::::::::::::::-@ ....... *:::::::::::::::::::::::::::%@ ............................. %@@@@@= .. :@%%%%%%%%%%#.........................................................................+@= ................................................E 159 + \\ W................ :@%=:::::::::::::::::::::::%%%-.:::::::::::::+%@@ =:::::--:::::@##%%::::::::@ ..... @:::::::::::::::::::::::%@+ ............................ @@%#%%%%%%%%%%#@@* +***@@%%%%%%%%%%%%%##..........................................................................................-%@@@@@+ .........................................E 160 + \\ W............. .*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::%@@@=:::::::::::::::@@ @::::::::@ .... @:::::::::::::::::::@@. ...................... @%%%%%%%%%%%%%%%#%%%%%%%@@%%%%@@@%%%%%%%%%#%%%%%%%%%%%%=.....................................................................................................@@@%. ....................................E 161 + \\ W............ =#-:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::=@* @+:::::=:::+@ ... @::::::::::::::::@. @@@@@%%@@@ ................... @@%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%%%%%%%%%%%%%%............................................................................................................@@@ ...................................E 162 + \\ W......... *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@ *%::::::::::@ @@ ... *::::::::::@@@ ... %%%%%%%%@ ................ @@%%%%%##%* -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%%......................................................................................................* @...@ ...................................E 163 + \\ W....... =@@=::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@ =@@@@ @:::::=@ ..... *:::::::::@ ....... :=@%%@ ............. %@%%%%%%%%%@: @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#=.......................................................................................................@ ...................................E 164 + \\ W...... @:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@: @:::-@ @@* ........ @::::::@- .............. ............... @@@%%%%%%%%%%@ @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.....................................................................................................+@@ .......................................E 165 + \\ W..... :@::::::::::::::::#%*::::::::::::::::::::::::::::::::::::::::::::::::::::@ ...... @:::::::@ .......... ..@:@: .............................. .... *@%%%%%%%%%%%@* +@%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%....................................................................................@ .@.=@.........@ .........................................E 166 + \\ W.... @::::::::::=%@@ #%:::::::::::::::::::::::::::::::::::::::::::::- ........ @::::::::@:@=::@ ........... ................................ -@@ .. @%%%@@%%%%%%%=# @%%%%%%%%%%%%%%%%%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.........................................................................%@@@@@####@# +....#% ............................................E 167 + \\ W. -@-:::*@- .. @::::::::::::::::::::::::::::::::::::::::::::@+ ..... @:::::::::::::::.- ................................................... @%%%@% ... @%%%@# #@%%% : : - %= :: : +%%%%%%%%%%%%%%%%%%%%%%.......................................................................-. @.....@ ...............................................E 168 + \\ W @:::::%@ ............. ::::::::::::::::::::::::::::::::::::::::::::::::%+ +@:::::::::::::::::@ ............................................... %@%%%= ... %@ @%%%@ @%%%%% %. . - : . . +%%%%%%%%%%%%%%%#*%##%-.......................................................................:* ..... @......@: ..............................................E 169 + \\ W@::::+@ .................. ::::::::::::::::::::::::::::::::::::::::::::::::::.+@ @*::::::::::::::::::::@ ........................................... :@@ @%%@# . #%%- @@@%%%%%% . . - # : . *#%%%%%%%%%%%#...............................................................................-@%% .... @........ ..............................................E 170 + \\ W ...................... :::::::::::::::::::::::::::::::::::::::::::::::::::-. @::::::::::::::::::::::::* ........................................... @%%%@ @%%%@ @@@%%%%%%%%%%%%%%%%%%% @. . - : *#. *%%%%%%%%%%%%%#.........................................................................................@ .... @.....% ..............................................E 171 + \\ W.. ......................... ::::::::::@:@:@@@:%@@%%@@@@@=@:::::::::::::::::::::= #=:::::::::::::::::::::::@ ........................................... :@%%%@ #%%%%%@ @@%%%%%%%%%%%%%%%%%%%%%% % % . .* += *%: -%%%%%%%%%#%............................................................................................@ ... +@..# ..............................................E 172 + \\ W............................... .@.::::::::@#@.@+@@%@*@:%@:@@#@:::::::::::::::::::::::::::::::::::::::::::::::::@ ........................................... @@@ @@%@@@@ @@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%...............................................................................................@- .... @ ..............................................E 173 + \\ W............................... @::::::::::@@@:@+@%%@%@:%@:@@@@:::::::::::::::::::::::::::::::::::::::%@@ @:::::@ ........................................... @@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.................................................................................................@ ..... ...............................................E 174 + \\ W............................... @:::::::::@=@:@*@#%@+@:%@:@@=@::::::::::::::::::::::::::::::::::::::*@ @::::::* ................................................ .@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##%##%%%%%%%%%%%%%.............................................................................................@@=.-: .........................................................E 175 + \\ W............................... @:::::::::::::::::::::::::::.::::::::::::::::::::::::::::::::::::::. @@@:@-:+= ................................................ @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%#.......*%##%%%%%%%#%...........................................................................................- @.@ .........................................................E 176 + \\ W.............................. @::::::::.:@@::@@:@@+@@.@@@:@*:@@*:@@*:::::::::::::::::::::::::::::::::@ ............................................. . @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%%#............%%%%%%%%%%..............................@@.+@-@.@.@@@................................................@ .....................................................E 177 + \\ W............................ -=:::::::::::@@*:@@*@@+@::@:@:@*@@=@:@@@::::::::::::::::::::::::::::::@@@ ..................................................... @%%%%%%%%%#%%%%%%@-@@%%%#%%%%%%%%%%%%%%%%%%%%##%#.........%#%%%%%%%%%=...........................@@=.@@..@.@.@...............................................@ *@@ ....................................................E 178 + \\ W........................... @:::::::::::::@=@:@@@%@+@@.@@@:@*@@:::@.@:::::::::::::::::::::@@# :%@ ...................................................... @%%%%%%%%%%%%%@@@@@- *@%#@- .@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%........#%%%%%%%%%%..........................*@@@.:*@.@.@+@..............................................@ =....# ....................................................E 179 + \\ W.......................... @:::::::::::::+@@@:@=@%@+@::@.@:@*#@=@:@@@:::::::::::::::::::.@ ........................................................ @%%%%%%%%%%%%%%@ #@%%@@ .@%%%%%%%%%%%%%%%#%%%%%%%%%%..........%%%%%%%#%=.........................@@=@.@@@.@.@.@............................................@ . =..++ .....................................................E 180 + \\ W......................... @::::::::::::::-=:=:=:--=:==:=:=:=::*@::=:=::::::::::::::::@# ................................................................. @%%%%%%%%%%%@@- .... .@@%%@* @%%%%@@@***%%%#......................%%%%%%%%%..................................................................................% ... .....................................................E 181 + \\ W......................... @.:::::::::::::::::::::::::::::::::::::::::::::::::::::::-% ................................................................... @%%%%%%%%%%%@ ....... @%@ @%%#@ -...............................#%%%%#.........................................................................% #*@@....+ ..... ..@ .....................................................E 182 + \\ W......................... @::::::::::::::::::::::::::::::::::::::::::::::::::::::@ ....................................................................... @%%%%%%%%%@: @%%%%@ :%%% .@.............................................................................................................@ @....@ ... ...@ .....................................................E 183 + \\ W.......................... @:::::::::::::::::::::::::::::::::::::::::::::::::::::- .......................................................................... ==-@@@@@ .@@+--*#####@# @%@ @@@ @................................................................................................................= @... +...@ .....................................................E 184 + \\ W.......................... @::::::::::::::::::::::::::::::::::::::::::::::::::::.@ ........................................................................... %@@@@@@=-============@ . @+.=.....................................................................................................@ #..@ @@%....@ .....................................................E 185 + \\ W.......................... =@::::::::::::::::::::::::::::::::::::::::::::::::::@ ........................................................................... @=====================@ .. ...... @......................................................................................................@ +- @.....@ .....................................................E 186 + \\ W........................... *::::::::::::::::::::::::::::::::::::::::::::::@= ............................................................................ @-======================-@+.. ... @.......................................................................................................@ .@...%*+ .......................................................E 187 + \\ W............................ *:::::::::::::::::::::::::::::::::::::::::::@ ............................................................................. :*-============================-@:. +====-@@@@@@@@* %.........................................................................................................@ .. @..-@ @@ .........................................................E 188 + \\ W............................ @:::@@::::::::::::::::::::::::::::-*=:::::=. ............................................................................... @==================================--===============-@#..........................................................................................................- .. *@ @.@ .........................................................E 189 + \\ W............................. @:@ @:::::::::::::::::::.@...... #@::: ............................................................................. ##=====================================================@ *................- #.....................................................................................= ... :.. .........................................................E 190 + \\ W.............................. @:@ @::::::::::::::::@ :-:: ............................................................................ @@-======================================================% -...............@ @..................................................................................+. ... ............................................................E 191 + \\ W.............................. @::@ %::::::::::::::+ ........... @.: .......................................................................... @=========================================================-@ %.................@# @.:@@:...........................................................................@ ...... ..............................................................E 192 + \\ W.............................. @:.- *-:::::::::::.@ ............ .@ ......................................................................... @============================================================-= *+.................@ . ==@@......................................................................:. .........................................................................E 193 + \\ W............................... =@:+% @::::::::::.@ ........... ........................................................................ @-============================================================-@. @................:%%:..:@ :+-...........................................................% .........................................................................E 194 + \\ W................................ =.@ -@::::::::.% ..... -@::::*@@ .................................................................... #-===============================================================:@ @:........................+ .. %........................................................@% ...........................................................................E 195 + \\ W................................. =::::::::.@ ... #@ @: -@:::@@: ............................................................. @=================================================================% @........................@ ...... @ .....................-@@-@.........................** ............................................................................E 196 + \\ W....................................... @:::::::::.@ #::% *:::=: ........................................................... *-=================================================================- @.......................@ ........ ...................@ @..................@ -@@ ....... ....................................................................E 197 + \\ W....................................... +@.:::::::::%@@@@@@:::@ ..... @::::@ .......................................................... @-=================================================================@. @....................@ ............ :@...............@ . :-...............- ........ ..................................................................E 198 + \\ W........................................ -@::::::::::::::::@ ....... .-+@@-: ........................................................... :==================================================================-@ @.................@ ............. @..............@ .... *................+@ ......... @..@ ..................................................................E 199 + \\ W.......................................... @@**+:.::::::::@ ......... ........................................................... @====================================================================-+ @.............@+ ............... :*..........*@ ....... +.................@ ........ +:..- .................................................................E 200 + \\ W............................................. *%::::::::::@ ............................................................................. +-=====================================================================*@ #........-#@ .................. #........** ........ @..............- ....... @:..@# ................................................................E 201 + \\ W.................................................... -@::::::::% ......... .............................................................. +-========================================================================% @...:@@ ...................... @.......@ ............. =.............+ ....... @@..# ...............................................................E 202 + \\ W........................................................ @+::::@ ...... :+: ....................................................... @-========================================================================-@ =%- ....................... @......* ............... ..............@ ......... *@@ ...............................................................E 203 + \\ W.......................................................... @:::@ ... @@%####@. ................................................... %+===================================@@=-@@=@@@-@@-@@@-@@@=================. .#@+-=@ ........................ @.....: ................ ..= @.......-= ........... ................................................................E 204 + \\ W............................................................. -::+@ ###################@ ................................................... #-================================-@%%-@--@=@+@@=@-@-@+@-=========================@ .......................... @...@- ................ ..@ @...@@ ..................................................................................E 205 + \\ W............................................................. -@@:+@@@#######################@ ................................................... *-================================@=@-@=-@=@+@@=@-=-@:@=========================@ ........................... @.% ................ ...@ @@: ........ ........................................................................E 206 + \\ W............................................................... @%########################@ ............................................... --==============================*@*@-@--@=@+@@-@-@-@+@========================-* ............................. @+ ............. @...@ ......... : ......................................................................E 207 + \\ W................................................................... =###########################@@* .............................................. @-================---========================================================@ ............................... *.- ........... %%...@ ......... #.@ ....................................................................E 208 + \\ W....................................................................... .##############################+@ ............................................. @%=---=-*+--%@ @-====================================================-@ .................................. ........... @.@ @...@ ....... @#....-: ....................................................................E 209 + \\ W...................................................................... @################################=@ ............................................... @-==============================================-@ .................................... .............. @..@ @..@ ... =@.....@ .. ..............................................................E 210 + \\ W..................................................................... @##################################@ ................................................................... +============================================--* ......................................................... *...@ @.@ @........@ .............................................................E 211 + \\ W.................................................................... @####################################@ .................................................................. *===========================================%# ............................................................ @..#@@ @............@ .++#@.@ .... ....................................................E 212 + \\ W................................................................... @######################################@+ ............................................................. @==========================================-@ ............................................................... +....@ @..........@# *.....% .. ............................................E 213 + \\ W................................................................... @#############################################%@+ ......................................................... @-========================================-@ .................................................................. @-...@- @.........+: @...*@ .. @---+: .......................................E 214 + \\ W................................................................... @##################################################@@ ...................................................... =-=====================================-@ .................................................................... @....% @........@ @....# .... #+--+##---=@% .....................................E 215 + \\ W................................................................... %##############%@@@#@@@#@%@%@@@#@%@@#################@. ..................................................... @-===================================-@ ........................................................................ %@..@ @@@@#@ .....+ ..... @+=-----------+@ ..................................E 216 + \\ W................................................................... @###############@@###@#@#@%@%#@%#@@@@####################@= .................................................... @-==================================@ .......................................................................... @..@ .:@@@@ ...... @------------#@ .................................E 217 + \\ W................................................................... +%################@@#@#@#@%@%#@%#@@@@#####################* ..................................................... :-=================================@ ............................................................................. *...:@@@@# ........... #-----------@ ................................E 218 + \\ W.................................................................... @#############%@%@#@#@#@%@##@%#@%@@#####################@ ...................................................... @=================================@ .............................................................................. @........@# ...................... @------------@ ...............................E 219 + \\ W..................................................................... #######################################################. ....................................................... @=================================@ .............................................................................. @@%.++@ ............... .. :+:@---@ @--*% ..............................E 220 + \\ W..................................................................... @############@@#%@%%@%@@@#@@@#@#%@@%#@@##############% ........................................................ .*=================================== .................................................................................. .............. @%%@@ .............................E 221 + \\ W....................................................................... @###########@@#%@@@@%@@##@#@#@#@%@@#@@@#############* ........................................................ %=================================-# .... ................................................................................................. %=@@@@= . :@ .............................E 222 + \\ W........................................................................ @#########%@@@%@@@@%@@@%@@@#@#@%###@%@###########@ ........................................................ @-=================================% ... :*@ ........................................................................................ @------+ . -=-@ .... ...............................E 223 + \\ W......................................................................... @########@@@@%@@@@%@@#%@#@#@#@%@@%@@@########### ......................................................... @===================================% @-=+ ....................................................................................... #@@@@%@--------: @--+ .....................................E 224 + \\ W.......................................................................... @############################%#################+ ......................................................... ===================================-@ =@@===+ .................................................................................... %=--------------= .=---=+ ....................................E 225 + \\ W........................................................................... @@#########################################** ......................................................... ==================================-@ @-======@ ................................................................................... %+------------------*@= @-----# ....................................E 226 + \\ W.............................................................................. @#######################################@ ......................................................... +===============================-@ =======% ................................................................................. .@-----------------------------# ....................................E 227 + \\ W................................................................................. =#######################################@ ......................................................... -+============================-@ . @=====+ ................................................................................ :@--------------------------------%* ..................................E 228 + \\ W................................................................................. @#####################################@ ........................................................... --==========================@ .. @====-@ .............................................................................. @--------------------------------------@ .................................E 229 + \\ W.................................................................................. @#####################################@ ............................................................. %==========================@ ... @-=====@ ............................................................................. @-----------------------------------------% ................................E 230 + \\ W.................................................................................. @*#################################%@ .............................................................. %=========================@ ... +-====* ............................................................................. @-----+@@-@*@@-@@@*@@@*@@#-@@=-@--@-*@@-----=@ ...............................E 231 + \\ W.................................................................................. :*############################+@* ................................................................ %========================-@ ... @-==-@ .............................................................................. *-----@+@-@*@@+@---#@-*@*@-@*#-@--@-@+@-------* ..............................E 232 + \\ W.................................................................................. :*###########################@ ..................................................................... @-=======================* ..... @@-@ .............................................................................. *-----@-@-@*@@--@@-#@-*@%@-@=@-@--@-@-@-------@ ..............................E 233 + \\ W.................................................................................. :*########################### ........................................................................ =+=====================@ ....... ............................................................................... @-----@#@+@#@@+@-@-#@-*@+@*@%@-@--@-@#@+------@ ..............................E 234 + \\ W.................................................................................. :############################@ ........................................................................ #===================*. ................................................................................................ @---------------------------------------------= ...............................E 235 + \\ W.................................................................................. %##########################@ ......................................................................... +================-@ ................................................................................................ =--------------------------------------------# ...............................E 236 + \\ W.................................................................................. @#########################@ ........................................................................... @================@ .................................................................................................. @-------------------------------------------@ ................................E 237 + \\ W................................................................................... @#######################%* ............................................................................. @-===========-@: .................................................................................................... *----------------------------------------+@ .................................E 238 + \\ W................................................................................... @#######################* ............................................................................... *:=========-@* ...................................................................................................... *----% @--------------------@ .................. ..........E 239 + \\ W................................................................................... @#####################%= ................................................................................ .@-=-#%%%%@ ........................................................................................................ :*... @------------------@ .................. @@ ..........E 240 + \\ W................................................................................... @####################@ ................................................................................. ........................................................................................................... ........... @--------------@ .................... @--@ .........E 241 + \\ W................................................................................... %#################### ..................................................................................... ...................................................................................................................................... @-----------=. ..................... @---@: .......E 242 + \\ W................................................................................... @##################% ................................................................................................................................................................................................................................... .%=---------@ .................... @#-----@ .......E 243 + \\ W.................................................................................... @##############%@- .................................................................................................................................................................................................................................... @@@ .................. ==----=@ .......E 244 + \\ W.................................................................................... @#############@ ......................................................................................................................................................................................................................................... ................... @+----@ .........E 245 + \\ W..................................................................................... @############ .................................................................................................................................................................................................................................................................... %@----*@: .............E 246 + \\ W...................................................................................... %##########= ................................................................................................................................................................................................................................................................. @-----@ ...............E 247 + \\ W...................................................................................... @#########@ ................................................................................................................................................................................................................................................................. =@-----#@# ..................E 248 + \\ W....................................................................................... %#######@ ................................................................................................................................................................................................................................................................. %--+@#% ....................E 249 + \\ W....................................................................................... @#######@@. ................................................................................................................................................................................................................................................................. ........................E 250 + \\ W........................................................................................ @########@ .................................................................................................................................................................................................................................................................. .............................E 251 + \\ W......................................................................................... @#####%. ....................................................................................................................................................................................................................................................................................................E 252 + \\ W.......................................................................................... @#####+ ....................................................................................................................................................................................................................................................................................................E 253 + \\ W........................................................................................... @#####@ .................................................................................................................................................................................................................................................................................................E 254 + \\ W............................................................................................ +@@@##%@# ...............................................................................................................................................................................................................................................................................................E 255 + \\ W............................................................................................. +@@@@@ ...............................................................................................................................................................................................................................................................................................E 256 + \\ W................................................................................................. ................................................................................................................................................................................................................................................................................................E 257 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 258 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 259 + \\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E 260 + \\ +SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS+ 261 + \\ 262 + ; 263 + const map_width = mapWidth: { 264 + @setEvalBranchQuota(100_000); 265 + break :mapWidth mem.indexOfScalar(u8, world_map, '\n').?; 266 + }; 267 + const map_height = mapHeight: { 268 + @setEvalBranchQuota(100_000); 269 + break :mapHeight mem.count(u8, world_map, "\n"); 270 + };
+153
src/View.zig
··· 1 + //! A View is effectively an "oversized" Window that can be written to and rendered in pieces. 2 + 3 + const std = @import("std"); 4 + const mem = std.mem; 5 + 6 + const View = @This(); 7 + 8 + const Screen = @import("Screen.zig"); 9 + const Window = @import("Window.zig"); 10 + const Unicode = @import("Unicode.zig"); 11 + const Cell = @import("Cell.zig"); 12 + 13 + /// View Allocator 14 + alloc: mem.Allocator, 15 + /// Underlying Screen 16 + screen: *Screen, 17 + /// Underlying Window 18 + win: *Window, 19 + 20 + 21 + pub const Config = struct { 22 + max_width: usize = 10_000, 23 + max_height: usize = 10_000, 24 + x_pixel: usize = 0, 25 + y_pixel: usize = 0, 26 + }; 27 + pub fn init(alloc: mem.Allocator, unicode: *const Unicode, config: Config) !View { 28 + const screen = try alloc.create(Screen); 29 + screen.* = try Screen.init( 30 + alloc, 31 + .{ 32 + .cols = config.max_width, 33 + .rows = config.max_height, 34 + .x_pixel = config.x_pixel, 35 + .y_pixel = config.y_pixel, 36 + }, 37 + unicode, 38 + ); 39 + const window = try alloc.create(Window); 40 + window.* = .{ 41 + .x_off = 0, 42 + .y_off = 0, 43 + .width = config.max_width, 44 + .height = config.max_height, 45 + .screen = screen, 46 + }; 47 + return .{ 48 + .alloc = alloc, 49 + .screen = screen, 50 + .win = window, 51 + }; 52 + } 53 + 54 + pub fn deinit(self: *View) void { 55 + self.alloc.destroy(self.win); 56 + self.screen.deinit(self.alloc); 57 + self.alloc.destroy(self.screen); 58 + } 59 + 60 + ///Render Config f/ `toWin()` 61 + pub const RenderConfig = struct { 62 + x: usize = 0, 63 + y: usize = 0, 64 + width: Extent = .fit, 65 + height: Extent = .fit, 66 + 67 + pub const Extent = union(enum) { 68 + fit, 69 + max: usize, 70 + }; 71 + }; 72 + /// Render a portion of this View to the provided Window (`win`). 73 + pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !void { 74 + //if (config.x >= self.screen.width or config.y >= self.screen.height) 75 + // return error.PositionOutOfBounds; 76 + const x = @min(self.screen.width - 1, config.x); 77 + const y = @min(self.screen.height - 1, config.y); 78 + const width = width: { 79 + const width = switch (config.width) { 80 + .fit => win.width, 81 + .max => |w| @min(win.width, w), 82 + }; 83 + break :width @min(width, self.screen.width -| x); 84 + }; 85 + const height = height: { 86 + const height = switch (config.height) { 87 + .fit => win.height, 88 + .max => |h| @min(win.height, h), 89 + }; 90 + break :height @min(height, self.screen.height -| y); 91 + }; 92 + //win.clear(); 93 + for (0..height) |row| { 94 + for (0..width) |col| { 95 + win.writeCell( 96 + col, 97 + row, 98 + self.win.readCell( 99 + @min(self.screen.width, x +| col), 100 + //self.screen.height -| 1 -| @min(self.screen.height, y +| row), 101 + @min(self.screen.height, y +| row), 102 + ) orelse { 103 + std.log.err( 104 + \\ Position Out of Bounds: 105 + \\ - Pos: {d}, {d} 106 + \\ - Size: {d}, {d} 107 + , .{ 108 + col, row, 109 + self.screen.width, self.screen.height, 110 + }, 111 + ); 112 + return error.PositionOutOfBounds; 113 + }, 114 + ); 115 + } 116 + } 117 + } 118 + 119 + /// Writes a cell to the location in the View 120 + pub fn writeCell(self: View, col: usize, row: usize, cell: Cell) void { 121 + self.win.writeCell(col, row, cell); 122 + } 123 + 124 + /// Reads a cell at the location in the View 125 + pub fn readCell(self: View, col: usize, row: usize) ?Cell { 126 + return self.win.readCell(col, row); 127 + } 128 + 129 + /// Fills the View with the default cell 130 + pub fn clear(self: View) void { 131 + self.win.clear(); 132 + } 133 + 134 + /// Returns the width of the grapheme. This depends on the terminal capabilities 135 + pub fn gwidth(self: View, str: []const u8) usize { 136 + return self.win.gwidth(str); 137 + } 138 + 139 + /// Fills the View with the provided cell 140 + pub fn fill(self: View, cell: Cell) void { 141 + self.fill(cell); 142 + } 143 + 144 + /// Prints segments to the View. Returns true if the text overflowed with the 145 + /// given wrap strategy and size. 146 + pub fn print(self: View, segments: []const Cell.Segment, opts: Window.PrintOptions) !Window.PrintResult { 147 + return self.win.print(segments, opts); 148 + } 149 + 150 + /// Print a single segment. This is just a shortcut for print(&.{segment}, opts) 151 + pub fn printSegment(self: View, segment: Cell.Segment, opts: Window.PrintOptions) !Window.PrintResult { 152 + return self.print(&.{segment}, opts); 153 + }
+1
src/main.zig
··· 30 30 pub const grapheme = @import("grapheme"); 31 31 pub const Event = @import("event.zig").Event; 32 32 pub const Unicode = @import("Unicode.zig"); 33 + pub const View = @import("View.zig"); 33 34 34 35 /// The target TTY implementation 35 36 pub const Tty = switch (builtin.os.tag) {