121812 / shangx

给服务器上柱香吧,希望它永不宕机。
MIT License
5 stars 1 forks source link

改成rust版本防止香气泄露 #1

Open novohool opened 11 months ago

novohool commented 11 months ago

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"

121812 commented 11 months ago

6,提个Pull吧

121812 commented 11 months ago

image 我运行了下,格式上还有些没对齐