1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Status update actions.
//!
//! Handles setting status entries, summaries, errors, and workflow context.
use crate::app::{actions::Action, state::AppState};
use crate::app::error_guidance::ErrorGuidance;
use crate::app::workflow::WorkflowContext;
/// Handle status update actions.
pub fn handle_status_updates(state: &mut AppState, action: &Action) -> bool {
match action {
Action::SetStatus(raw) => {
state.last_status = raw.clone();
true
}
Action::SetStatusLines(lines) => {
state.status_lines = lines.clone();
true
}
Action::SetStatusEntries(entries) => {
// Preserve the selected path before updating entries
// (files can move positions when status changes, e.g., untracked -> staged)
let preserved_path = state.status_selected_path.clone();
// Try to find the preserved path in the new entries list
// This ensures selection persists even if file moves position after staging
if let Some(found_idx) = entries.iter()
.position(|e| e.path == preserved_path) {
state.status_selected = found_idx;
} else {
// Path not found (file might have been removed) - use index with bounds check
let max_idx = entries.len().saturating_sub(1);
state.status_selected = state.status_selected.min(max_idx);
}
state.status_entries = entries.clone();
// Update workflow context when status changes
state.workflow_context = Some(WorkflowContext::detect(&*state));
true
}
Action::SetStatusSummary(summary) => {
state.status_summary = summary.clone();
true
}
Action::SetStatusError(err) => {
state.last_status_error = err.clone();
// Generate error guidance if error exists
if let Some(ref error_msg) = err {
state.error_guidance = Some(ErrorGuidance::from_error(error_msg));
} else {
state.error_guidance = None;
}
true
}
Action::SetErrorGuidance(guidance) => {
state.error_guidance = guidance.clone();
true
}
Action::UpdateWorkflowContext => {
state.workflow_context = Some(WorkflowContext::detect(&*state));
true
}
_ => false,
}
}