Last active
March 23, 2023 05:07
-
-
Save aerphanas/a62c1c2424a82cbbbefb99dad51e5aaa to your computer and use it in GitHub Desktop.
animate text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io::{self, Write}; | |
| use std::time::{Instant, Duration}; | |
| use std::thread; | |
| fn main() { | |
| let time: Instant = Instant::now(); | |
| let text: &str = "Marhaban ya Ramadhan"; | |
| let mut is_up: bool = true; | |
| let mut current_index: usize = 0; | |
| (0..).take_while(|_| time.elapsed().as_secs() < 4) | |
| .for_each(|_| { | |
| let output: String = format!( "{}{}", | |
| " ".repeat(current_index), | |
| text.chars() | |
| .nth(current_index) | |
| .unwrap() | |
| ); | |
| print!("{}\r", output); | |
| io::stdout() | |
| .flush() | |
| .unwrap(); | |
| if is_up { | |
| current_index += 1 | |
| } else { | |
| current_index -= 1 | |
| }; | |
| if current_index == 0 || current_index == text.len() - 1 { | |
| is_up = !is_up; | |
| } | |
| thread::sleep(Duration::from_millis(105)); | |
| }); | |
| println!("{}", text); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment