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
373
374
375
376
377
//! File-manager actions: create / rename / delete (trash) and multi-select / visual range — methods on `App`.
use super::*;
impl App {
// --- ファイル操作: 作成/リネーム/削除 (M7 Phase B・確認+ゴミ箱) -------------
/// Whether a confirm/input dialog is showing (while true, main intercepts keys).
pub fn is_dialog(&self) -> bool {
self.dialog.is_some()
}
/// Whether it is a confirm (y/n) dialog (false = text input dialog). For render/key branching.
pub fn dialog_is_confirm(&self) -> bool {
matches!(
self.dialog.as_ref().map(|d| &d.kind),
Some(DialogKind::Confirm { .. })
)
}
/// Render view for the dialog: (is_confirm, heading/message, text being entered, cursor character position).
/// Preview is rendered via a separate path (`dialog_preview_view`), so this returns None for it.
pub fn dialog_view(&self) -> Option<(bool, &str, &str, usize)> {
match &self.dialog.as_ref()?.kind {
DialogKind::Confirm { message, .. } => Some((true, message.as_str(), "", 0)),
DialogKind::Input {
title,
buffer,
cursor,
} => Some((false, title.as_str(), buffer.as_str(), *cursor)),
DialogKind::Preview { .. } => None,
}
}
/// Whether the batch-rename preview is showing (keys: y apply / Esc cancel / j k scroll).
pub fn dialog_is_preview(&self) -> bool {
matches!(
self.dialog.as_ref().map(|d| &d.kind),
Some(DialogKind::Preview { .. })
)
}
/// For rendering the preview: (heading, the "old → new" list, first displayed row).
pub fn dialog_preview_view(&self) -> Option<(&str, &[String], usize)> {
match &self.dialog.as_ref()?.kind {
DialogKind::Preview {
title,
lines,
scroll,
} => Some((title.as_str(), lines.as_slice(), *scroll)),
_ => None,
}
}
/// Scroll the preview up/down.
pub fn dialog_preview_scroll(&mut self, delta: i32) {
if let Some(Dialog {
kind: DialogKind::Preview { lines, scroll, .. },
..
}) = self.dialog.as_mut()
{
let max = lines.len().saturating_sub(1) as i32;
*scroll = (*scroll as i32 + delta).clamp(0, max) as usize;
}
}
/// Whether the confirm dialog offers `!`=permanent delete (unrecoverable) (true only for delete confirmation).
pub fn dialog_allow_permanent(&self) -> bool {
matches!(
self.dialog.as_ref().map(|d| &d.kind),
Some(DialogKind::Confirm {
allow_permanent: true,
..
})
)
}
/// Whether the current confirm dialog is a branch deletion (so the y/!/n wording is for a branch, not a file deletion).
pub fn confirm_is_branch_delete(&self) -> bool {
matches!(
self.dialog.as_ref().map(|d| &d.op),
Some(PendingOp::GitDeleteBranch { .. })
)
}
/// Whether the current confirm dialog is a drag-and-drop transfer (so the keys/wording become c=copy / m=move).
pub fn confirm_is_drop(&self) -> bool {
matches!(
self.dialog.as_ref().map(|d| &d.op),
Some(PendingOp::DropTransfer { .. })
)
}
/// Whether the current confirm dialog is the app-quit confirmation (so `q`/`y`/Enter quit and the chip/footer say "quit").
pub fn confirm_is_quit(&self) -> bool {
matches!(self.dialog.as_ref().map(|d| &d.op), Some(PendingOp::Quit))
}
/// Receive a paste from the terminal (including drag-and-drop). While input is active (dialog input / filter /
/// search / branch filter), **insert text**; in Tree mode, if the dropped content is an existing path,
/// open a **copy/move dialog** (drop target = the cursor's base directory). Control characters are stripped.
pub fn handle_paste(&mut self, text: String) {
// 1) テキスト入力中: そのまま入力欄へ流す(制御文字は捨てる)。
if self.is_text_input_active() {
for ch in text.chars().filter(|c| !c.is_control()) {
self.input_push_char(ch);
}
return;
}
// 2) Tree モードのみ「ドロップ」として扱う(プレビュー等では無視)。
if !matches!(self.mode, Mode::Tree) {
return;
}
let sources = parse_dropped_paths(&text);
if sources.is_empty() {
return; // ドロップ対象(実在パス)が無ければ何もしない
}
let dir = self.op_base_dir();
let message = format!(
"{} {} → {}",
sources.len(),
crate::i18n::tr(self.lang, crate::i18n::Msg::DroppedItems),
self.format_path(&dir)
);
self.dialog = Some(Dialog {
op: PendingOp::DropTransfer { sources, dir },
kind: DialogKind::Confirm {
message,
allow_permanent: false,
},
});
}
/// Whether text input is active (a state where a paste should be inserted as characters).
fn is_text_input_active(&self) -> bool {
self.dialog_is_text_input()
|| self.is_filtering()
|| self.is_searching()
|| self.git_branch_filtering()
}
/// Whether the currently open dialog is the Input (text input) kind.
fn dialog_is_text_input(&self) -> bool {
matches!(
self.dialog.as_ref().map(|d| &d.kind),
Some(DialogKind::Input { .. })
)
}
/// Feed one character to the active input field (priority: dialog input > filter > search > branch filter).
fn input_push_char(&mut self, ch: char) {
if self.dialog_is_text_input() {
self.dialog_input_push(ch);
} else if self.is_filtering() {
self.filter_input_push(ch);
} else if self.is_searching() {
self.search_input_push(ch);
} else if self.git_branch_filtering() {
self.git_branch_filter_push(ch);
}
}
/// Execute the drop confirmation's `c`=copy / `m`=move. Transfers each source to the drop target and flashes the result.
pub fn drop_apply(&mut self, move_it: bool) -> Result<()> {
let Some(dialog) = self.dialog.take() else {
return Ok(());
};
let PendingOp::DropTransfer { sources, dir } = dialog.op else {
return Ok(());
};
let (mut ok, mut last) = (0usize, None);
let mut err: Option<String> = None;
for src in &sources {
let r = if move_it {
crate::fileops::move_into(&dir, src)
} else {
crate::fileops::copy_into(&dir, src)
};
match r {
Ok(p) => {
ok += 1;
last = Some(p);
}
Err(e) => {
err = Some(e.to_string());
break;
}
}
}
self.refresh()?;
if let Some(p) = &last {
let _ = self.reveal_and_select(p);
}
let verb = if move_it {
crate::i18n::tr(self.lang, crate::i18n::Msg::Moved)
} else {
crate::i18n::tr(self.lang, crate::i18n::Msg::Copied)
};
self.flash = Some(match err {
Some(e) => format!(
"{}: {e}",
crate::i18n::tr(self.lang, crate::i18n::Msg::Failed)
),
None => format!("{verb} ({ok})"),
});
Ok(())
}
/// The operation target directory relative to the cursor (selection is a directory = inside it / a file = its parent / none = root).
pub(super) fn op_base_dir(&self) -> PathBuf {
match self.entries.get(self.selected) {
Some(e) if e.is_dir => e.path.clone(),
Some(e) => e
.path
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| self.root.clone()),
None => self.root.clone(),
}
}
/// Deep reveal: expand **every** collapsed ancestor of `target` under root (rebuilding as each level
/// appears), then select it. Unlike `reveal_and_select` (one level), this reaches into collapsed
/// subtrees — used by the changed-file jump (`n`/`N`) and follow mode. Returns whether the target
/// became visible and selected (false = e.g. hidden by the dotfile filter).
pub(super) fn reveal_path_deep(&mut self, target: &Path) -> Result<bool> {
// root 直下から target の親まで浅い順に。expanded を立てて rebuild すると次の階層が現れる。
let mut ancestors: Vec<PathBuf> = Vec::new();
let mut p = target.parent();
while let Some(a) = p {
if a == self.root || !a.starts_with(&self.root) {
break;
}
ancestors.push(a.to_path_buf());
p = a.parent();
}
ancestors.reverse();
for anc in ancestors {
if let Some(e) = self.entries.iter_mut().find(|e| e.path == anc) {
if e.is_dir && !e.expanded {
e.expanded = true;
self.rebuild_tree()?;
}
}
}
if let Some(i) = self.entries.iter().position(|e| e.path == target) {
self.selected = i;
return Ok(true);
}
Ok(false)
}
/// After an operation, reveal and select `target`. If the parent is a collapsed dir, expand it before rebuilding.
pub(super) fn reveal_and_select(&mut self, target: &Path) -> Result<()> {
if let Some(parent) = target.parent() {
if let Some(e) = self.entries.iter_mut().find(|e| e.path == parent) {
if e.is_dir && !e.expanded {
e.expanded = true;
}
}
}
self.rebuild_tree()?;
if let Some(i) = self.entries.iter().position(|e| e.path == target) {
self.selected = i;
}
Ok(())
}
// --- 複数選択 + ビジュアル(範囲)選択 (M7 Phase B) ------------------------
/// Whether there are any selected items (the committed set).
pub fn has_selection(&self) -> bool {
!self.selection.is_empty()
}
/// Whether `path` is in the committed selection (for the render's marker check).
pub fn is_selected(&self, path: &Path) -> bool {
self.selection.contains(path)
}
/// Clear the entire selection (Esc / after a batch operation). If in visual mode, also clears the range.
pub fn clear_selection(&mut self) {
self.selection.clear();
self.visual_anchor = None;
}
/// `V`=toggle the selection of the single item at the cursor and move down one (for picking scattered items / consecutive selection).
pub fn toggle_select(&mut self) {
if let Some(e) = self.entries.get(self.selected) {
let p = e.path.clone();
if !self.selection.remove(&p) {
self.selection.insert(p);
}
self.tree_next();
}
}
/// Whether visual (range) selection mode is active.
pub fn is_visual(&self) -> bool {
self.visual_anchor.is_some()
}
/// `v`=start visual mode. Places the anchor at the current cursor (does nothing on an empty tree).
pub fn enter_visual(&mut self) {
if !self.entries.is_empty() {
self.visual_anchor = Some(self.selected);
}
}
/// The visual range [lo, hi] (anchor to cursor, ascending). None if not in visual mode.
fn visual_bounds(&self) -> Option<(usize, usize)> {
self.visual_anchor
.map(|a| (a.min(self.selected), a.max(self.selected)))
}
/// Whether row `idx` is within the visual range (for the render's live preview).
pub fn is_in_visual_range(&self, idx: usize) -> bool {
matches!(self.visual_bounds(), Some((lo, hi)) if idx >= lo && idx <= hi)
}
/// Commit the range with `v`/Esc etc.: add the paths in range to the selection set and leave visual mode.
pub fn exit_visual_commit(&mut self) {
if let Some((lo, hi)) = self.visual_bounds() {
let paths: Vec<PathBuf> = (lo..=hi)
.filter_map(|i| self.entries.get(i).map(|e| e.path.clone()))
.collect();
for p in paths {
self.selection.insert(p);
}
}
self.visual_anchor = None;
}
/// Esc=leave visual mode without taking in the range (keeps the committed selection).
pub fn exit_visual_cancel(&mut self) {
self.visual_anchor = None;
}
/// `a`/`A` during visual mode = scope bulk selection. `all_displayed`=everything displayed / otherwise=the same parent level as the cursor.
/// Also takes in the in-progress range, adds it to the selection, and leaves visual mode.
pub fn visual_select_scope(&mut self, all_displayed: bool) {
// 進行中の範囲を確定。
let mut paths: Vec<PathBuf> = self
.visual_bounds()
.map(|(lo, hi)| {
(lo..=hi)
.filter_map(|i| self.entries.get(i).map(|e| e.path.clone()))
.collect()
})
.unwrap_or_default();
// スコープ(全表示 or カーソルと同じ親)を追加。
let parent = self
.entries
.get(self.selected)
.and_then(|e| e.path.parent().map(|p| p.to_path_buf()));
for e in &self.entries {
let same_parent = e.path.parent().map(|p| p.to_path_buf()) == parent;
if all_displayed || same_parent {
paths.push(e.path.clone());
}
}
for p in paths {
self.selection.insert(p);
}
self.visual_anchor = None;
}
/// Whether to render the marker column (leftmost 2 cells): there is a committed selection, or visual mode is active.
pub fn show_selection_gutter(&self) -> bool {
!self.selection.is_empty() || self.is_visual()
}
/// The count currently marked (committed ∪ visual range). For the context's `sel: N` display.
pub fn marked_count(&self) -> usize {
match self.visual_bounds() {
Some((lo, hi)) => {
let mut n = self.selection.len();
for i in lo..=hi {
if let Some(e) = self.entries.get(i) {
if !self.selection.contains(&e.path) {
n += 1;
}
}
}
n
}
None => self.selection.len(),
}
}
/// Target paths for a batch operation: if there is a selection, **all selected items** (path ascending); otherwise, the single item at the cursor.
pub(super) fn op_targets(&self) -> Vec<PathBuf> {
if self.selection.is_empty() {
self.entries
.get(self.selected)
.map(|e| vec![e.path.clone()])
.unwrap_or_default()
} else {
self.selection.iter().cloned().collect()
}
}
}