diff options
| author | nsfisis <nsfisis@gmail.com> | 2022-12-22 20:44:23 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2022-12-22 20:45:15 +0900 |
| commit | ec557a8be01a4290a1ac8bca31f7c734035ccf7d (patch) | |
| tree | 25e8b331f3c3c11e39fabc20b11587fa64278763 | |
| parent | 07f2ccd045c904237fbd3cc0256d4f5e3636fc46 (diff) | |
| download | turbofish-aquarium-ec557a8be01a4290a1ac8bca31f7c734035ccf7d.tar.gz turbofish-aquarium-ec557a8be01a4290a1ac8bca31f7c734035ccf7d.tar.zst turbofish-aquarium-ec557a8be01a4290a1ac8bca31f7c734035ccf7d.zip | |
implement
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 118 | ||||
| -rw-r--r-- | Cargo.toml | 10 | ||||
| -rw-r--r-- | src/main.rs | 116 |
4 files changed, 245 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d2b5d8f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,118 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "numtoa" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_termios" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f" +dependencies = [ + "redox_syscall", +] + +[[package]] +name = "termion" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "659c1f379f3408c7e5e84c7d0da6d93404e3800b6b9d063ba24436419302ec90" +dependencies = [ + "libc", + "numtoa", + "redox_syscall", + "redox_termios", +] + +[[package]] +name = "turbofish-aquarium" +version = "0.1.0" +dependencies = [ + "rand", + "termion", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..157ee39 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "turbofish-aquarium" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" +termion = "2.0.1" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..606326e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,116 @@ +use rand::{thread_rng, Rng}; +use std::{ + io::{stdout, Read, Write}, + thread::sleep, + time::Duration, +}; +use termion::{async_stdin, raw::IntoRawMode, terminal_size}; + +const N_FISH: usize = 64; + +struct Turbofish { + x: i16, + y: u16, + speed: i16, + bold: bool, +} + +impl Turbofish { + fn text(&self, offset: i16) -> String { + let s = if self.speed < 0 { "<>::" } else { "::<>" }; + let s = if offset <= -4 { + "" + } else if offset < 0 { + &s[(-offset as usize)..] + } else if offset >= 4 { + "" + } else if offset > 0 { + &s[0..(4 - offset as usize)] + } else { + s + }; + s.to_string() + } + + fn tick(&mut self) { + self.x += self.speed; + self.bold = !self.bold; + } +} + +fn clear(mut stdout: impl Write) { + write!(stdout, "{}", termion::clear::All).unwrap(); +} + +fn update(school_of_fish: &mut [Turbofish], columns: u16) { + for fish in school_of_fish.iter_mut() { + fish.tick(); + if fish.x < -10 || fish.x > columns as i16 + 10 { + fish.speed *= -1; + } + } +} + +fn render(mut stdout: impl Write, school_of_fish: &Vec<Turbofish>, columns: u16) { + for fish in school_of_fish { + let x = fish.x; + let y = fish.y; + if fish.bold { + write!(stdout, "{}", termion::style::Bold).unwrap(); + } + write!( + stdout, + "{}{}{}", + termion::cursor::Goto(x.clamp(0, columns as i16 - 1) as u16 + 1, y + 1), + fish.text(if x < 0 { + x + } else if (x as u16) + 4 >= columns { + x + 4 - columns as i16 + } else { + 0 + }), + termion::style::Reset, + ) + .unwrap(); + } +} + +fn main() { + let mut stdin = async_stdin().bytes(); + let mut stdout = stdout().into_raw_mode().unwrap(); + let mut rng = thread_rng(); + + let (columns, lines) = terminal_size().unwrap(); + + let mut school_of_fish = Vec::with_capacity(N_FISH); + for _i in 0..N_FISH { + let x = rng.gen_range(0..(columns as i16 - 1)); + let y = rng.gen_range(0..(lines - 1)); + let speed = [-2, -1, 1, 2][rng.gen_range(0..4)]; + school_of_fish.push(Turbofish { + x, + y, + speed, + bold: rng.gen(), + }); + } + + clear(&mut stdout); + stdout.flush().unwrap(); + + loop { + if let Some(Ok(b'q')) = stdin.next() { + break; + } + + clear(&mut stdout); + update(&mut school_of_fish, columns); + render(&mut stdout, &school_of_fish, columns); + stdout.flush().unwrap(); + + sleep(Duration::from_millis(100)); + } + + clear(&mut stdout); + stdout.flush().unwrap(); +} |
