1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
mod commands;
mod terminal;
use anyhow::Result;
use std::{cmp, process, thread, time};
use std::io::{self, stdout, Write};
use commands::Command;
use terminal::BACKSPACE;
pub fn from_yaml(data: &str) -> Result<Vec<Command>> {
let yaml = serde_yaml::from_str(data)?;
Ok(yaml)
}
const DELAY_AFTER_EXECUTE: u32 = 250;
pub fn execute(commands: Vec<Command>) -> Result<()> {
let mut prompt = None;
let mut cursor = 0;
let mut speed_factor = 1;
for cmd in commands {
match cmd {
Command::Write {msec, text, color} => {
for c in text.chars() {
delay(msec, speed_factor);
print!("{}", terminal::colorful(&c.to_string(), color));
stdout().flush()?;
}
cursor += text.len();
},
Command::Erase {msec, by_chars, amount} => {
let deletions = match (by_chars, amount) {
(Some(by_chars), None) => by_chars.len(),
(None, Some(amount)) => amount as usize,
(Some(by_chars), Some(amount)) => amount as usize + by_chars.len(),
(None, None) => 0,
};
let deletions = cmp::min(deletions, cursor);
cursor -= deletions;
erase(deletions, msec, speed_factor)?;
},
Command::Execute {line} => {
println!("");
let words = shellwords::split(line).unwrap();
if let Some((cmd, args)) = words.split_first() {
process::Command::new(cmd).args(args).spawn()?;
}
delay(DELAY_AFTER_EXECUTE, speed_factor);
show_prompt(&prompt)?;
cursor = 0;
},
Command::Wait {msec} => {
delay(msec, speed_factor);
},
Command::Pause => {
let mut answer = String::new();
io::stdin().read_line(&mut answer)?;
},
Command::Clear => {
print!("\x1B[2J\x1B[1;1H");
show_prompt(&prompt)?;
cursor = 0;
}
Command::Prompt {text, color} => {
let ps1 = terminal::colorful(text, color);
prompt = Some(ps1);
show_prompt(&prompt)?;
cursor = 0;
},
Command::NewLine => {
print!("\n");
show_prompt(&prompt)?;
cursor = 0;
},
Command::Turbo {by} => {
speed_factor = by;
},
}
}
Ok(())
}
fn show_prompt(prompt: &Option<String>) -> Result<()> {
if let Some(ps1) = prompt {
print!("{ps1} ");
stdout().flush()?;
}
Ok(())
}
fn erase(amount: usize, msec: u32, speed_factor: u32) -> Result<()> {
for _ in 0..amount {
delay(msec, speed_factor);
print!("{} {}", BACKSPACE, BACKSPACE);
stdout().flush()?;
}
Ok(())
}
fn delay(msec: u32, speed_factor: u32) {
let t = msec / speed_factor;
thread::sleep(time::Duration::from_millis(t.into()));
}