use crate::app::AppState;
use crate::commands::{Command, StageFilesCommand, UnstageFilesCommand, CommitCommand};
use crate::app::commit_template;
pub fn create_stage_command(state: &AppState, stage: bool) -> Option<Box<dyn Command>> {
if !state.selected_files.is_empty() {
let paths: Vec<String> = state.selected_files.iter().cloned().collect();
return if stage {
Some(Box::new(StageFilesCommand { paths }))
} else {
Some(Box::new(UnstageFilesCommand { paths }))
};
}
if !state.status_selected_path.is_empty() {
let paths = vec![state.status_selected_path.clone()];
return if stage {
Some(Box::new(StageFilesCommand { paths }))
} else {
Some(Box::new(UnstageFilesCommand { paths }))
};
}
None
}
pub fn create_commit_command(state: &AppState, amend: bool) -> Option<Box<dyn Command>> {
let mut message = state.commit_input.clone();
if let Some(ref template) = state.commit_template {
message = commit_template::substitute_template(template, state, &message);
}
if message.trim().is_empty() && !amend {
return None;
}
let (author, email) = if amend {
(
if state.amend_author_name.is_empty() { None } else { Some(state.amend_author_name.clone()) },
if state.amend_author_email.is_empty() { None } else { Some(state.amend_author_email.clone()) }
)
} else {
(None, None)
};
Some(Box::new(CommitCommand {
message,
amend,
author_name: author,
author_email: email,
}))
}