Skip to content

Instantly share code, notes, and snippets.

@aerphanas
Last active March 23, 2023 05:07
Show Gist options
  • Select an option

  • Save aerphanas/a62c1c2424a82cbbbefb99dad51e5aaa to your computer and use it in GitHub Desktop.

Select an option

Save aerphanas/a62c1c2424a82cbbbefb99dad51e5aaa to your computer and use it in GitHub Desktop.
animate text
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