File tree 3 files changed +33
-2
lines changed
3 files changed +33
-2
lines changed Original file line number Diff line number Diff line change 1
1
# Command
2
2
3
- Command is behavioral design pattern that converts requests or simple operations into objects.
3
+ ** Command** is behavioral design pattern that converts requests or
4
+ simple operations into objects.
5
+
6
+ A specific thing about Command Pattern implementation in 🦀 Rust is that
7
+ a command instance should NOT hold a permanent reference to global context,
8
+ instead the latter should be passed from top to down as a mutable parameter
9
+ of the "` execute ` " method:
10
+
11
+ ``` rust
12
+ fn execute (& mut self , app : & mut cursive :: Cursive ) -> bool ;
13
+ ```
4
14
5
15
## Text Editor: Commands and Undo
6
16
17
+ How to launch:
18
+
7
19
``` bash
8
- cargo run
20
+ cargo run --bin command
9
21
```
10
22
23
+ Key points:
24
+
25
+ - Each button runs a separate command.
26
+ - Because a command is represented as an object, it can be pushed into a
27
+ ` history ` array in order to be undone later.
28
+ - TUI is created with ` cursive ` crate.
29
+
11
30
![ Text Editor screenshot] ( res/editor.png )
31
+
32
+ ## Notes
33
+
34
+ This example is inspired by [ Command in Java (Example)] ( https://refactoring.guru/design-patterns/command/java/example ) .
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ pub use copy::CopyCommand;
6
6
pub use cut:: CutCommand ;
7
7
pub use paste:: PasteCommand ;
8
8
9
+ /// Declares a method for executing (and undoing) a command.
10
+ ///
11
+ /// Each command receives an application context to access
12
+ /// visual components (e.g. edit view) and a clipboard.
9
13
pub trait Command {
10
14
fn execute ( & mut self , app : & mut cursive:: Cursive ) -> bool ;
11
15
fn undo ( & mut self , app : & mut cursive:: Cursive ) ;
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ use cursive::{
8
8
9
9
use command:: { Command , CopyCommand , CutCommand , PasteCommand } ;
10
10
11
+ /// An application context to be passed into visual component callbacks.
12
+ /// It contains a clipboard and a history of commands to be undone.
11
13
#[ derive( Default ) ]
12
14
struct AppContext {
13
15
clipboard : String ,
@@ -31,6 +33,7 @@ fn main() {
31
33
app. run ( ) ;
32
34
}
33
35
36
+ /// Executes a command and then pushes it to a history array.
34
37
fn execute ( app : & mut Cursive , mut command : impl Command + ' static ) {
35
38
if command. execute ( app) {
36
39
app. with_user_data ( |context : & mut AppContext | {
@@ -39,6 +42,7 @@ fn execute(app: &mut Cursive, mut command: impl Command + 'static) {
39
42
}
40
43
}
41
44
45
+ /// Pops the last command and executes an undo action.
42
46
fn undo ( app : & mut Cursive ) {
43
47
let mut context = app. take_user_data :: < AppContext > ( ) . unwrap ( ) ;
44
48
if let Some ( mut command) = context. history . pop ( ) {
You can’t perform that action at this time.
0 commit comments