Command is behavioral design pattern that converts requests or simple operations into objects.
In 🦀 Rust, a command instance should NOT hold a permanent reference to global
context, instead the latter should be passed from top to down as a mutable
parameter of the "execute
" method:
fn execute(&mut self, app: &mut cursive::Cursive) -> bool;
How to launch:
cargo run --bin command
Key points:
- Each button runs a separate command.
- Because a command is represented as an object, it can be pushed into a
history
array in order to be undone later. - TUI is created with
cursive
crate.
This example is inspired by Command in Java (Example).