Skip to content

Commit f076e3a

Browse files
committed
added turbo command
1 parent 5c51144 commit f076e3a

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "swordfish-rs"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
edition = "2021"
55
license-file = "LICENSE"
66
description = "Cli tool for typing effect in Termainl for screencasts"

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Example `screenplay.yaml` file:
1818

1919
```yaml
2020
- !clear
21+
# - !turbo {by: 3}
2122
- !write {msec: 0, color: green, text: "$ "}
2223
- !write {msec: 20, text: "i am going to list this dir"}
2324
- !wait {msec: 1000}
@@ -58,16 +59,10 @@ swordfish path/to/file.yaml
5859

5960
The following commands are available, written with `!` before the command name, for example `!clear`.
6061

61-
#### `write`
62+
#### `clear`
6263

63-
Write text to the terminal.
64+
Clear screen command.
6465

65-
| Argument | Type | Description |
66-
| - | - | - |
67-
|`text`| String | the text to type in the terminal, each character will be entered one by one with some delay |
68-
|`msec`| Integer | delay between typed chars in millisecs |
69-
|`color` (optional)| String | text's color: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white` or a brighter variant, for example `bright_red` |
70-
7166
#### `erase`
7267

7368
Erase characters to the left.
@@ -90,15 +85,9 @@ Execute shell commands or other applications and show their output.
9085

9186
The output is presented, while the executed command itself will not show.
9287

93-
#### `wait`
94-
95-
| Argument | Type | Description |
96-
| - | - | - |
97-
|`msec`| Integer | delay before next command in millisecs |
98-
99-
#### `clear`
88+
#### `new_line`
10089

101-
Clear screen command.
90+
Simulate user's `ENTER`.
10291

10392
#### `pause`
10493

@@ -113,6 +102,26 @@ Prompt specify a constant text that is shown after every `execute` and cis not a
113102
|`text`| String | the prompt text |
114103
|`color` (optional)| String | text's color: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white` or a brighter variant, for example `bright_red` |
115104

116-
#### `new_line`
105+
#### `turbo`
117106

118-
Simulate user's `ENTER`.
107+
Speed everything, useful when iterating over the screenplay.
108+
109+
| Argument | Type | Description |
110+
| - | - | - |
111+
|`by`| Integer | Speed everything by this factor |
112+
113+
#### `wait`
114+
115+
| Argument | Type | Description |
116+
| - | - | - |
117+
|`msec`| Integer | delay before next command in millisecs |
118+
119+
#### `write`
120+
121+
Write text to the terminal.
122+
123+
| Argument | Type | Description |
124+
| - | - | - |
125+
|`text`| String | the text to type in the terminal, each character will be entered one by one with some delay |
126+
|`msec`| Integer | delay between typed chars in millisecs |
127+
|`color` (optional)| String | text's color: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white` or a brighter variant, for example `bright_red` |

examples/screenplay.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- !clear
2+
# - !turbo {by: 10}
23
- !prompt {text: "$", color: green}
34
- !write {msec: 20, text: "i am going to list this dir"}
45
- !new_line

src/commands.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ pub enum Command<'a> {
1111
Prompt {text: &'a str, color: Option<&'a str>},
1212
Wait {msec: u32},
1313
Write {msec: u32, color: Option<&'a str>, text: &'a str},
14+
Turbo {by: u32},
1415
}

src/lib.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ pub fn from_yaml(data: &str) -> Result<Vec<Command>> {
1212
Ok(yaml)
1313
}
1414

15-
const DELAY_AFTER_EXECUTE: u64 = 250;
15+
const DELAY_AFTER_EXECUTE: u32 = 250;
1616

1717
pub fn execute(commands: Vec<Command>) -> Result<()> {
1818
let mut prompt = None;
1919
let mut cursor = 0;
20+
let mut speed_factor = 1;
2021

2122
for cmd in commands {
2223
match cmd {
2324
Command::Write {msec, text, color} => {
2425
for c in text.chars() {
25-
thread::sleep(time::Duration::from_millis(msec.into()));
26+
delay(msec, speed_factor);
2627
print!("{}", terminal::colorful(&c.to_string(), color));
2728
stdout().flush()?;
2829
}
@@ -39,7 +40,7 @@ pub fn execute(commands: Vec<Command>) -> Result<()> {
3940
// Remove the deletions up till the cursor
4041
let deletions = cmp::min(deletions, cursor);
4142
cursor -= deletions;
42-
erase(deletions, msec)?;
43+
erase(deletions, msec, speed_factor)?;
4344
},
4445
Command::Execute {line} => {
4546
println!("");
@@ -48,12 +49,12 @@ pub fn execute(commands: Vec<Command>) -> Result<()> {
4849
if let Some((cmd, args)) = words.split_first() {
4950
process::Command::new(cmd).args(args).spawn()?;
5051
}
51-
thread::sleep(time::Duration::from_millis(DELAY_AFTER_EXECUTE));
52+
delay(DELAY_AFTER_EXECUTE, speed_factor);
5253
show_prompt(&prompt)?;
5354
cursor = 0;
5455
},
5556
Command::Wait {msec} => {
56-
thread::sleep(time::Duration::from_millis(msec.into()));
57+
delay(msec, speed_factor);
5758
},
5859
Command::Pause => {
5960
let mut answer = String::new();
@@ -74,7 +75,10 @@ pub fn execute(commands: Vec<Command>) -> Result<()> {
7475
print!("\n");
7576
show_prompt(&prompt)?;
7677
cursor = 0;
77-
}
78+
},
79+
Command::Turbo {by} => {
80+
speed_factor = by;
81+
},
7882
}
7983
}
8084

@@ -90,11 +94,16 @@ fn show_prompt(prompt: &Option<String>) -> Result<()> {
9094
Ok(())
9195
}
9296

93-
fn erase(amount: usize, msec: u32) -> Result<()> {
97+
fn erase(amount: usize, msec: u32, speed_factor: u32) -> Result<()> {
9498
for _ in 0..amount {
95-
thread::sleep(time::Duration::from_millis(msec.into()));
99+
delay(msec, speed_factor);
96100
print!("{} {}", BACKSPACE, BACKSPACE);
97101
stdout().flush()?;
98102
}
99103
Ok(())
100-
}
104+
}
105+
106+
fn delay(msec: u32, speed_factor: u32) {
107+
let t = msec / speed_factor;
108+
thread::sleep(time::Duration::from_millis(t.into()));
109+
}

0 commit comments

Comments
 (0)