git_tailor/views/
squash_select.rs1use crate::app::{AppAction, AppMode, AppState, KeyCommand};
19
20pub fn handle_key(action: KeyCommand, app: &mut AppState) -> AppAction {
27 let (source_index, is_fixup) = match app.mode {
28 AppMode::SquashSelect {
29 source_index,
30 is_fixup,
31 } => (source_index, is_fixup),
32 _ => return AppAction::Handled,
33 };
34
35 match action {
36 KeyCommand::MoveUp => {
37 if app.reverse {
38 app.move_down();
39 } else {
40 app.move_up();
41 }
42 app.selection_index = app.selection_index.min(source_index);
43 AppAction::Handled
44 }
45 KeyCommand::MoveDown => {
46 if app.reverse {
47 app.move_up();
48 } else {
49 app.move_down();
50 }
51 app.selection_index = app.selection_index.min(source_index);
52 AppAction::Handled
53 }
54 KeyCommand::PageUp => {
55 let h = app.commit_list_visible_height;
56 if app.reverse {
57 app.page_down(h);
58 } else {
59 app.page_up(h);
60 }
61 app.selection_index = app.selection_index.min(source_index);
62 AppAction::Handled
63 }
64 KeyCommand::PageDown => {
65 let h = app.commit_list_visible_height;
66 if app.reverse {
67 app.page_up(h);
68 } else {
69 app.page_down(h);
70 }
71 app.selection_index = app.selection_index.min(source_index);
72 AppAction::Handled
73 }
74 KeyCommand::Confirm => {
75 let target_index = app.selection_index;
76
77 if target_index == source_index {
79 app.set_error_message("Cannot squash a commit into itself");
80 return AppAction::Handled;
81 }
82
83 let target = &app.commits[target_index];
85 if target.oid == "staged" || target.oid == "unstaged" {
86 app.set_error_message("Cannot squash into staged/unstaged changes");
87 return AppAction::Handled;
88 }
89
90 let source = &app.commits[source_index];
91 let result = AppAction::PrepareSquash {
92 source_oid: source.oid.clone(),
93 target_oid: target.oid.clone(),
94 source_message: source.message.clone(),
95 target_message: target.message.clone(),
96 is_fixup,
97 };
98
99 app.mode = AppMode::CommitList;
100 result
101 }
102 KeyCommand::ShowHelp => {
103 app.toggle_help();
104 AppAction::Handled
105 }
106 KeyCommand::Quit => {
107 app.cancel_squash_select();
108 AppAction::Handled
109 }
110 _ => AppAction::Handled,
111 }
112}