Open novohool opened 11 months ago
extern crate crossterm;
use crossterm::{terminal, ExecutableCommand, QueueableCommand}; use std::{thread, time};
struct Incense { smoke: char, smoke_lines_offset: u32, smoke_columns_offset: u32, incense_pattern: &'static str, }
impl Incense { fn new() -> Incense { Incense { smoke: ',', smoke_lines_offset: 0, smoke_columns_offset: 5, incense_pattern: " ╱╱┏╮ ╱╱╱╱╱┃┃ ╱╱╱╱╱╱┃┃ ┛┛┛┛┛┛┃┃ ╱╱╱╱╱╱┃┃ ╱╱╱╱╱╱ ┃┃ ╱╱╱╱╱╱ ┃┃ ╱╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱╱╱╱ ", } }
fn format_incense_pattern(&mut self) { let (columns, lines) = terminal::size().unwrap(); let center_lines = lines / 2 - self.incense_pattern.matches('\n').count() as u16 / 2; self.smoke_columns_offset = if self.smoke_columns_offset < 9 { self.smoke_columns_offset + 1 } else { 5 }; let smoke_columns = self.smoke_columns_offset * 4; self.smoke_lines_offset = if self.smoke_lines_offset < 4 { self.smoke_lines_offset + 1 } else { 0 }; let smoke = " ".repeat(smoke_columns as usize) + &self.smoke.to_string() + &"\n".repeat(self.smoke_lines_offset as usize); let text = "\n".repeat(center_lines as usize) + &smoke + &self.incense_pattern; let mut format_incense_pattern = String::new(); for i in text.split('\n') { let center_columns = (columns - i.len() as u16) / 2; format_incense_pattern .push_str(&("\n".to_owned() + &" ".repeat(center_columns as usize) + i)); } println!("{}", format_incense_pattern); }
}
fn main() { let mut st = Incense::new();
let interval = time::Duration::from_millis(500); for _ in 0..42 { terminal::enable_raw_mode().unwrap(); std::io::stdout() .execute(terminal::Clear(terminal::ClearType::All)) .unwrap(); st.format_incense_pattern(); thread::sleep(interval); terminal::disable_raw_mode().unwrap(); }
- Cargo.toml
[package] name = "shangx" version = "0.1.0" authors = ["runner"] edition = "2021"
[dependencies] crossterm= "0.27.0"
6,提个Pull吧
我运行了下,格式上还有些没对齐
use crossterm::{terminal, ExecutableCommand, QueueableCommand}; use std::{thread, time};
struct Incense { smoke: char, smoke_lines_offset: u32, smoke_columns_offset: u32, incense_pattern: &'static str, }
impl Incense { fn new() -> Incense { Incense { smoke: ',', smoke_lines_offset: 0, smoke_columns_offset: 5, incense_pattern: " ╱╱┏╮ ╱╱╱╱╱┃┃ ╱╱╱╱╱╱┃┃ ┛┛┛┛┛┛┃┃ ╱╱╱╱╱╱┃┃ ╱╱╱╱╱╱ ┃┃ ╱╱╱╱╱╱ ┃┃ ╱╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱ ┃┃ ╱╱╱╱╱╱╱╱ ", } }
}
fn main() { let mut st = Incense::new();
}
[package] name = "shangx" version = "0.1.0" authors = ["runner"] edition = "2021"
[dependencies] crossterm= "0.27.0"