File tree Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Original file line number Diff line number Diff line change 11# Command
22
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+ ```
414
515## Text Editor: Commands and Undo
616
17+ How to launch:
18+
719``` bash
8- cargo run
20+ cargo run --bin command
921```
1022
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+
1130![ 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;
66pub use cut:: CutCommand ;
77pub use paste:: PasteCommand ;
88
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.
913pub trait Command {
1014 fn execute ( & mut self , app : & mut cursive:: Cursive ) -> bool ;
1115 fn undo ( & mut self , app : & mut cursive:: Cursive ) ;
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ use cursive::{
88
99use command:: { Command , CopyCommand , CutCommand , PasteCommand } ;
1010
11+ /// An application context to be passed into visual component callbacks.
12+ /// It contains a clipboard and a history of commands to be undone.
1113#[ derive( Default ) ]
1214struct AppContext {
1315 clipboard : String ,
@@ -31,6 +33,7 @@ fn main() {
3133 app. run ( ) ;
3234}
3335
36+ /// Executes a command and then pushes it to a history array.
3437fn execute ( app : & mut Cursive , mut command : impl Command + ' static ) {
3538 if command. execute ( app) {
3639 app. with_user_data ( |context : & mut AppContext | {
@@ -39,6 +42,7 @@ fn execute(app: &mut Cursive, mut command: impl Command + 'static) {
3942 }
4043}
4144
45+ /// Pops the last command and executes an undo action.
4246fn undo ( app : & mut Cursive ) {
4347 let mut context = app. take_user_data :: < AppContext > ( ) . unwrap ( ) ;
4448 if let Some ( mut command) = context. history . pop ( ) {
You can’t perform that action at this time.
0 commit comments