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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Backup orchestrators that animate the centered backup
//! splash while `crate::backup::create_backup` runs. The
//! draw + key-wait helpers live in [`super::splash`]; this
//! module owns the throttled progress callback, the
//! per-user backup-dir resolution, and the wait-for-key
//! gating. Extracted from `tui::app` in the 1.2.7 refactor.
use anyhow::Result;
use ratatui::Terminal;
use crate::config::Config;
use crate::project::ProjectLayout;
use super::splash::{draw_backup_splash, wait_for_any_key_on_backup_splash};
/// Check whether the project is overdue for a backup and run one if so,
/// streaming progress into the splash drawn over the alternate screen.
/// Returns `Ok(())` when no backup was required OR the backup succeeded;
/// `Err(_)` if the zip failed mid-flight.
pub(super) fn maybe_auto_backup<B: ratatui::backend::Backend>(
terminal: &mut Terminal<B>,
layout: &ProjectLayout,
cfg: &Config,
) -> Result<()> {
// Auto-backup opts out only via `max_age = 0s` now — `out_dir` empty
// means "use the per-user default" (see `default_user_backup_dir`).
let bcfg = &cfg.backup;
if bcfg.max_age.as_secs() == 0 {
return Ok(());
}
// If we already backed up recently, do nothing.
let now = chrono::Utc::now();
if let Some(state) = crate::backup::BackupState::load(&layout.root) {
let age = now.signed_duration_since(state.last_at);
if age.num_seconds() >= 0
&& (age.num_seconds() as u64) < bcfg.max_age.as_secs()
{
return Ok(());
}
}
// Resolve the backup directory. Empty `out_dir` → per-user data
// location; absolute path → used as-is; relative → resolved against
// the project root (legacy override for users who explicitly want
// backups inside the project).
let out_dir = {
let raw = bcfg.out_dir.trim();
if raw.is_empty() {
crate::store::default_user_backup_dir(&layout.root)
} else {
let p = std::path::PathBuf::from(raw);
if p.is_absolute() {
p
} else {
layout.root.join(p)
}
}
};
std::fs::create_dir_all(&out_dir).ok();
let abs_project = std::fs::canonicalize(&layout.root)
.unwrap_or_else(|_| layout.root.clone());
let abs_out = std::fs::canonicalize(&out_dir).unwrap_or_else(|_| out_dir.clone());
let skip = crate::cli::backup::skip_dirs_for(&abs_project, &abs_out);
let project_display = layout.root.display().to_string();
// First frame: 0/0 so the bar shows immediately even before file
// enumeration completes.
let _ = terminal.draw(|f| draw_backup_splash(f, &project_display, 0, 0, None));
let mut last_redraw = std::time::Instant::now();
// Track the most-recent progress numbers so the post-call
// wait-for-key splash can keep the bar at 100% instead of
// resetting it to 0/0.
let mut last_progress: (usize, usize) = (0, 0);
let backup_result = {
let mut progress = |done: usize, total: usize| {
last_progress = (done, total);
// Throttle redraws to ~30Hz so a tiny project doesn't drown the
// terminal in noise on a fast disk.
if last_redraw.elapsed() < std::time::Duration::from_millis(33) {
return;
}
last_redraw = std::time::Instant::now();
let _ = terminal.draw(|f| {
draw_backup_splash(f, &project_display, done, total, None)
});
};
crate::backup::create_backup(&abs_project, &abs_out, &skip, Some(&mut progress))
};
let wait = cfg.backup.wait_for_key_after_backup;
let (done_n, total_n) = last_progress;
match backup_result {
Ok(out_path) => {
if wait {
wait_for_any_key_on_backup_splash(
terminal,
&project_display,
done_n,
total_n,
Some(&out_path),
);
}
Ok(())
}
Err(e) => {
if wait {
wait_for_any_key_on_backup_splash(
terminal,
&project_display,
done_n,
total_n,
None,
);
}
Err(anyhow::Error::from(e))
}
}
}
/// Manual backup triggered by `Ctrl+B B` (uppercase). Unlike
/// `maybe_auto_backup`, this fires unconditionally — the
/// "we already backed up recently" cooldown is skipped because
/// the user explicitly asked for a fresh archive. The
/// `backup.wait_for_key_after_backup` toggle still applies.
pub(super) fn run_manual_backup<B: ratatui::backend::Backend>(
terminal: &mut Terminal<B>,
layout: &ProjectLayout,
cfg: &Config,
) -> Result<std::path::PathBuf> {
// Resolve the backup directory the same way `maybe_auto_backup`
// does. Kept duplicated rather than refactored out so a single
// future change to either path can be reasoned about in
// isolation.
let bcfg = &cfg.backup;
let out_dir = {
let raw = bcfg.out_dir.trim();
if raw.is_empty() {
crate::store::default_user_backup_dir(&layout.root)
} else {
let p = std::path::PathBuf::from(raw);
if p.is_absolute() {
p
} else {
layout.root.join(p)
}
}
};
std::fs::create_dir_all(&out_dir).ok();
let abs_project = std::fs::canonicalize(&layout.root)
.unwrap_or_else(|_| layout.root.clone());
let abs_out = std::fs::canonicalize(&out_dir).unwrap_or_else(|_| out_dir.clone());
let skip = crate::cli::backup::skip_dirs_for(&abs_project, &abs_out);
let project_display = layout.root.display().to_string();
let _ = terminal.draw(|f| draw_backup_splash(f, &project_display, 0, 0, None));
let mut last_redraw = std::time::Instant::now();
let mut last_progress: (usize, usize) = (0, 0);
let backup_result = {
let mut progress = |done: usize, total: usize| {
last_progress = (done, total);
if last_redraw.elapsed() < std::time::Duration::from_millis(33) {
return;
}
last_redraw = std::time::Instant::now();
let _ = terminal.draw(|f| {
draw_backup_splash(f, &project_display, done, total, None)
});
};
crate::backup::create_backup(&abs_project, &abs_out, &skip, Some(&mut progress))
};
let wait = cfg.backup.wait_for_key_after_backup;
let (done_n, total_n) = last_progress;
match backup_result {
Ok(out_path) => {
if wait {
wait_for_any_key_on_backup_splash(
terminal,
&project_display,
done_n,
total_n,
Some(&out_path),
);
}
Ok(out_path)
}
Err(e) => {
if wait {
wait_for_any_key_on_backup_splash(
terminal,
&project_display,
done_n,
total_n,
None,
);
}
Err(anyhow::Error::from(e))
}
}
}