@@ -8,23 +8,29 @@ use crate::{functions, terminal, state::State};
88#[ enum_dispatch]
99#[ derive( Debug , Deserialize ) ]
1010#[ serde( rename_all = "snake_case" ) ]
11- pub enum Command {
11+ pub enum Command < ' a > {
1212 Clear ( Clear ) ,
13- Erase ( Erase ) ,
14- Execute ( Execute ) ,
13+ #[ serde( borrow) ]
14+ Erase ( Erase < ' a > ) ,
15+ #[ serde( borrow) ]
16+ Execute ( Execute < ' a > ) ,
1517 NewLine ( NewLine ) ,
1618 Pause ( Pause ) ,
17- Prompt ( Prompt ) ,
18- Wait ( Wait ) ,
19- Write ( Write ) ,
19+ #[ serde( borrow) ]
20+ Prompt ( Prompt < ' a > ) ,
2021 Turbo ( Turbo ) ,
22+ Wait ( Wait ) ,
23+ #[ serde( borrow) ]
24+ Write ( Write < ' a > ) ,
2125}
2226
2327#[ enum_dispatch( Command ) ]
2428pub trait Runnable {
2529 fn run ( & self , state : & mut State ) -> Result < ( ) > ;
2630}
2731
32+ /// `!clear`
33+ /// Clear screen command.
2834#[ derive( Debug , Deserialize ) ]
2935pub struct Clear { }
3036
@@ -37,14 +43,16 @@ impl Runnable for Clear {
3743 }
3844}
3945
46+ /// `!erase`
47+ /// Erase characters to the left.
4048#[ derive( Debug , Deserialize ) ]
41- pub struct Erase {
49+ pub struct Erase < ' a > {
4250 pub msec : u32 ,
43- pub by_chars : Option < String > ,
51+ pub by_chars : Option < & ' a str > ,
4452 pub amount : Option < u32 > ,
4553}
4654
47- impl Runnable for Erase {
55+ impl Runnable for Erase < ' _ > {
4856 fn run ( & self , state : & mut State ) -> Result < ( ) > {
4957 let deletions = match ( self . by_chars . as_ref ( ) , self . amount ) {
5058 ( Some ( by_chars) , None ) => by_chars. len ( ) ,
@@ -61,12 +69,14 @@ impl Runnable for Erase {
6169 }
6270}
6371
72+ /// `!execute`
73+ /// Execute shell commands or other applications and show their output.
6474#[ derive( Debug , Deserialize ) ]
65- pub struct Execute {
66- pub line : String ,
75+ pub struct Execute < ' a > {
76+ pub line : & ' a str ,
6777}
6878
69- impl Runnable for Execute {
79+ impl < ' a > Runnable for Execute < ' a > {
7080 fn run ( & self , state : & mut State ) -> Result < ( ) > {
7181 println ! ( "" ) ;
7282 let words = shellwords:: split ( & self . line ) . unwrap ( ) ;
@@ -81,6 +91,8 @@ impl Runnable for Execute {
8191 }
8292}
8393
94+ /// `!new_line`
95+ /// Simulate user's `ENTER`.
8496#[ derive( Debug , Deserialize ) ]
8597pub struct NewLine { }
8698
@@ -93,6 +105,8 @@ impl Runnable for NewLine {
93105 }
94106}
95107
108+ /// `!pasue`
109+ /// Pause before next command and wait for user input (any key...)
96110#[ derive( Debug , Deserialize ) ]
97111pub struct Pause { }
98112
@@ -104,13 +118,15 @@ impl Runnable for Pause {
104118 }
105119}
106120
121+ /// `!prompt`
122+ /// Prompt specify a constant text that is shown after every `execute` and cis not affected by `erase`.
107123#[ derive( Debug , Deserialize ) ]
108- pub struct Prompt {
109- pub text : String ,
110- pub color : Option < String > ,
124+ pub struct Prompt < ' a > {
125+ pub text : & ' a str ,
126+ pub color : Option < & ' a str > ,
111127}
112128
113- impl Runnable for Prompt {
129+ impl Runnable for Prompt < ' _ > {
114130 fn run ( & self , state : & mut State ) -> Result < ( ) > {
115131 let ps1 = terminal:: colorful ( & self . text , self . color . as_deref ( ) ) ;
116132 state. prompt = Some ( ps1) ;
@@ -120,6 +136,22 @@ impl Runnable for Prompt {
120136 }
121137}
122138
139+ /// `!turbo`
140+ /// Speed everything, useful when iterating over the screenplay.
141+ #[ derive( Debug , Deserialize ) ]
142+ pub struct Turbo {
143+ pub by : u32 ,
144+ }
145+
146+ impl Runnable for Turbo {
147+ fn run ( & self , state : & mut State ) -> Result < ( ) > {
148+ state. speed_factor = self . by ;
149+ Ok ( ( ) )
150+ }
151+ }
152+
153+ /// `!wait`
154+ /// Pauses execution for the specified time, then contrinues.
123155#[ derive( Debug , Deserialize ) ]
124156pub struct Wait {
125157 pub msec : u32 ,
@@ -132,14 +164,16 @@ impl Runnable for Wait {
132164 }
133165}
134166
167+ /// `!write`
168+ /// Write text to the terminal.
135169#[ derive( Debug , Deserialize ) ]
136- pub struct Write {
170+ pub struct Write < ' a > {
137171 pub msec : u32 ,
138- pub color : Option < String > ,
139- pub text : String ,
172+ pub color : Option < & ' a str > ,
173+ pub text : & ' a str ,
140174}
141175
142- impl Runnable for Write {
176+ impl Runnable for Write < ' _ > {
143177 fn run ( & self , state : & mut State ) -> Result < ( ) > {
144178 for c in self . text . chars ( ) {
145179 functions:: delay ( self . msec , state. speed_factor ) ;
@@ -151,14 +185,3 @@ impl Runnable for Write {
151185 }
152186}
153187
154- #[ derive( Debug , Deserialize ) ]
155- pub struct Turbo {
156- pub by : u32 ,
157- }
158-
159- impl Runnable for Turbo {
160- fn run ( & self , state : & mut State ) -> Result < ( ) > {
161- state. speed_factor = self . by ;
162- Ok ( ( ) )
163- }
164- }
0 commit comments