feat: add command pattern
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
42
crates/hyperlog-tui/src/commands/batch.rs
Normal file
42
crates/hyperlog-tui/src/commands/batch.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use super::IntoCommand;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BatchCommand {
|
||||
commands: Vec<super::Command>,
|
||||
}
|
||||
|
||||
impl BatchCommand {
|
||||
pub fn with(&mut self, cmd: impl IntoCommand) -> &mut Self {
|
||||
self.commands.push(cmd.into_command());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoCommand for Vec<super::Command> {
|
||||
fn into_command(self) -> super::Command {
|
||||
BatchCommand::from(self).into_command()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<super::Command>> for BatchCommand {
|
||||
fn from(value: Vec<super::Command>) -> Self {
|
||||
BatchCommand { commands: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoCommand for BatchCommand {
|
||||
fn into_command(self) -> super::Command {
|
||||
super::Command::new(|dispatch| {
|
||||
for command in self.commands {
|
||||
let msg = command.execute(dispatch.clone());
|
||||
if let Some(msg) = msg {
|
||||
tracing::info!("executing batch command for msg: {:?}", msg);
|
||||
dispatch.send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user