From 2d5e6c6a0758a39ac16de217bc4bd6bd53104d1c Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Mon, 7 Jun 2021 03:28:22 -0700 Subject: wooooo! switch to using tokio; make ^C work properly --- Cargo.lock | 482 +++++++++++------------------------------------ Cargo.toml | 6 +- src/main.rs | 15 +- src/prelude.rs | 3 - src/terminal.rs | 89 ++++----- src/terminal/decoding.rs | 35 +--- 6 files changed, 174 insertions(+), 456 deletions(-) delete mode 100644 src/prelude.rs diff --git a/Cargo.lock b/Cargo.lock index b6d2c4d..66608c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,124 +30,6 @@ dependencies = [ "term", ] -[[package]] -name = "async-channel" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59740d83946db6a5af71ae25ddf9562c2b176b2ca42cf99a455f09f4a220d6b9" -dependencies = [ - "concurrent-queue", - "event-listener", - "futures-core", -] - -[[package]] -name = "async-executor" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb877970c7b440ead138f6321a3b5395d6061183af779340b65e20c0fede9146" -dependencies = [ - "async-task", - "concurrent-queue", - "fastrand", - "futures-lite", - "once_cell", - "vec-arena", -] - -[[package]] -name = "async-global-executor" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6" -dependencies = [ - "async-channel", - "async-executor", - "async-io", - "async-mutex", - "blocking", - "futures-lite", - "num_cpus", - "once_cell", -] - -[[package]] -name = "async-io" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9315f8f07556761c3e48fec2e6b276004acf426e6dc068b2c2251854d65ee0fd" -dependencies = [ - "concurrent-queue", - "fastrand", - "futures-lite", - "libc", - "log", - "nb-connect", - "once_cell", - "parking", - "polling", - "vec-arena", - "waker-fn", - "winapi", -] - -[[package]] -name = "async-lock" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1996609732bde4a9988bc42125f55f2af5f3c36370e27c778d5191a4a1b63bfb" -dependencies = [ - "event-listener", -] - -[[package]] -name = "async-mutex" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" -dependencies = [ - "event-listener", -] - -[[package]] -name = "async-std" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341" -dependencies = [ - "async-channel", - "async-global-executor", - "async-io", - "async-lock", - "crossbeam-utils 0.8.1", - "futures-channel", - "futures-core", - "futures-io", - "futures-lite", - "gloo-timers", - "kv-log-macro", - "log", - "memchr", - "num_cpus", - "once_cell", - "pin-project-lite", - "pin-utils", - "slab", - "wasm-bindgen-futures", -] - -[[package]] -name = "async-task" -version = "4.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" - -[[package]] -name = "atomic-waker" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" - [[package]] name = "atty" version = "0.2.14" @@ -224,26 +106,6 @@ dependencies = [ "byte-tools", ] -[[package]] -name = "blocking" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9" -dependencies = [ - "async-channel", - "async-task", - "atomic-waker", - "fastrand", - "futures-lite", - "once_cell", -] - -[[package]] -name = "bumpalo" -version = "3.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f07aa6688c702439a1be0307b6a94dffe1168569e45b9500c1372bc580740d59" - [[package]] name = "byte-tools" version = "0.3.1" @@ -257,10 +119,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" [[package]] -name = "cache-padded" -version = "1.1.1" +name = "bytes" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" [[package]] name = "cc" @@ -280,15 +142,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "concurrent-queue" -version = "1.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" -dependencies = [ - "cache-padded", -] - [[package]] name = "constant_time_eq" version = "0.1.5" @@ -306,17 +159,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "crossbeam-utils" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d" -dependencies = [ - "autocfg", - "cfg-if 1.0.0", - "lazy_static", -] - [[package]] name = "diff" version = "0.1.12" @@ -370,69 +212,18 @@ dependencies = [ "log", ] -[[package]] -name = "event-listener" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" - [[package]] name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" -[[package]] -name = "fastrand" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca5faf057445ce5c9d4329e382b2ce7ca38550ef3b73a5348362d5f24e0c7fe3" -dependencies = [ - "instant", -] - [[package]] name = "fixedbitset" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d" -[[package]] -name = "futures-channel" -version = "0.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2d31b7ec7efab6eefc7c57233bb10b847986139d88cc2f5a02a1ae6871a1846" -dependencies = [ - "futures-core", -] - -[[package]] -name = "futures-core" -version = "0.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79e5145dde8da7d1b3892dad07a9c98fc04bc39892b1ecc9692cf53e2b780a65" - -[[package]] -name = "futures-io" -version = "0.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500" - -[[package]] -name = "futures-lite" -version = "1.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4481d0cd0de1d204a4fa55e7d45f07b1d958abcb06714b3446438e2eff695fb" -dependencies = [ - "fastrand", - "futures-core", - "futures-io", - "memchr", - "parking", - "pin-project-lite", - "waker-fn", -] - [[package]] name = "generic-array" version = "0.12.3" @@ -453,19 +244,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "gloo-timers" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" -dependencies = [ - "futures-channel", - "futures-core", - "js-sys", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "hashbrown" version = "0.9.0" @@ -509,24 +287,6 @@ dependencies = [ "either", ] -[[package]] -name = "js-sys" -version = "0.3.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cfb73131c35423a367daf8cbd24100af0d077668c8c2943f0e7dd775fef0f65" -dependencies = [ - "wasm-bindgen", -] - -[[package]] -name = "kv-log-macro" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" -dependencies = [ - "log", -] - [[package]] name = "lalrpop" version = "0.19.1" @@ -569,9 +329,18 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.82" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" + +[[package]] +name = "lock_api" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" +checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +dependencies = [ + "scopeguard", +] [[package]] name = "log" @@ -589,12 +358,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" [[package]] -name = "nb-connect" -version = "1.0.2" +name = "mio" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998" +checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" dependencies = [ "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ "winapi", ] @@ -616,6 +397,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -639,10 +429,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" [[package]] -name = "parking" -version = "2.0.0" +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" +checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +dependencies = [ + "cfg-if 1.0.0", + "instant", + "libc", + "redox_syscall 0.2.8", + "smallvec", + "winapi", +] [[package]] name = "petgraph" @@ -695,19 +504,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "polling" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2a7bc6b2a29e632e45451c941832803a18cce6781db04de8a04696cdca8bde4" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "log", - "wepoll-sys", - "winapi", -] - [[package]] name = "precomputed-hash" version = "0.1.1" @@ -738,6 +534,15 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "redox_syscall" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +dependencies = [ + "bitflags", +] + [[package]] name = "redox_users" version = "0.3.5" @@ -745,7 +550,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" dependencies = [ "getrandom", - "redox_syscall", + "redox_syscall 0.1.57", "rust-argon2", ] @@ -776,9 +581,15 @@ dependencies = [ "base64", "blake2b_simd", "constant_time_eq", - "crossbeam-utils 0.7.2", + "crossbeam-utils", ] +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "serde" version = "1.0.115" @@ -815,23 +626,12 @@ dependencies = [ name = "shell" version = "0.1.0" dependencies = [ - "async-std", "lalrpop", "lalrpop-util", "nix", "pin-project", "pin-utils", - "signal-hook", -] - -[[package]] -name = "signal-hook" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "780f5e3fe0c66f67197236097d89de1e86216f1f6fdeaf47c442f854ab46c240" -dependencies = [ - "libc", - "signal-hook-registry", + "tokio", ] [[package]] @@ -850,10 +650,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7" [[package]] -name = "slab" -version = "0.4.2" +name = "smallvec" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "string_cache" @@ -906,119 +706,53 @@ dependencies = [ ] [[package]] -name = "typenum" -version = "1.12.0" +name = "tokio" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" - -[[package]] -name = "unicode-xid" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" - -[[package]] -name = "vec-arena" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eafc1b9b2dfc6f5529177b62cf806484db55b32dc7c9658a118e11bbeb33061d" - -[[package]] -name = "waker-fn" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" - -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - -[[package]] -name = "wasm-bindgen" -version = "0.2.70" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be" -dependencies = [ - "cfg-if 1.0.0", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.70" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7" -dependencies = [ - "bumpalo", - "lazy_static", - "log", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3de431a2910c86679c34283a33f66f4e4abd7e0aec27b6669060148872aadf94" -dependencies = [ - "cfg-if 1.0.0", - "js-sys", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.70" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c" +checksum = "0a38d31d7831c6ed7aad00aa4c12d9375fd225a6dd77da1d25b707346319a975" dependencies = [ - "quote", - "wasm-bindgen-macro-support", + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "tokio-macros", + "winapi", ] [[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.70" +name = "tokio-macros" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385" +checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37" dependencies = [ "proc-macro2", "quote", "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", ] [[package]] -name = "wasm-bindgen-shared" -version = "0.2.70" +name = "typenum" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd4945e4943ae02d15c13962b38a5b1e81eadd4b71214eee75af64a4d6a4fd64" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" [[package]] -name = "web-sys" -version = "0.3.47" +name = "unicode-xid" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c40dc691fc48003eba817c38da7113c15698142da971298003cac3ef175680b3" -dependencies = [ - "js-sys", - "wasm-bindgen", -] +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] -name = "wepoll-sys" -version = "3.0.1" +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff" -dependencies = [ - "cc", -] +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "winapi" diff --git a/Cargo.toml b/Cargo.toml index fe931da..210d96e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,10 @@ lalrpop-util = "0.19" nix = "0.19" pin-project = "1.0.4" pin-utils = "0.1.0" -signal-hook = "0.3.4" -[dependencies.async-std] -version = "1.7.0" +[dependencies.tokio] +version = "1.6.1" +features = [ "full" ] [build-dependencies] lalrpop = { version = "0.19", features = [ "lexer" ] } diff --git a/src/main.rs b/src/main.rs index 935155c..e94ce0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,26 @@ #![forbid(unsafe_code)] -use crate::prelude::*; - use crate::result::Result; use crate::terminal::{Input, Terminal}; -use async_std::io; use std::collections::HashMap; use std::collections::HashSet; use std::env; use std::os::unix::fs::PermissionsExt; use std::process::{self, Command}; +use tokio::io::{self, AsyncWriteExt}; #[macro_use] extern crate lalrpop_util; lalrpop_mod!(pub commandline); pub mod error; pub mod path; -pub mod prelude; pub mod result; pub mod terminal; -fn main() -> Result<()> { - let result = async_std::task::block_on(async { repl().await }); +#[tokio::main] +async fn main() -> Result<()> { + let result = repl().await; process::exit(match result { Ok(()) => 0, Err(ref e) => { @@ -61,8 +59,9 @@ async fn repl() -> Result<()> { async fn prompt() -> Result<()> { - print!("\n$ "); - io::stdout().flush().await?; + let mut stdout = io::stdout(); + stdout.write_all("\n$ ".as_bytes()).await?; + stdout.flush().await?; Ok(()) } diff --git a/src/prelude.rs b/src/prelude.rs deleted file mode 100644 index f678ba1..0000000 --- a/src/prelude.rs +++ /dev/null @@ -1,3 +0,0 @@ -#![forbid(unsafe_code)] -pub use async_std::prelude::*; - diff --git a/src/terminal.rs b/src/terminal.rs index 73ba035..f42d153 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,15 +1,15 @@ #![forbid(unsafe_code)] -use crate::prelude::*; use crate::terminal::prelude::*; use crate::terminal::decoding::CharBufReader; -use async_std::io::{self, Read}; -use async_std::os::unix::io::{AsRawFd, RawFd}; -use async_std::sync::{Arc, Mutex}; use nix::sys::termios::{self, Termios}; -use signal_hook::consts::TERM_SIGNALS; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::sync::Arc; +use tokio::io::{self, AsyncRead, AsyncWriteExt}; +use tokio::signal::unix::{signal, SignalKind}; +use tokio::sync::{mpsc, Mutex}; +use tokio::task; pub mod decoding; pub mod error; @@ -87,31 +87,41 @@ enum InputAction { } -pub struct Terminal { +pub struct Terminal { reader: Arc>>, - //reader: Arc>, line_buffer: LineBuffer, file_descriptor: RawFd, initial_termios: Termios, - is_interrupted: Arc, + interrupt_receiver: mpsc::Receiver<()>, } -impl Terminal { +async fn handle_signals(interrupt_sender: mpsc::Sender<()>) -> Result<()> +{ + let mut stream = signal(SignalKind::interrupt()).map_err(error::internal)?; + /* TODO make it work on other signals: + SignalKind::hangup(); + SignalKind::interrupt(); + SignalKind::terminate(); + SignalKind::quit(); + */ + + loop { + stream.recv().await; + interrupt_sender.send(()).await.map_err(error::internal)?; + } +} + + +impl Terminal { pub fn init(input_stream: InputStream) -> Result> { - let is_interrupted = Arc::new(AtomicBool::new(false)); + let (interrupt_sender, interrupt_receiver) = mpsc::channel(1); - for signal in TERM_SIGNALS { - signal_hook::flag::register(*signal, Arc::clone(&is_interrupted)) - .map_err(error::internal)?; - } + let _ = task::spawn(handle_signals(interrupt_sender)); let fd = input_stream.as_raw_fd(); let termios = termios::tcgetattr(fd).map_err(error::mode_setting)?; - let reader = Arc::new(Mutex::new(CharBufReader::new( - input_stream, Arc::clone(&is_interrupted)))); - //let reader = Arc::new(CharBufReader::new( - //input_stream, Arc::clone(&is_interrupted))); + let reader = Arc::new(Mutex::new(CharBufReader::new(input_stream))); let line_buffer = LineBuffer::new(); let terminal = Terminal { @@ -119,7 +129,7 @@ impl Terminal { line_buffer: line_buffer, file_descriptor: fd, initial_termios: termios, - is_interrupted: is_interrupted, + interrupt_receiver: interrupt_receiver, }; terminal.init_modes()?; @@ -158,8 +168,6 @@ impl Terminal { pub fn cleanup_modes(&self) -> Result<()> { - println!("de-initializing"); // DO NOT SUBMIT - let termios = self.initial_termios.clone(); termios::tcsetattr(self.file_descriptor, @@ -171,23 +179,20 @@ impl Terminal { } - pub fn is_exiting(&self) -> bool { - self.is_interrupted.load(Ordering::Relaxed) - } - - pub async fn handle_input(&mut self) -> Result { - let is_interrupted = Arc::clone(&self.is_interrupted); - loop { let mut action: Option = None; - let string = CharBufReader::fill_buf(Arc::clone(&self.reader)).await.map_err(error::input)?; - - if is_interrupted.load(Ordering::Relaxed) { - break; - } + let string = tokio::select! { + result = CharBufReader::fill_buf(Arc::clone(&self.reader)) => { + let string: String = result.map_err(error::input)?; + string + } + _ = self.interrupt_receiver.recv() => { + return Ok(Input::End); + } + }; let mut chars = string.char_indices(); @@ -201,8 +206,11 @@ impl Terminal { } _ => { self.line_buffer.insert(&c.to_string()); - print!("{}", c); - io::stdout().flush(); + + let mut stdout = io::stdout(); + stdout.write_all(format!("{}", c).as_bytes()).await + .map_err(error::internal)?; + stdout.flush().await.map_err(error::internal)?; } } @@ -230,13 +238,8 @@ impl Terminal { } println!("line buffer {:?}", self.line_buffer); - if self.is_interrupted.load(Ordering::Relaxed) { - println!("exiting 3"); - Ok(Input::End) - } else { - let input = Input::String(self.line_buffer.as_string()); - Ok(input) - } + let input = Input::String(self.line_buffer.as_string()); + Ok(input) } } diff --git a/src/terminal/decoding.rs b/src/terminal/decoding.rs index 11f6046..018730e 100644 --- a/src/terminal/decoding.rs +++ b/src/terminal/decoding.rs @@ -3,33 +3,31 @@ use crate::terminal::prelude::*; use crate::terminal::error; -use async_std::future::Future; -use async_std::io::{BufRead, BufReader, Read}; -use async_std::sync::{Arc, Mutex}; -use async_std::task::{Context, Poll}; use pin_project::pin_project; use pin_utils::pin_mut; +use std::future::Future; use std::pin::Pin; use std::str; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; +use std::task::{Context, Poll}; +use tokio::io::{AsyncBufRead, AsyncRead, BufReader}; +use tokio::sync::Mutex; #[pin_project] -pub struct CharBufReader { +pub struct CharBufReader { #[pin] byte_reader: BufReader, #[pin] char_buffer: String, - is_interrupted: Arc, } #[pin_project] -struct FillBufFuture { +struct FillBufFuture { char_reader: Arc>>, - //char_reader: Arc>, } -impl Future for FillBufFuture { +impl Future for FillBufFuture { type Output = Result; fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) @@ -45,16 +43,9 @@ impl Future for FillBufFuture { let mut byte_reader: Pin<&mut BufReader> = Pin::new(&mut char_reader.byte_reader); loop { - if char_reader.is_interrupted.load(Ordering::Relaxed) { - println!("char reader got interrupt"); - return Poll::Pending; - } - - println!("about to fill_buf"); match byte_reader.as_mut().poll_fill_buf(context).map_err(error::input)? { Poll::Ready(byte_buffer) => { - println!("done with fill_buf"); match str::from_utf8(&byte_buffer) { Err(error) => { let n_valid = error.valid_up_to(); @@ -86,7 +77,6 @@ impl Future for FillBufFuture { } } Poll::Pending => { - println!("fill_buf pending"); return Poll::Pending; } } @@ -95,7 +85,6 @@ impl Future for FillBufFuture { return Poll::Ready(Ok(char_reader.char_buffer.to_string())); } Poll::Pending => { - println!("char_reader mutex pending"); return Poll::Pending; } } @@ -103,21 +92,17 @@ impl Future for FillBufFuture { } -impl CharBufReader { - pub fn new(input_stream: R, is_interrupted: Arc) - -> CharBufReader - { +impl CharBufReader { + pub fn new(input_stream: R) -> CharBufReader { let byte_reader = BufReader::new(input_stream); CharBufReader { byte_reader: byte_reader, char_buffer: String::new(), - is_interrupted: is_interrupted, } } pub fn fill_buf(reader: Arc>) - //pub fn fill_buf(reader: Arc) -> impl Future> { FillBufFuture { -- cgit 1.4.1