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

69 lines
2 KiB
Go

package tui
import (
"context"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/jobs"
"vcom/internal/tui/modal"
"vcom/internal/tui/notify"
"vcom/internal/vfs"
)
type dirSizeResult struct {
Loc vfs.Location // directory holding the entry
Name string
Size vfs.SizeResult
}
// dirSize is Space: compute the size of the directory under the cursor in
// the background; unreadable subdirectories make it "≥ X" (N-P1-14).
func (a *App) dirSize() tea.Cmd {
p := a.Active()
e, ok := p.Selected()
if !ok || e.Parent || !e.NavigableDir() {
return a.notify(notify.Info, "Directory size: put the cursor on a directory")
}
loc, name := p.Location(), e.Name
fsys, path := p.FS(), p.FS().Paths().Join(p.Path(), e.Name)
fn := func(ctx context.Context, h *jobs.Handle) (any, error) {
r, err := vfs.DirSize(ctx, fsys, path, func(s vfs.SizeResult) {
h.SetProgress(jobs.Progress{Phase: "counting", Current: path, FilesDone: s.Files, BytesDone: s.Bytes})
})
return dirSizeResult{Loc: loc, Name: name, Size: r}, err
}
return a.startJobOpts("dirsize", "Size of "+name, true, fn, (*App).finishDirSize, holdMounts(p.Here())...)
}
// SizeText renders a DirSize result: "12.3 MiB" or "≥ 12.3 MiB (2 unreadable)".
func SizeText(r vfs.SizeResult) string {
if r.Unreadable > 0 {
return fmt.Sprintf("≥ %s (%d unreadable)", modal.HumanBytes(r.Bytes), r.Unreadable)
}
return modal.HumanBytes(r.Bytes)
}
func (a *App) finishDirSize(s jobs.Snapshot) tea.Cmd {
res, _ := s.Result.(dirSizeResult)
if s.State != jobs.Done {
if s.State == jobs.Cancelled {
return nil
}
return a.notify(notify.Error, fmt.Sprintf("Size of %s: %v", res.Name, cleanErr(s.Err)))
}
label := SizeText(res.Size)
short := modal.HumanBytes(res.Size.Bytes)
if res.Size.Unreadable > 0 {
short = "≥" + short
}
if p := a.panelAt(res.Loc); p != nil {
p.SetSizeLabel(res.Name, short)
}
lvl := notify.Info
if res.Size.Unreadable > 0 {
lvl = notify.Warn
}
return a.notify(lvl, fmt.Sprintf("%s: %s, %d files, %d dirs", res.Name, label, res.Size.Files, res.Size.Dirs))
}