vcom/internal/tui/ops_transfer.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

145 lines
4.1 KiB
Go

package tui
import (
"context"
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/jobs"
"vcom/internal/transfer"
"vcom/internal/tui/panel"
)
// releaser adapts a release func to jobs.Releaser.
type releaser func()
func (r releaser) Release() { r() }
// holdMounts takes a reference on every mount of the given targets for the
// lifetime of a job (an SSH lease must not be closed under a transfer when
// the panel leaves the host; N-P1-12).
func holdMounts(ts ...panel.Target) []jobs.Releaser {
var out []jobs.Releaser
seen := map[uint64]bool{}
for _, t := range ts {
for _, m := range t.Mounts {
if m.Acquire == nil || seen[m.ID] {
continue
}
seen[m.ID] = true
if rel := m.Acquire(); rel != nil {
out = append(out, releaser(rel))
}
}
}
return out
}
// transferOptions maps [transfer]/[behavior] of the config.
func (a *App) transferOptions() transfer.Options {
c := a.deps.Config
o := transfer.DefaultOptions()
o.PreserveMode, o.PreserveTimes = c.Transfer.Preserve, c.Transfer.Preserve
switch c.Transfer.OnConflict {
case "overwrite":
o.Conflict = transfer.Overwrite
case "skip":
o.Conflict = transfer.Skip
case "newer":
o.Conflict = transfer.OverwriteOlder
case "rename":
o.Conflict = transfer.KeepBoth
}
switch c.Transfer.Fsync {
case "always":
o.Fsync = transfer.FsyncAlways
case "never":
o.Fsync = transfer.FsyncNever
}
if c.Behavior.FollowSymlinks {
o.Symlinks = transfer.FollowLinks
}
return o
}
// opTargets are the active panel's targets as paths.
func (a *App) opTargets() (paths, names []string, hidden int) {
p := a.Active()
es, hidden := p.Targets()
for _, e := range es {
names = append(names, e.Name)
paths = append(paths, p.FS().Paths().Join(p.Path(), e.Name))
}
return paths, names, hidden
}
// canTransfer: the source lists files, the target is a writable directory
// (not the SSH host list, not an archive; N-P2-10, U-P0-2).
func (a *App) canTransfer(move bool) error {
src, dst := a.Active(), a.Passive()
switch {
case src.Source != panel.SourceDir:
return errors.New("nothing to copy here")
case dst.Source != panel.SourceDir:
return errors.New("the other panel is not a directory")
case dst.FS().Caps().ReadOnly:
return errors.New("the other panel is read-only (archive)")
case move && src.FS().Caps().ReadOnly:
return errors.New("cannot move out of a read-only panel")
}
if es, _ := src.Targets(); len(es) == 0 {
return errors.New("nothing selected")
}
return nil
}
func (a *App) startTransfer(op transfer.Op) tea.Cmd {
src, dst := a.Active(), a.Passive()
paths, names, hidden := a.opTargets()
opts := a.transferOptions()
req := transfer.Request{Op: op, From: transfer.FSSource(src.FS(), paths, opts.Symlinks), To: dst.FS(), ToDir: dst.Path(), Opts: opts}
verb, kind := "Copy", jobs.Kind("copy")
if op == transfer.Move {
verb, kind = "Move", "move"
}
title := fmt.Sprintf("%s %s", verb, describe(names))
return a.startJob(kind, title, transferJob(req, verb, hidden), nil, holdMounts(src.Here(), dst.Here())...)
}
// describe names a selection: "a.txt" or "3 items".
func describe(names []string) string {
if len(names) == 1 {
return names[0]
}
return fmt.Sprintf("%d items", len(names))
}
// transferJob: Prepare (scanning) → plan dialog → Execute. It captures only
// the request.
func transferJob(req transfer.Request, verb string, hidden int) jobs.Func {
return func(ctx context.Context, h *jobs.Handle) (any, error) {
h.SetProgress(jobs.Progress{Phase: "scanning"})
plan, err := transfer.Prepare(ctx, req, func(p transfer.Progress) {
h.SetProgress(jobs.Progress{Phase: "scanning", FilesTotal: p.FilesTotal, BytesTotal: p.BytesTotal})
})
if err != nil {
return transfer.Report{Err: err}, err
}
ans, err := h.Ask(ctx, askPlan{Verb: verb, Plan: plan, Hidden: hidden})
if err != nil {
return transfer.Report{Cancelled: true}, err
}
pol, ok := ans.(transfer.ConflictPolicy)
if !ok {
return transfer.Report{}, errUserCancelled
}
plan.Req.Opts.Conflict = pol
rep := transfer.Execute(ctx, plan, transferHooks(h))
if rep.Cancelled {
return rep, context.Canceled
}
return rep, rep.Err
}
}