use crate::app::{actions::Action, state::AppState};
use crate::app::commit_template;
pub fn handle_input(mut state: AppState, action: &Action) -> Option<AppState> {
match action {
Action::StartCommit => {
state.commit_mode = true;
state.amend_mode = false;
state.amend_editing_field = Some(crate::app::state::AmendField::Message);
if let Some(ref template) = state.commit_template {
state.commit_input = commit_template::get_template_preview(template, &state);
} else {
state.commit_input.clear();
}
state.command_palette = false;
}
Action::StartAmend => {
state.commit_mode = true;
state.amend_mode = true;
state.amend_editing_field = Some(crate::app::state::AmendField::Message);
if let Some(ref last_info) = state.last_commit_info {
state.commit_input = last_info.message.clone();
state.amend_author_name = last_info.author_name.clone();
state.amend_author_email = last_info.author_email.clone();
}
state.command_palette = false;
}
Action::LoadLastCommitInfo => {
}
Action::SetLastCommitInfo(info) => {
state.last_commit_info = info.clone();
if let Some(ref last_info) = info {
state.commit_input = last_info.message.clone();
state.amend_author_name = last_info.author_name.clone();
state.amend_author_email = last_info.author_email.clone();
}
}
Action::SetAmendAuthorName(name) => {
state.amend_author_name = name.clone();
}
Action::SetAmendAuthorEmail(email) => {
state.amend_author_email = email.clone();
}
Action::SetAmendEditingField(field) => {
state.amend_editing_field = *field;
}
Action::CancelCommit => {
state.commit_mode = false;
state.amend_mode = false;
state.commit_input.clear();
state.amend_author_name.clear();
state.amend_author_email.clear();
state.last_commit_info = None;
state.amend_editing_field = None;
}
Action::SetCommitInput(msg) => {
state.commit_input = msg.clone();
}
Action::CommitInputChar(c) => {
state.commit_input.push(*c);
}
Action::CommitInputBackspace => {
state.commit_input.pop();
}
_ => return None,
}
Some(state)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app::{Action, AppState};
#[test]
fn test_handle_commit_input_char() {
let mut state = AppState::new();
state.commit_input = String::new();
state = handle_input(state, &Action::CommitInputChar('a')).unwrap();
state = handle_input(state, &Action::CommitInputChar('b')).unwrap();
assert_eq!(state.commit_input, "ab");
}
#[test]
fn test_handle_commit_backspace() {
let mut state = AppState::new();
state.commit_input = "abc".to_string();
state = handle_input(state, &Action::CommitInputBackspace).unwrap();
assert_eq!(state.commit_input, "ab");
}
}