From 94bec1ec2abe534b169b0bec0a9a032f670763ac Mon Sep 17 00:00:00 2001 From: 00JCIV00 Date: Sun, 22 Sep 2024 20:12:26 -0400 Subject: [PATCH] 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. --- build.zig | 3 +- examples/view.zig | 270 ++++++++++++++++++++++++++++++++++++++++++++++ src/View.zig | 153 ++++++++++++++++++++++++++ src/main.zig | 1 + 4 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 examples/view.zig create mode 100644 src/View.zig diff --git a/build.zig b/build.zig index a6e75f0..663e89a 100644 --- a/build.zig +++ b/build.zig @@ -51,6 +51,7 @@ pub fn build(b: *std.Build) void { // Examples const Example = enum { + aio, cli, image, main, @@ -58,9 +59,9 @@ pub fn build(b: *std.Build) void { table, text_input, vaxis, + view, vt, xev, - aio, }; const example_option = b.option(Example, "example", "Example to run (default: text_input)") orelse .text_input; const example_step = b.step("example", "Run example"); diff --git a/examples/view.zig b/examples/view.zig new file mode 100644 index 0000000..c402541 --- /dev/null +++ b/examples/view.zig @@ -0,0 +1,270 @@ +const std = @import("std"); +const log = std.log.scoped(.main); +const mem = std.mem; +const process = std.process; + +const vaxis = @import("vaxis"); +const View = vaxis.View; +const Cell = vaxis.Cell; +const border = vaxis.widgets.border; + + +const Event = union(enum) { + key_press: vaxis.Key, + winsize: vaxis.Winsize, +}; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer { + const deinit_status = gpa.deinit(); + if (deinit_status == .leak) { + log.err("memory leak", .{}); + } + } + const alloc = gpa.allocator(); + + var x: usize = 0; + var y: usize = 0; + var h: usize = 0; + var w: usize = 0; + defer log.info( + \\Map Size: {d}x{d} + \\Screen Size: {d}x{d} + \\Position: {d}, {d} + , .{ + map_width, map_height, + w, h, + x, y, + } + ); + + var tty = try vaxis.Tty.init(); + defer tty.deinit(); + + var buffered_writer = tty.bufferedWriter(); + const writer = buffered_writer.writer().any(); + + // Initialize Vaxis + var vx = try vaxis.init(alloc, .{ + .kitty_keyboard_flags = .{ .report_events = true }, + }); + defer vx.deinit(alloc, tty.anyWriter()); + var loop: vaxis.Loop(Event) = .{ + .vaxis = &vx, + .tty = &tty, + }; + try loop.init(); + try loop.start(); + defer loop.stop(); + try vx.enterAltScreen(writer); + try buffered_writer.flush(); + try vx.queryTerminal(tty.anyWriter(), 20 * std.time.ns_per_s); + // Initialize a View + var map_view = try View.init(alloc, &vx.unicode, .{ .max_width = map_width, .max_height = map_height }); + defer map_view.deinit(); + w = map_view.screen.width; + h = map_view.screen.height; + var map_buf: [map_width * map_height]u8 = undefined; + _ = mem.replace(u8, world_map, "\n", "", map_buf[0..]); + _ = try map_view.printSegment(.{ .text = map_buf[0..] }, .{ .wrap = .grapheme }); + + while (true) { + const event = loop.nextEvent(); + switch (event) { + .key_press => |key| { + if (key.matches('c', .{ .ctrl = true })) break; + if (key.matches(vaxis.Key.left, .{})) x -|= 1; + if (key.matches(vaxis.Key.right, .{})) x +|= 1; + if (key.matches(vaxis.Key.up, .{})) y -|= 1; + if (key.matches(vaxis.Key.down, .{})) y +|= 1; + }, + .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + } + x = @min(x, map_width -| 1); + y = @min(y, map_height -| 1); + + const win = vx.window(); + win.clear(); + try map_view.toWin( + &win, + .{ + .x = x, + .y = y, + //.width = .{ .max = 10 }, + //.height = .{ .max = 10 }, + } + ); + + // Render the screen + try vx.render(writer); + try buffered_writer.flush(); + } +} + +const _world_map = +\\ +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN+ +\\ W................................................................................................E +\\ W................................................................................................E +\\ W............................ @:::::::::@ ..................... .............................E +\\ W..... . ..... ::: ...::::::: ........ ...... %@%...........:*%@ . ..............E +\\ W..@:::::::::::::::::::: ::: ::::@ @@ ..... %#%%%%%%%%%%%%...........................* .......E +\\ W ::::@::::::::::::: .. :: ... * ....... . %%% %%%%%%%%%%%%%..................... ..* .........E +\\ W .....@::::::::::::.=::::: ........... @% @. %% %%%-...................:%% @. ..........E +\\ W...... :::%@@:@:::::::::@ :-........... @%%%%%%%%%%%%%%%%........................ .. ..........E +\\ W......@::::@@@.=-:::::=: ............. %@%%@# %%%%%%%..%%%.........@............ @=............E +\\ W..... :::::::::::::: ................. %%% @ % ........................... . . .............E +\\ W..... #:::::::::::@ .................. @=====@ .........................@ .= ............E +\\ W...... :::::@ @ ................... ============== .... ..................... .. .............E +\\ W....... .::=.. :# ................. ================ ...... ............... .................E +\\ W......... ::::... @-............... ================- ....@ ...-... . ....@ .. ...............E +\\ W............. :=.. % ...............==================+ ......@. .... ... .. @ ..............E +\\ W............... ######@ ............ ========@=@@@======@........ @.... . .. ................E +\\ W................ ######### ................. ===========@............... .@ ..@ =..............E +\\ W............... #############= ............. ========== ................. . ..*. . --- .......E +\\ W................ ###@@@#@#####%.............. ======== .................... @.@.... .*-=- ......E +\\ W................. ##@##@@#@### .............. ========-. -...................... -- + ........E +\\ W................... ##########................=======% *= .................... @-------- .......E +\\ W................... #######* ................-====== .-+....................#-@--@-@#--- ......E +\\ W................... ####### .................. ====- ........................@----------- ......E +\\ W................... ###### ....................@-* .......................... = ----:..... .E +\\ W....................####@ ......................................................... - .... -@ E +\\ W.................... ###.................................................................%-@ ...E +\\ W..................... #@........................................................................E +\\ W...................... ......................................................................E +\\ +SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS+ +\\ +; + +const world_map = +\\ +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN+ +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W........................................................................................................................................................ ............................................................................................................................................................................................................................................E +\\ W................................................................................................................................ ................................................................................................................................................................................................................................E +\\ W........................................................................................................................... @@@@@@@@%-::::---::::::=#@@@@.... ................................................................................................................................................................................................................................E +\\ W..................................................................................................................... .@@@@:::::::::::::::::::::::::::::::::::::-@@ ...............................................................................................................................................................................................................................E +\\ W.................................................................................................................... %@@@#:::::::::::::::::::::::::::::::::::::@@- ...................................................................................... .........................................................................................................................E +\\ W................................................................................................................... @:::::::::::::::::::::::::::::::::::::::::@ .................................................................................... .@....@@: .......................................................................................................................E +\\ W............................................................................................... .... +:::::::::::::::::::::::::::::::::- ........................................................................... .. #@-.................@ ......................................................................................................E +\\ W......................................................................................... +@@@@@@@@@@ .... @:::::::::::::::::::::::::::::::@: ........................................................................ @@...........................%%%%*....%%@@= ..................................................................................E +\\ W........................ ... .................... @:::::::::::::@ ........ @::::::::::::::::::::::::::::+% ............................... ....................... @%%%%@@@%%%............................................-@@ %@@*******@@ .. ............................................................E +\\ W................... *@@#+++++++++@@%=== ======== ... @@::@ +::::::::::::::::-@ ....... *:::::::::::::::::::::::::::%@ ............................. %@@@@@= .. :@%%%%%%%%%%#.........................................................................+@= ................................................E +\\ W................ :@%=:::::::::::::::::::::::%%%-.:::::::::::::+%@@ =:::::--:::::@##%%::::::::@ ..... @:::::::::::::::::::::::%@+ ............................ @@%#%%%%%%%%%%#@@* +***@@%%%%%%%%%%%%%##..........................................................................................-%@@@@@+ .........................................E +\\ W............. .*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::%@@@=:::::::::::::::@@ @::::::::@ .... @:::::::::::::::::::@@. ...................... @%%%%%%%%%%%%%%%#%%%%%%%@@%%%%@@@%%%%%%%%%#%%%%%%%%%%%%=.....................................................................................................@@@%. ....................................E +\\ W............ =#-:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::=@* @+:::::=:::+@ ... @::::::::::::::::@. @@@@@%%@@@ ................... @@%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%%%%%%%%%%%%%%............................................................................................................@@@ ...................................E +\\ W......... *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@ *%::::::::::@ @@ ... *::::::::::@@@ ... %%%%%%%%@ ................ @@%%%%%##%* -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%%......................................................................................................* @...@ ...................................E +\\ W....... =@@=::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@ =@@@@ @:::::=@ ..... *:::::::::@ ....... :=@%%@ ............. %@%%%%%%%%%@: @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#=.......................................................................................................@ ...................................E +\\ W...... @:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@: @:::-@ @@* ........ @::::::@- .............. ............... @@@%%%%%%%%%%@ @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.....................................................................................................+@@ .......................................E +\\ W..... :@::::::::::::::::#%*::::::::::::::::::::::::::::::::::::::::::::::::::::@ ...... @:::::::@ .......... ..@:@: .............................. .... *@%%%%%%%%%%%@* +@%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%....................................................................................@ .@.=@.........@ .........................................E +\\ W.... @::::::::::=%@@ #%:::::::::::::::::::::::::::::::::::::::::::::- ........ @::::::::@:@=::@ ........... ................................ -@@ .. @%%%@@%%%%%%%=# @%%%%%%%%%%%%%%%%%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.........................................................................%@@@@@####@# +....#% ............................................E +\\ W. -@-:::*@- .. @::::::::::::::::::::::::::::::::::::::::::::@+ ..... @:::::::::::::::.- ................................................... @%%%@% ... @%%%@# #@%%% : : - %= :: : +%%%%%%%%%%%%%%%%%%%%%%.......................................................................-. @.....@ ...............................................E +\\ W @:::::%@ ............. ::::::::::::::::::::::::::::::::::::::::::::::::%+ +@:::::::::::::::::@ ............................................... %@%%%= ... %@ @%%%@ @%%%%% %. . - : . . +%%%%%%%%%%%%%%%#*%##%-.......................................................................:* ..... @......@: ..............................................E +\\ W@::::+@ .................. ::::::::::::::::::::::::::::::::::::::::::::::::::.+@ @*::::::::::::::::::::@ ........................................... :@@ @%%@# . #%%- @@@%%%%%% . . - # : . *#%%%%%%%%%%%#...............................................................................-@%% .... @........ ..............................................E +\\ W ...................... :::::::::::::::::::::::::::::::::::::::::::::::::::-. @::::::::::::::::::::::::* ........................................... @%%%@ @%%%@ @@@%%%%%%%%%%%%%%%%%%% @. . - : *#. *%%%%%%%%%%%%%#.........................................................................................@ .... @.....% ..............................................E +\\ W.. ......................... ::::::::::@:@:@@@:%@@%%@@@@@=@:::::::::::::::::::::= #=:::::::::::::::::::::::@ ........................................... :@%%%@ #%%%%%@ @@%%%%%%%%%%%%%%%%%%%%%% % % . .* += *%: -%%%%%%%%%#%............................................................................................@ ... +@..# ..............................................E +\\ W............................... .@.::::::::@#@.@+@@%@*@:%@:@@#@:::::::::::::::::::::::::::::::::::::::::::::::::@ ........................................... @@@ @@%@@@@ @@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%...............................................................................................@- .... @ ..............................................E +\\ W............................... @::::::::::@@@:@+@%%@%@:%@:@@@@:::::::::::::::::::::::::::::::::::::::%@@ @:::::@ ........................................... @@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.................................................................................................@ ..... ...............................................E +\\ W............................... @:::::::::@=@:@*@#%@+@:%@:@@=@::::::::::::::::::::::::::::::::::::::*@ @::::::* ................................................ .@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##%##%%%%%%%%%%%%%.............................................................................................@@=.-: .........................................................E +\\ W............................... @:::::::::::::::::::::::::::.::::::::::::::::::::::::::::::::::::::. @@@:@-:+= ................................................ @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%#.......*%##%%%%%%%#%...........................................................................................- @.@ .........................................................E +\\ W.............................. @::::::::.:@@::@@:@@+@@.@@@:@*:@@*:@@*:::::::::::::::::::::::::::::::::@ ............................................. . @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%%#............%%%%%%%%%%..............................@@.+@-@.@.@@@................................................@ .....................................................E +\\ W............................ -=:::::::::::@@*:@@*@@+@::@:@:@*@@=@:@@@::::::::::::::::::::::::::::::@@@ ..................................................... @%%%%%%%%%#%%%%%%@-@@%%%#%%%%%%%%%%%%%%%%%%%%##%#.........%#%%%%%%%%%=...........................@@=.@@..@.@.@...............................................@ *@@ ....................................................E +\\ W........................... @:::::::::::::@=@:@@@%@+@@.@@@:@*@@:::@.@:::::::::::::::::::::@@# :%@ ...................................................... @%%%%%%%%%%%%%@@@@@- *@%#@- .@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%........#%%%%%%%%%%..........................*@@@.:*@.@.@+@..............................................@ =....# ....................................................E +\\ W.......................... @:::::::::::::+@@@:@=@%@+@::@.@:@*#@=@:@@@:::::::::::::::::::.@ ........................................................ @%%%%%%%%%%%%%%@ #@%%@@ .@%%%%%%%%%%%%%%%#%%%%%%%%%%..........%%%%%%%#%=.........................@@=@.@@@.@.@.@............................................@ . =..++ .....................................................E +\\ W......................... @::::::::::::::-=:=:=:--=:==:=:=:=::*@::=:=::::::::::::::::@# ................................................................. @%%%%%%%%%%%@@- .... .@@%%@* @%%%%@@@***%%%#......................%%%%%%%%%..................................................................................% ... .....................................................E +\\ W......................... @.:::::::::::::::::::::::::::::::::::::::::::::::::::::::-% ................................................................... @%%%%%%%%%%%@ ....... @%@ @%%#@ -...............................#%%%%#.........................................................................% #*@@....+ ..... ..@ .....................................................E +\\ W......................... @::::::::::::::::::::::::::::::::::::::::::::::::::::::@ ....................................................................... @%%%%%%%%%@: @%%%%@ :%%% .@.............................................................................................................@ @....@ ... ...@ .....................................................E +\\ W.......................... @:::::::::::::::::::::::::::::::::::::::::::::::::::::- .......................................................................... ==-@@@@@ .@@+--*#####@# @%@ @@@ @................................................................................................................= @... +...@ .....................................................E +\\ W.......................... @::::::::::::::::::::::::::::::::::::::::::::::::::::.@ ........................................................................... %@@@@@@=-============@ . @+.=.....................................................................................................@ #..@ @@%....@ .....................................................E +\\ W.......................... =@::::::::::::::::::::::::::::::::::::::::::::::::::@ ........................................................................... @=====================@ .. ...... @......................................................................................................@ +- @.....@ .....................................................E +\\ W........................... *::::::::::::::::::::::::::::::::::::::::::::::@= ............................................................................ @-======================-@+.. ... @.......................................................................................................@ .@...%*+ .......................................................E +\\ W............................ *:::::::::::::::::::::::::::::::::::::::::::@ ............................................................................. :*-============================-@:. +====-@@@@@@@@* %.........................................................................................................@ .. @..-@ @@ .........................................................E +\\ W............................ @:::@@::::::::::::::::::::::::::::-*=:::::=. ............................................................................... @==================================--===============-@#..........................................................................................................- .. *@ @.@ .........................................................E +\\ W............................. @:@ @:::::::::::::::::::.@...... #@::: ............................................................................. ##=====================================================@ *................- #.....................................................................................= ... :.. .........................................................E +\\ W.............................. @:@ @::::::::::::::::@ :-:: ............................................................................ @@-======================================================% -...............@ @..................................................................................+. ... ............................................................E +\\ W.............................. @::@ %::::::::::::::+ ........... @.: .......................................................................... @=========================================================-@ %.................@# @.:@@:...........................................................................@ ...... ..............................................................E +\\ W.............................. @:.- *-:::::::::::.@ ............ .@ ......................................................................... @============================================================-= *+.................@ . ==@@......................................................................:. .........................................................................E +\\ W............................... =@:+% @::::::::::.@ ........... ........................................................................ @-============================================================-@. @................:%%:..:@ :+-...........................................................% .........................................................................E +\\ W................................ =.@ -@::::::::.% ..... -@::::*@@ .................................................................... #-===============================================================:@ @:........................+ .. %........................................................@% ...........................................................................E +\\ W................................. =::::::::.@ ... #@ @: -@:::@@: ............................................................. @=================================================================% @........................@ ...... @ .....................-@@-@.........................** ............................................................................E +\\ W....................................... @:::::::::.@ #::% *:::=: ........................................................... *-=================================================================- @.......................@ ........ ...................@ @..................@ -@@ ....... ....................................................................E +\\ W....................................... +@.:::::::::%@@@@@@:::@ ..... @::::@ .......................................................... @-=================================================================@. @....................@ ............ :@...............@ . :-...............- ........ ..................................................................E +\\ W........................................ -@::::::::::::::::@ ....... .-+@@-: ........................................................... :==================================================================-@ @.................@ ............. @..............@ .... *................+@ ......... @..@ ..................................................................E +\\ W.......................................... @@**+:.::::::::@ ......... ........................................................... @====================================================================-+ @.............@+ ............... :*..........*@ ....... +.................@ ........ +:..- .................................................................E +\\ W............................................. *%::::::::::@ ............................................................................. +-=====================================================================*@ #........-#@ .................. #........** ........ @..............- ....... @:..@# ................................................................E +\\ W.................................................... -@::::::::% ......... .............................................................. +-========================================================================% @...:@@ ...................... @.......@ ............. =.............+ ....... @@..# ...............................................................E +\\ W........................................................ @+::::@ ...... :+: ....................................................... @-========================================================================-@ =%- ....................... @......* ............... ..............@ ......... *@@ ...............................................................E +\\ W.......................................................... @:::@ ... @@%####@. ................................................... %+===================================@@=-@@=@@@-@@-@@@-@@@=================. .#@+-=@ ........................ @.....: ................ ..= @.......-= ........... ................................................................E +\\ W............................................................. -::+@ ###################@ ................................................... #-================================-@%%-@--@=@+@@=@-@-@+@-=========================@ .......................... @...@- ................ ..@ @...@@ ..................................................................................E +\\ W............................................................. -@@:+@@@#######################@ ................................................... *-================================@=@-@=-@=@+@@=@-=-@:@=========================@ ........................... @.% ................ ...@ @@: ........ ........................................................................E +\\ W............................................................... @%########################@ ............................................... --==============================*@*@-@--@=@+@@-@-@-@+@========================-* ............................. @+ ............. @...@ ......... : ......................................................................E +\\ W................................................................... =###########################@@* .............................................. @-================---========================================================@ ............................... *.- ........... %%...@ ......... #.@ ....................................................................E +\\ W....................................................................... .##############################+@ ............................................. @%=---=-*+--%@ @-====================================================-@ .................................. ........... @.@ @...@ ....... @#....-: ....................................................................E +\\ W...................................................................... @################################=@ ............................................... @-==============================================-@ .................................... .............. @..@ @..@ ... =@.....@ .. ..............................................................E +\\ W..................................................................... @##################################@ ................................................................... +============================================--* ......................................................... *...@ @.@ @........@ .............................................................E +\\ W.................................................................... @####################################@ .................................................................. *===========================================%# ............................................................ @..#@@ @............@ .++#@.@ .... ....................................................E +\\ W................................................................... @######################################@+ ............................................................. @==========================================-@ ............................................................... +....@ @..........@# *.....% .. ............................................E +\\ W................................................................... @#############################################%@+ ......................................................... @-========================================-@ .................................................................. @-...@- @.........+: @...*@ .. @---+: .......................................E +\\ W................................................................... @##################################################@@ ...................................................... =-=====================================-@ .................................................................... @....% @........@ @....# .... #+--+##---=@% .....................................E +\\ W................................................................... %##############%@@@#@@@#@%@%@@@#@%@@#################@. ..................................................... @-===================================-@ ........................................................................ %@..@ @@@@#@ .....+ ..... @+=-----------+@ ..................................E +\\ W................................................................... @###############@@###@#@#@%@%#@%#@@@@####################@= .................................................... @-==================================@ .......................................................................... @..@ .:@@@@ ...... @------------#@ .................................E +\\ W................................................................... +%################@@#@#@#@%@%#@%#@@@@#####################* ..................................................... :-=================================@ ............................................................................. *...:@@@@# ........... #-----------@ ................................E +\\ W.................................................................... @#############%@%@#@#@#@%@##@%#@%@@#####################@ ...................................................... @=================================@ .............................................................................. @........@# ...................... @------------@ ...............................E +\\ W..................................................................... #######################################################. ....................................................... @=================================@ .............................................................................. @@%.++@ ............... .. :+:@---@ @--*% ..............................E +\\ W..................................................................... @############@@#%@%%@%@@@#@@@#@#%@@%#@@##############% ........................................................ .*=================================== .................................................................................. .............. @%%@@ .............................E +\\ W....................................................................... @###########@@#%@@@@%@@##@#@#@#@%@@#@@@#############* ........................................................ %=================================-# .... ................................................................................................. %=@@@@= . :@ .............................E +\\ W........................................................................ @#########%@@@%@@@@%@@@%@@@#@#@%###@%@###########@ ........................................................ @-=================================% ... :*@ ........................................................................................ @------+ . -=-@ .... ...............................E +\\ W......................................................................... @########@@@@%@@@@%@@#%@#@#@#@%@@%@@@########### ......................................................... @===================================% @-=+ ....................................................................................... #@@@@%@--------: @--+ .....................................E +\\ W.......................................................................... @############################%#################+ ......................................................... ===================================-@ =@@===+ .................................................................................... %=--------------= .=---=+ ....................................E +\\ W........................................................................... @@#########################################** ......................................................... ==================================-@ @-======@ ................................................................................... %+------------------*@= @-----# ....................................E +\\ W.............................................................................. @#######################################@ ......................................................... +===============================-@ =======% ................................................................................. .@-----------------------------# ....................................E +\\ W................................................................................. =#######################################@ ......................................................... -+============================-@ . @=====+ ................................................................................ :@--------------------------------%* ..................................E +\\ W................................................................................. @#####################################@ ........................................................... --==========================@ .. @====-@ .............................................................................. @--------------------------------------@ .................................E +\\ W.................................................................................. @#####################################@ ............................................................. %==========================@ ... @-=====@ ............................................................................. @-----------------------------------------% ................................E +\\ W.................................................................................. @*#################################%@ .............................................................. %=========================@ ... +-====* ............................................................................. @-----+@@-@*@@-@@@*@@@*@@#-@@=-@--@-*@@-----=@ ...............................E +\\ W.................................................................................. :*############################+@* ................................................................ %========================-@ ... @-==-@ .............................................................................. *-----@+@-@*@@+@---#@-*@*@-@*#-@--@-@+@-------* ..............................E +\\ W.................................................................................. :*###########################@ ..................................................................... @-=======================* ..... @@-@ .............................................................................. *-----@-@-@*@@--@@-#@-*@%@-@=@-@--@-@-@-------@ ..............................E +\\ W.................................................................................. :*########################### ........................................................................ =+=====================@ ....... ............................................................................... @-----@#@+@#@@+@-@-#@-*@+@*@%@-@--@-@#@+------@ ..............................E +\\ W.................................................................................. :############################@ ........................................................................ #===================*. ................................................................................................ @---------------------------------------------= ...............................E +\\ W.................................................................................. %##########################@ ......................................................................... +================-@ ................................................................................................ =--------------------------------------------# ...............................E +\\ W.................................................................................. @#########################@ ........................................................................... @================@ .................................................................................................. @-------------------------------------------@ ................................E +\\ W................................................................................... @#######################%* ............................................................................. @-===========-@: .................................................................................................... *----------------------------------------+@ .................................E +\\ W................................................................................... @#######################* ............................................................................... *:=========-@* ...................................................................................................... *----% @--------------------@ .................. ..........E +\\ W................................................................................... @#####################%= ................................................................................ .@-=-#%%%%@ ........................................................................................................ :*... @------------------@ .................. @@ ..........E +\\ W................................................................................... @####################@ ................................................................................. ........................................................................................................... ........... @--------------@ .................... @--@ .........E +\\ W................................................................................... %#################### ..................................................................................... ...................................................................................................................................... @-----------=. ..................... @---@: .......E +\\ W................................................................................... @##################% ................................................................................................................................................................................................................................... .%=---------@ .................... @#-----@ .......E +\\ W.................................................................................... @##############%@- .................................................................................................................................................................................................................................... @@@ .................. ==----=@ .......E +\\ W.................................................................................... @#############@ ......................................................................................................................................................................................................................................... ................... @+----@ .........E +\\ W..................................................................................... @############ .................................................................................................................................................................................................................................................................... %@----*@: .............E +\\ W...................................................................................... %##########= ................................................................................................................................................................................................................................................................. @-----@ ...............E +\\ W...................................................................................... @#########@ ................................................................................................................................................................................................................................................................. =@-----#@# ..................E +\\ W....................................................................................... %#######@ ................................................................................................................................................................................................................................................................. %--+@#% ....................E +\\ W....................................................................................... @#######@@. ................................................................................................................................................................................................................................................................. ........................E +\\ W........................................................................................ @########@ .................................................................................................................................................................................................................................................................. .............................E +\\ W......................................................................................... @#####%. ....................................................................................................................................................................................................................................................................................................E +\\ W.......................................................................................... @#####+ ....................................................................................................................................................................................................................................................................................................E +\\ W........................................................................................... @#####@ .................................................................................................................................................................................................................................................................................................E +\\ W............................................................................................ +@@@##%@# ...............................................................................................................................................................................................................................................................................................E +\\ W............................................................................................. +@@@@@ ...............................................................................................................................................................................................................................................................................................E +\\ W................................................................................................. ................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ W...........................................................................................................................................................................................................................................................................................................................................................................................................E +\\ +SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS+ +\\ +; +const map_width = mapWidth: { + @setEvalBranchQuota(100_000); + break :mapWidth mem.indexOfScalar(u8, world_map, '\n').?; +}; +const map_height = mapHeight: { + @setEvalBranchQuota(100_000); + break :mapHeight mem.count(u8, world_map, "\n"); +}; diff --git a/src/View.zig b/src/View.zig new file mode 100644 index 0000000..ae96655 --- /dev/null +++ b/src/View.zig @@ -0,0 +1,153 @@ +//! A View is effectively an "oversized" Window that can be written to and rendered in pieces. + +const std = @import("std"); +const mem = std.mem; + +const View = @This(); + +const Screen = @import("Screen.zig"); +const Window = @import("Window.zig"); +const Unicode = @import("Unicode.zig"); +const Cell = @import("Cell.zig"); + +/// View Allocator +alloc: mem.Allocator, +/// Underlying Screen +screen: *Screen, +/// Underlying Window +win: *Window, + + +pub const Config = struct { + max_width: usize = 10_000, + max_height: usize = 10_000, + x_pixel: usize = 0, + y_pixel: usize = 0, +}; +pub fn init(alloc: mem.Allocator, unicode: *const Unicode, config: Config) !View { + const screen = try alloc.create(Screen); + screen.* = try Screen.init( + alloc, + .{ + .cols = config.max_width, + .rows = config.max_height, + .x_pixel = config.x_pixel, + .y_pixel = config.y_pixel, + }, + unicode, + ); + const window = try alloc.create(Window); + window.* = .{ + .x_off = 0, + .y_off = 0, + .width = config.max_width, + .height = config.max_height, + .screen = screen, + }; + return .{ + .alloc = alloc, + .screen = screen, + .win = window, + }; +} + +pub fn deinit(self: *View) void { + self.alloc.destroy(self.win); + self.screen.deinit(self.alloc); + self.alloc.destroy(self.screen); +} + +///Render Config f/ `toWin()` +pub const RenderConfig = struct { + x: usize = 0, + y: usize = 0, + width: Extent = .fit, + height: Extent = .fit, + + pub const Extent = union(enum) { + fit, + max: usize, + }; +}; +/// Render a portion of this View to the provided Window (`win`). +pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !void { + //if (config.x >= self.screen.width or config.y >= self.screen.height) + // return error.PositionOutOfBounds; + const x = @min(self.screen.width - 1, config.x); + const y = @min(self.screen.height - 1, config.y); + const width = width: { + const width = switch (config.width) { + .fit => win.width, + .max => |w| @min(win.width, w), + }; + break :width @min(width, self.screen.width -| x); + }; + const height = height: { + const height = switch (config.height) { + .fit => win.height, + .max => |h| @min(win.height, h), + }; + break :height @min(height, self.screen.height -| y); + }; + //win.clear(); + for (0..height) |row| { + for (0..width) |col| { + win.writeCell( + col, + row, + self.win.readCell( + @min(self.screen.width, x +| col), + //self.screen.height -| 1 -| @min(self.screen.height, y +| row), + @min(self.screen.height, y +| row), + ) orelse { + std.log.err( + \\ Position Out of Bounds: + \\ - Pos: {d}, {d} + \\ - Size: {d}, {d} + , .{ + col, row, + self.screen.width, self.screen.height, + }, + ); + return error.PositionOutOfBounds; + }, + ); + } + } +} + +/// Writes a cell to the location in the View +pub fn writeCell(self: View, col: usize, row: usize, cell: Cell) void { + self.win.writeCell(col, row, cell); +} + +/// Reads a cell at the location in the View +pub fn readCell(self: View, col: usize, row: usize) ?Cell { + return self.win.readCell(col, row); +} + +/// Fills the View with the default cell +pub fn clear(self: View) void { + self.win.clear(); +} + +/// Returns the width of the grapheme. This depends on the terminal capabilities +pub fn gwidth(self: View, str: []const u8) usize { + return self.win.gwidth(str); +} + +/// Fills the View with the provided cell +pub fn fill(self: View, cell: Cell) void { + self.fill(cell); +} + +/// Prints segments to the View. Returns true if the text overflowed with the +/// given wrap strategy and size. +pub fn print(self: View, segments: []const Cell.Segment, opts: Window.PrintOptions) !Window.PrintResult { + return self.win.print(segments, opts); +} + +/// Print a single segment. This is just a shortcut for print(&.{segment}, opts) +pub fn printSegment(self: View, segment: Cell.Segment, opts: Window.PrintOptions) !Window.PrintResult { + return self.print(&.{segment}, opts); +} diff --git a/src/main.zig b/src/main.zig index 855bd91..125a169 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,6 +30,7 @@ pub const GraphemeCache = @import("GraphemeCache.zig"); pub const grapheme = @import("grapheme"); pub const Event = @import("event.zig").Event; pub const Unicode = @import("Unicode.zig"); +pub const View = @import("View.zig"); /// The target TTY implementation pub const Tty = switch (builtin.os.tag) {