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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::cell::Cell;
use std::time::{Duration, Instant};
use crate::config::{self, Config};
use crate::git::{self, Commit};
/// Possible screens the application can be in.
#[derive(Clone, Debug, PartialEq)]
pub enum Screen {
/// Initial state while git data is being fetched.
Loading,
/// Commit list (main screen).
List,
/// Commit detail overlay.
Detail,
/// Error screen with a message.
Error(String),
/// Modal alert that requires user acknowledgment.
Alert(AlertKind),
}
/// Discriminated reason for a modal alert.
#[derive(Clone, Debug, PartialEq)]
pub enum AlertKind {
/// The commit being viewed in Detail no longer exists in the repository.
CommitDeleted { oid: String },
}
/// A transient non-blocking notification displayed in the footer.
#[derive(Clone, Debug)]
pub struct Notification {
pub message: String,
pub expires_at: Instant,
}
/// Central application state.
pub struct App {
pub screen: Screen,
pub commits: Vec<Commit>,
pub selected_index: usize,
pub selected_commit: Option<Commit>,
pub colors: config::Colors,
pub should_quit: bool,
pub detail_scroll: Cell<usize>,
/// Height of the scrollable content area (updated by render for page-scroll calculations).
pub detail_content_height: Cell<usize>,
/// Whether the keybindings help overlay is shown.
pub show_help: bool,
// ── Live-sync state ──────────────────────────────────────────
/// Transient footer notification (HEAD-move announcement).
pub notification: Option<Notification>,
/// Once true, the polling loop stops permanently (fatal repo error).
pub polling_stopped: bool,
/// The HEAD commit OID from the last successful poll (used for change detection).
pub current_head_oid: Option<String>,
/// Polling interval in milliseconds.
pub poll_interval_ms: u64,
/// How long a notification stays visible before auto-dismiss (ms).
pub notification_timeout_ms: u64,
/// Timestamp of the last poll (for throttling).
pub last_poll_time: Instant,
}
impl App {
/// Create a new app in the Loading state.
pub fn new(config: Config) -> Self {
App {
screen: Screen::Loading,
commits: Vec::new(),
selected_index: 0,
selected_commit: None,
colors: config.colors,
should_quit: false,
detail_scroll: Cell::new(0),
detail_content_height: Cell::new(0),
show_help: false,
notification: None,
polling_stopped: false,
current_head_oid: None,
poll_interval_ms: config.poll_interval_ms,
notification_timeout_ms: config.notification_timeout_ms,
// Initialise to the past so the first poll always runs.
last_poll_time: Instant::now()
- Duration::from_millis(config.poll_interval_ms + 1),
}
}
/// Load commits from the git repository.
/// Transitions to `List` on success, `Error` on failure.
pub fn load_commits(&mut self) {
match git::load_commits() {
Ok(commits) => {
self.commits = commits;
if self.commits.is_empty() {
self.screen =
Screen::Error("このリポジトリにはまだコミットがありません。".into());
} else {
self.screen = Screen::List;
}
}
Err(e) => {
self.screen = Screen::Error(e.to_string());
}
}
}
/// Run one polling cycle.
///
/// 1. Throttle to `poll_interval_ms`.
/// 2. Open repository and check HEAD OID.
/// 3. Reload commits.
/// 4. If HEAD changed → notification + close detail + reload list.
/// 5. If HEAD unchanged + in Detail → update timestamp / detect deletion.
/// 6. Replace commit list while OID-tracking the selection.
pub fn poll(&mut self) {
if self.polling_stopped {
self.tick_notification();
return;
}
let now = Instant::now();
// Throttle: don't poll more often than the configured interval.
if now - self.last_poll_time < Duration::from_millis(self.poll_interval_ms) {
self.tick_notification();
return;
}
self.last_poll_time = now;
self.tick_notification();
// 1. Open repository
let repo = match git::open_repo() {
Ok(r) => r,
Err(e) => {
self.screen = Screen::Error(e.to_string());
self.polling_stopped = true;
return;
}
};
// 2. Get current HEAD OID
let new_head_oid = match git::current_head_oid(&repo) {
Ok(oid) => oid,
Err(e) => {
self.screen = Screen::Error(e.to_string());
self.polling_stopped = true;
return;
}
};
// 3. Load commits
let new_commits = match git::load_commits_from(&repo) {
Ok(c) => c,
Err(e) => {
self.screen = Screen::Error(e.to_string());
self.polling_stopped = true;
return;
}
};
// 4. First poll after startup: just record HEAD, no change detection.
if self.current_head_oid.is_none() {
self.current_head_oid = Some(new_head_oid.clone());
self.replace_commits(new_commits);
return;
}
// 5. HEAD change detection
let old_head = self.current_head_oid.clone();
let head_changed = old_head.as_ref() != Some(&new_head_oid);
if head_changed {
// Build notification BEFORE moving new_head_oid.
let old_short = old_head.as_ref().map(|o| &o[..7.min(o.len())]).unwrap_or("?");
let new_short = new_head_oid[..7.min(new_head_oid.len())].to_string();
self.current_head_oid = Some(new_head_oid);
self.set_notification(format!("HEAD moved: {} → {}", old_short, new_short));
// Close Detail if it was open
if matches!(self.screen, Screen::Detail) {
self.screen = Screen::List;
self.selected_commit = None;
self.detail_scroll.set(0);
}
self.replace_commits(new_commits);
} else {
// HEAD unchanged — still update timestamps
if let (Screen::Detail, Some(sel)) = (&self.screen, self.selected_commit.clone()) {
let oid = sel.oid.clone();
if let Some(nc) = new_commits.iter().find(|c| c.oid == oid) {
// Update timestamp on the detail commit
if let Some(ref mut sc) = self.selected_commit {
sc.date = nc.date.clone();
sc.author = nc.author.clone();
}
} else {
// Selected commit no longer reachable — check if object exists
let gone = git::object_exists(&repo, &oid)
.map(|exists| !exists)
.unwrap_or(true);
if gone {
self.screen =
Screen::Alert(AlertKind::CommitDeleted { oid: oid.clone() });
self.commits = new_commits;
return;
}
}
}
self.replace_commits(new_commits);
}
}
// ── Notification helpers ────────────────────────────────────
/// Create or overwrite the footer notification with a timeout.
pub fn set_notification(&mut self, message: String) {
let timeout_ms = self.notification_timeout_ms.max(100);
self.notification = Some(Notification {
message,
expires_at: Instant::now() + Duration::from_millis(timeout_ms),
});
}
/// Clear expired notification based on real time.
fn tick_notification(&mut self) {
if let Some(ref notif) = self.notification {
if Instant::now() >= notif.expires_at {
self.notification = None;
}
}
}
// ── Commit list management ───────────────────────────────────
/// Replace the commit list while OID-tracking the current selection.
fn replace_commits(&mut self, new_commits: Vec<Commit>) {
let target_oid = self
.commits
.get(self.selected_index)
.map(|c| c.oid.clone());
self.commits = new_commits;
self.selected_index = target_oid
.and_then(|oid| self.commits.iter().position(|c| c.oid == oid))
.unwrap_or(0);
}
// ── Navigation ───────────────────────────────────────────────
/// Move selection up (towards older commits).
pub fn navigate_up(&mut self) {
if self.screen == Screen::List && !self.commits.is_empty() {
if self.selected_index > 0 {
self.selected_index -= 1;
}
}
}
/// Move selection down (towards newer commits).
pub fn navigate_down(&mut self) {
if self.screen == Screen::List && !self.commits.is_empty() {
if self.selected_index < self.commits.len().saturating_sub(1) {
self.selected_index += 1;
}
}
}
/// Select the current commit and load its detail (body + diff).
/// Transitions to `Detail` on success, stays on `List` on error.
pub fn select_commit(&mut self) {
if self.screen != Screen::List {
return;
}
if self.commits.is_empty() || self.selected_index >= self.commits.len() {
return;
}
let selected = &self.commits[self.selected_index];
let oid = selected.oid.clone();
self.detail_scroll.set(0);
match git::load_diff(&oid) {
Ok((body, diff)) => {
let mut commit = selected.clone();
commit.body = body;
commit.diff = diff;
self.selected_commit = Some(commit);
self.screen = Screen::Detail;
}
Err(_e) => {
// On error, stay on list (diff loading failed silently).
let mut commit = selected.clone();
commit.body = String::new();
commit.diff = String::new();
self.selected_commit = Some(commit);
self.screen = Screen::Detail;
}
}
}
/// Close the detail overlay and return to the list.
pub fn close_detail(&mut self) {
if self.screen == Screen::Detail {
self.screen = Screen::List;
self.selected_commit = None;
self.detail_scroll.set(0);
}
}
/// Dismiss the alert and return to the commit list.
pub fn dismiss_alert(&mut self) {
if matches!(self.screen, Screen::Alert(_)) {
self.screen = Screen::List;
self.selected_commit = None;
self.detail_scroll.set(0);
}
}
/// Scroll up in the detail view (towards earlier content).
pub fn scroll_detail_up(&mut self) {
if self.screen == Screen::Detail && self.detail_scroll.get() > 0 {
self.detail_scroll.set(self.detail_scroll.get() - 1);
}
}
/// Scroll down in the detail view (towards later content).
pub fn scroll_detail_down(&mut self) {
// Ceiling is enforced in the render function where we know content height.
if self.screen == Screen::Detail {
self.detail_scroll.set(self.detail_scroll.get() + 1);
}
}
/// Scroll up by one page in the detail view.
pub fn scroll_detail_page_up(&mut self) {
if self.screen == Screen::Detail && self.detail_scroll.get() > 0 {
let page = self.detail_content_height.get().max(1);
let new = self.detail_scroll.get().saturating_sub(page);
// Clamp: don't overshoot the start when `page` is large.
self.detail_scroll.set(if new > self.detail_scroll.get() {
0
} else {
new
});
}
}
/// Scroll down by one page in the detail view.
pub fn scroll_detail_page_down(&mut self) {
// Ceiling is enforced in the render function where we know content height.
if self.screen == Screen::Detail {
let page = self.detail_content_height.get().max(1);
self.detail_scroll
.set(self.detail_scroll.get().saturating_add(page));
}
}
/// Set the quit flag.
pub fn quit(&mut self) {
self.should_quit = true;
}
/// Dismiss the error screen (equivalent to quitting).
pub fn error_dismiss(&mut self) {
if matches!(self.screen, Screen::Error(_)) {
self.quit();
}
}
}