From 751611307d449f1a853f395f745d7b6aa971c8ec Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 12 Sep 2022 14:42:09 +0200 Subject: [PATCH] Port to crossterm --- Cargo.lock | 383 +++++++++++++++---------------------------------- Cargo.toml | 4 +- src/display.rs | 126 ++++++++-------- src/main.rs | 63 +++++--- 4 files changed, 221 insertions(+), 355 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e7b427c..9485369 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,13 +3,10 @@ version = 3 [[package]] -name = "ansi_term" -version = "0.12.1" +name = "autocfg" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" @@ -17,12 +14,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -30,70 +21,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "clearscreen" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c969a6b6dadff9f3349b1f783f553e2411104763ca4789e1c6ca6a41f46a57b0" -dependencies = [ - "nix", - "terminfo", - "thiserror", - "which", - "winapi", -] - -[[package]] -name = "dirs" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" -dependencies = [ - "cfg-if 0.1.10", - "dirs-sys", -] - -[[package]] -name = "dirs-sys" -version = "0.3.7" +name = "crossterm" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" dependencies = [ + "bitflags", + "crossterm_winapi", "libc", - "redox_users", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", "winapi", ] [[package]] -name = "either" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" - -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "getch" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13990e2d5b29e1770ddf7fc000afead4acb9bd8f8a9602de63bf189e261b1ba8" -dependencies = [ - "libc", - "termios", -] - -[[package]] -name = "getrandom" -version = "0.1.16" +name = "crossterm_winapi" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c" dependencies = [ - "cfg-if 1.0.0", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", + "winapi", ] [[package]] @@ -102,9 +51,9 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -126,74 +75,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] -name = "memchr" -version = "2.5.0" +name = "lock_api" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - -[[package]] -name = "nix" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "195cdbc1741b8134346d515b3a56a1c94b0912758009cfd53f99ea0f57b065fc" -dependencies = [ - "bitflags", - "cfg-if 1.0.0", - "libc", -] - -[[package]] -name = "nom" -version = "5.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" +checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" dependencies = [ - "memchr", - "version_check", + "autocfg", + "scopeguard", ] [[package]] -name = "once_cell" -version = "1.14.0" +name = "log" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" - -[[package]] -name = "phf" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ - "phf_shared", + "cfg-if", ] [[package]] -name = "phf_codegen" -version = "0.8.0" +name = "mio" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" dependencies = [ - "phf_generator", - "phf_shared", + "libc", + "log", + "wasi", + "windows-sys", ] [[package]] -name = "phf_generator" -version = "0.8.0" +name = "parking_lot" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ - "phf_shared", - "rand 0.7.3", + "lock_api", + "parking_lot_core", ] [[package]] -name = "phf_shared" -version = "0.8.0" +name = "parking_lot_core" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" dependencies = [ - "siphasher", + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", ] [[package]] @@ -202,38 +134,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" -[[package]] -name = "proc-macro2" -version = "1.0.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc", - "rand_pcg", -] - [[package]] name = "rand" version = "0.8.5" @@ -241,18 +141,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha 0.3.1", - "rand_core 0.6.3", -] - -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", + "rand_chacha", + "rand_core", ] [[package]] @@ -262,16 +152,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core 0.6.3", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", + "rand_core", ] [[package]] @@ -280,25 +161,7 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ - "getrandom 0.2.7", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", -] - -[[package]] -name = "rand_pcg" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" -dependencies = [ - "rand_core 0.5.1", + "getrandom", ] [[package]] @@ -310,141 +173,125 @@ dependencies = [ "bitflags", ] -[[package]] -name = "redox_users" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" -dependencies = [ - "getrandom 0.2.7", - "redox_syscall", - "thiserror", -] - [[package]] name = "rs2048" version = "0.1.0" dependencies = [ - "ansi_term", - "clearscreen", - "getch", + "crossterm", "glam", "hsl", - "rand 0.8.5", + "rand", ] [[package]] -name = "siphasher" -version = "0.3.10" +name = "scopeguard" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] -name = "syn" -version = "1.0.99" +name = "signal-hook" +version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d" dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", + "libc", + "signal-hook-registry", ] [[package]] -name = "terminfo" -version = "0.7.3" +name = "signal-hook-mio" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76971977e6121664ec1b960d1313aacfa75642adc93b9d4d53b247bd4cb1747e" +checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" dependencies = [ - "dirs", - "fnv", - "nom", - "phf", - "phf_codegen", + "libc", + "mio", + "signal-hook", ] [[package]] -name = "termios" -version = "0.3.3" +name = "signal-hook-registry" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "411c5bf740737c7918b8b1fe232dca4dc9f8e754b8ad5e20966814001ed0ac6b" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" dependencies = [ "libc", ] [[package]] -name = "thiserror" -version = "1.0.34" +name = "smallvec" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c1b05ca9d106ba7d2e31a9dab4a64e7be2cce415321966ea3132c49a656e252" -dependencies = [ - "thiserror-impl", -] +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" [[package]] -name = "thiserror-impl" -version = "1.0.34" +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8f2591983642de85c921015f3f070c665a197ed69e417af436115e3a1407487" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ - "proc-macro2", - "quote", - "syn", + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] [[package]] -name = "unicode-ident" -version = "1.0.3" +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] -name = "version_check" -version = "0.9.4" +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" +name = "windows-sys" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] [[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" +name = "windows_aarch64_msvc" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" [[package]] -name = "which" -version = "4.3.0" +name = "windows_i686_gnu" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c831fbbee9e129a8cf93e7747a82da9d95ba8e16621cae60ec2cdc849bacb7b" -dependencies = [ - "either", - "libc", - "once_cell", -] +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" [[package]] -name = "winapi" -version = "0.3.9" +name = "windows_i686_msvc" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows_x86_64_gnu" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows_x86_64_msvc" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml index a7862f7..475d02f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -ansi_term = "0.12.1" -clearscreen = "1.0.10" -getch = "0.3.1" +crossterm = "0.25.0" glam = "0.21.3" hsl = "0.1.1" rand = "0.8.5" diff --git a/src/display.rs b/src/display.rs index 6d750de..fba426a 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,6 +1,5 @@ use super::game::{Board, Pos}; -use ansi_term::Color; -use std::fmt; +use crossterm::{cursor, queue, style::*}; enum Mode { Roof_, @@ -13,20 +12,19 @@ enum Mode { const FIELD_HEIGHT: usize = 3; const FIELD_WIDTH: usize = 8; -fn write_line(f: &mut fmt::Formatter, vec: &[u32], mode: Mode) -> fmt::Result { +fn write_line(stdout: &mut std::io::Stdout, vec: &[u32], mode: Mode) -> crossterm::Result<()> { let mut vec = vec; let len = vec.len(); - write!( - f, - "{}", - match mode { + queue!( + stdout, + Print(match mode { Mode::Roof_ => "┏", Mode::Data_ => "┃", Mode::Floor => "┠", Mode::Empty => "┃", Mode::Base_ => "┗", - } + }) )?; for i in 0..len { @@ -34,11 +32,11 @@ fn write_line(f: &mut fmt::Formatter, vec: &[u32], mode: Mode) -> fmt::Result { vec = &vec[0..vec.len() - 1]; match mode { - Mode::Data_ | Mode::Empty => write!(f, "\x1b[0m ")?, + Mode::Data_ | Mode::Empty => queue!(stdout, Print(" "))?, _ => {} } - let color = match mode { + match mode { Mode::Data_ | Mode::Empty if n != 0 => { let (r, g, b) = hsl::HSL { h: (n * 360 / 12) as f64, @@ -47,102 +45,100 @@ fn write_line(f: &mut fmt::Formatter, vec: &[u32], mode: Mode) -> fmt::Result { } .to_rgb(); - Color::Black.on(Color::RGB(r, g, b)) + queue!( + stdout, + SetColors(Colors::new(Color::Black, Color::Rgb { r, g, b })) + )?; } - _ => Color::White.on(Color::Black), + _ => {} }; if let Mode::Data_ = mode { if n == 0 { - write!(f, "{}", " ".repeat(FIELD_WIDTH - 2))?; + queue!(stdout, Print(" ".repeat(FIELD_WIDTH - 2)))?; } else { - write!( - f, - "{}", - color.paint(format!("{:^w$}", 1 << n, w = FIELD_WIDTH - 2)) + queue!( + stdout, + Print(format!("{:^w$}", 1 << n, w = FIELD_WIDTH - 2)) )?; } } else { - write!( - f, - "{}", - match mode { + queue!( + stdout, + Print(match mode { Mode::Roof_ | Mode::Base_ => "━".repeat(FIELD_WIDTH), Mode::Floor => "─".repeat(FIELD_WIDTH), - Mode::Empty => color.paint(" ".repeat(FIELD_WIDTH - 2)).to_string(), + Mode::Empty => " ".repeat(FIELD_WIDTH - 2), Mode::Data_ => panic!("unreachable"), - } + }) )?; } match mode { - Mode::Data_ | Mode::Empty => write!(f, " ")?, + Mode::Data_ | Mode::Empty => { + queue!(stdout, Print(" "), SetAttribute(Attribute::Reset))? + } _ => {} } if i != len - 1 { - write!( - f, - "{}", - match mode { + queue!( + stdout, + Print(match mode { Mode::Roof_ => "┯", Mode::Data_ => "│", Mode::Floor => "┼", Mode::Empty => "│", Mode::Base_ => "┷", - } + }) )?; } } - write!( - f, - "{}", - match mode { + queue!( + stdout, + Print(match mode { Mode::Roof_ => "┓", Mode::Data_ => "┃", Mode::Floor => "┨", Mode::Empty => "┃", Mode::Base_ => "┛", - } + }), + cursor::MoveToNextLine(1), )?; - writeln!(f)?; - Ok(()) } -impl fmt::Display for Board { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let dummy = vec![0; self.size.x as usize]; - - write_line(f, &dummy, Mode::Roof_)?; - - for y in 0..self.size.y { - let vec = (0..self.size.x) - .rev() - .map(|x| self.get(Pos::new(x, y)).value()) - .collect::>(); - - for i in 0..FIELD_HEIGHT { - write_line( - f, - &vec, - if i == FIELD_HEIGHT / 2 { - Mode::Data_ - } else { - Mode::Empty - }, - )?; - } +pub fn display_board(stdout: &mut std::io::Stdout, board: &Board) -> crossterm::Result<()> { + let dummy = vec![0; board.size.x as usize]; + + write_line(stdout, &dummy, Mode::Roof_)?; + + for y in 0..board.size.y { + let vec = (0..board.size.x) + .rev() + .map(|x| board.get(Pos::new(x, y)).value()) + .collect::>(); + + for i in 0..FIELD_HEIGHT { + write_line( + stdout, + &vec, + if i == FIELD_HEIGHT / 2 { + Mode::Data_ + } else { + Mode::Empty + }, + )?; + } - if y != self.size.y - 1 { - write_line(f, &dummy, Mode::Floor)?; - } + if y != board.size.y - 1 { + write_line(stdout, &dummy, Mode::Floor)?; } + } - write_line(f, &dummy, Mode::Base_)?; + write_line(stdout, &dummy, Mode::Base_)?; - Ok(()) - } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index a7b3e59..e04f92c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,56 @@ pub mod display; pub mod game; +use crossterm::{cursor, event, execute, queue, terminal}; use game::{Board, Dir::*, Pos}; +use std::io::Write; fn main() { let mut rng = rand::thread_rng(); - let getch = getch::Getch::new(); - let board = Board::new(Pos::new(4, 4)); + let mut stdout = std::io::stdout(); + queue!(stdout, terminal::EnterAlternateScreen, cursor::Hide).unwrap(); + + terminal::enable_raw_mode().unwrap(); + + let board = Board::new(Pos::new(4, 4)); board.spawn(&mut rng); - clearscreen::clear().unwrap(); - print!("{board}"); - - while let Ok(ch) = getch.getch() { - if !board.step(match ch { - b'w' => Up, - b'a' => Left, - b's' => Down, - b'd' => Right, - b'q' => break, - _ => continue, - }) { - continue; - } + board.spawn(&mut rng); + + loop { + queue!( + stdout, + terminal::Clear(terminal::ClearType::All), + cursor::MoveTo(0, 0) + ) + .unwrap(); + display::display_board(&mut stdout, &board).unwrap(); + stdout.flush().unwrap(); - board.spawn(&mut rng); - clearscreen::clear().unwrap(); - print!("{board}"); + if let Ok(evt) = event::read() { + match evt { + event::Event::Key(event::KeyEvent { code, .. }) => match code { + event::KeyCode::Char(ch) => { + if board.step(match ch.to_ascii_lowercase() { + 'w' => Up, + 'a' => Left, + 's' => Down, + 'd' => Right, + 'q' => break, + _ => continue, + }) { + board.spawn(&mut rng); + } + } + _ => {} + }, + _ => {} + } + } else { + break; + } } + + terminal::disable_raw_mode().unwrap(); + execute!(stdout, cursor::Show, terminal::LeaveAlternateScreen).unwrap(); } -- 2.44.0