vcom/internal/tui/ops_delete.go
vrubelroman 280f3880ef feat(tui): file operations through jobs and transfer (6.2)
Copy/move run as jobs: Prepare (scanning) → plan dialog (files, size,
existing targets with a policy choice, hidden marks) → Execute with a
Progress modal (b/Esc background, c asks before cancelling), conflict
dialogs with apply-to-all and Retry/Skip/Skip all/Abort on item errors,
then a summary and reload of the affected panels (each keeps its cursor).
F8 trashes (default button), refused items are offered for permanent
deletion; X deletes permanently (default Cancel), SSH panels without a
trash go straight to that dialog. F7/F2 work through the panel's FS with
name validation (no "/"). Space computes a directory size in the
background ("≥ X (N unreadable)"); B lists jobs and brings one back.
Read-only (archive) panels and the host list are never targets. Jobs hold
references on the panels' mounts while they run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 13:59:22 +03:00

215 lines
6.3 KiB
Go

package tui
import (
"context"
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/jobs"
"vcom/internal/transfer"
"vcom/internal/tui/modal"
"vcom/internal/tui/notify"
"vcom/internal/tui/panel"
"vcom/internal/vfs"
)
func (a *App) canDelete() error {
p := a.Active()
switch {
case p.Source != panel.SourceDir:
return errors.New("nothing to delete here")
case p.FS().Caps().ReadOnly:
return errors.New("this panel is read-only (archive)")
}
if es, _ := p.Targets(); len(es) == 0 {
return errors.New("nothing selected")
}
return nil
}
// trashSelected is F8/Delete/x: move to the trash (D9, U-P0-4). Without a
// trash on this FS (SSH) it offers permanent deletion, default Cancel.
func (a *App) trashSelected() tea.Cmd {
p := a.Active()
paths, names, hidden := a.opTargets()
fsys, here := p.FS(), p.Here()
tr, ok := fsys.(vfs.Trasher)
if !ok || !fsys.Caps().Trash {
return a.confirmPermanent(here, paths, names, hidden, fmt.Sprintf("%s has no trash.", fsys.Label()))
}
start := func() tea.Cmd {
return a.startJob("trash", "Trash "+describe(names), trashJob(tr, here.Location(), paths), (*App).finishTrash, holdMounts(here)...)
}
if !a.deps.Config.Behavior.ConfirmDelete && hidden == 0 {
return start()
}
d := &modal.Dialog{
Title: fmt.Sprintf("Move %s to the trash?", describe(names)),
Fields: []modal.KV{{Label: "From", Value: displayPath(here.Location())}},
Buttons: []modal.Button{
{Label: "Move to trash", Key: "y", Value: true, Default: true},
{Label: "Cancel", Key: "n", Cancel: true},
},
}
if hidden > 0 {
d.Warnings = []string{fmt.Sprintf("%d marked item(s) hidden by the filter are NOT included", hidden)}
}
a.openModal(d, func(o modal.Outcome) tea.Cmd {
if o.Cancelled {
return nil
}
return start()
})
return nil
}
// deleteSelected is Shift+F8/X: permanent deletion, always confirmed,
// default Cancel.
func (a *App) deleteSelected() tea.Cmd {
p := a.Active()
paths, names, hidden := a.opTargets()
return a.confirmPermanent(p.Here(), paths, names, hidden, "")
}
func (a *App) confirmPermanent(here panel.Target, paths, names []string, hidden int, why string) tea.Cmd {
d := &modal.Dialog{
Title: fmt.Sprintf("Delete %s permanently?", describe(names)),
Fields: []modal.KV{{Label: "From", Value: displayPath(here.Location())}},
Text: []string{"This cannot be undone."},
Buttons: []modal.Button{
{Label: "Delete permanently", Key: "d", Value: true, Danger: true},
{Label: "Cancel", Key: "n", Cancel: true, Default: true},
},
}
if why != "" {
d.Text = append([]string{why}, d.Text...)
}
if len(names) > 1 && len(names) <= 10 {
for _, n := range names {
d.Fields = append(d.Fields, modal.KV{Label: "•", Value: n})
}
}
if hidden > 0 {
d.Warnings = []string{fmt.Sprintf("%d marked item(s) hidden by the filter are NOT included", hidden)}
}
a.openModal(d, func(o modal.Outcome) tea.Cmd {
if o.Cancelled {
return nil
}
return a.startJob("delete", "Delete "+describe(names), deleteJob(here.FS(), here.Location(), paths), nil, holdMounts(here)...)
})
return nil
}
// trashResult is what a trash job returns.
type trashResult struct {
Here panel.Target
Done int
Failed []string // refused by the trash: offered for permanent deletion
Errs []error
Loc vfs.Location
}
func trashJob(tr vfs.Trasher, loc vfs.Location, paths []string) jobs.Func {
return func(ctx context.Context, h *jobs.Handle) (any, error) {
res := trashResult{Loc: loc}
for i, p := range paths {
if err := ctx.Err(); err != nil {
return res, err
}
h.SetProgress(jobs.Progress{Phase: "moving to trash", Current: p, FilesDone: i, FilesTotal: len(paths)})
if err := tr.Trash(ctx, p); err != nil {
res.Failed = append(res.Failed, p) // keep going: only refused items are offered (QA-17)
res.Errs = append(res.Errs, err)
continue
}
res.Done++
}
return res, nil
}
}
func (a *App) finishTrash(s jobs.Snapshot) tea.Cmd {
res, _ := s.Result.(trashResult)
cmds := []tea.Cmd{a.invalidate([]vfs.Location{res.Loc})}
switch {
case s.State == jobs.Cancelled:
cmds = append(cmds, a.notify(notify.Warn, fmt.Sprintf("Trash cancelled (%d moved)", res.Done)))
case len(res.Failed) > 0:
var names []string
for _, p := range res.Failed {
names = append(names, res.Loc.FS.Paths().Base(p))
}
here := panel.Target{Mounts: nil, Path: res.Loc.Path}
if p := a.panelAt(res.Loc); p != nil {
here = p.Here()
}
if len(here.Mounts) == 0 {
break
}
why := fmt.Sprintf("The trash refused %d item(s): %v", len(res.Failed), cleanErr(res.Errs[0]))
cmds = append(cmds, a.confirmPermanent(here, res.Failed, names, 0, why))
default:
cmds = append(cmds, a.notify(notify.Success, fmt.Sprintf("Moved %d item(s) to the trash", res.Done)))
}
return tea.Batch(cmds...)
}
// panelAt returns a panel standing at loc.
func (a *App) panelAt(loc vfs.Location) *panel.Model {
for _, p := range a.panels {
if p.Location().Same(loc) {
return p
}
}
return nil
}
// deleteJob removes trees without following links (vfs.RemoveTree), asking
// on errors (Retry/Skip/Skip all/Abort).
func deleteJob(fsys vfs.FS, loc vfs.Location, paths []string) jobs.Func {
return func(ctx context.Context, h *jobs.Handle) (any, error) {
rep := transfer.Report{Affected: []vfs.Location{loc}}
removed, skipAll := 0, false
for i := 0; i < len(paths); i++ {
p := paths[i]
h.SetProgress(jobs.Progress{Phase: "deleting", Current: p, FilesDone: i, FilesTotal: len(paths)})
err := vfs.RemoveTree(ctx, fsys, p, func(n int) {
h.SetProgress(jobs.Progress{Phase: "deleting", Current: p, FilesDone: i, FilesTotal: len(paths), BytesDone: int64(removed + n)})
})
if err == nil {
rep.Done++
continue
}
if ctx.Err() != nil {
rep.Cancelled = true
return rep, ctx.Err()
}
ie := transfer.ItemError{Item: transfer.Item{Rel: fsys.Paths().Base(p)}, Op: "delete", Err: err}
act := transfer.ErrorSkip
if !skipAll {
ans, aerr := h.Ask(ctx, askItemError{E: ie})
if aerr != nil {
return rep, aerr
}
act, _ = ans.(transfer.ErrorAction)
}
switch act {
case transfer.ErrorRetry:
i--
continue
case transfer.ErrorSkipRest:
skipAll = true
case transfer.ErrorStop:
rep.Failed++
rep.Errors = append(rep.Errors, ie)
return rep, transfer.ErrAborted
}
rep.Failed++
rep.Errors = append(rep.Errors, ie)
}
return rep, nil
}
}