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

105 lines
2.7 KiB
Go

package modal
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"vcom/internal/tui/keys"
"vcom/internal/tui/style"
)
// ProgressState is what a Progress modal shows; the App updates it from job
// progress ticks.
type ProgressState struct {
Phase string
Current string
FilesDone int
FilesTotal int
BytesDone int64
BytesTotal int64
Waiting bool // the job waits for an answer
}
// ProgressBackground is the Value of a Progress closed with b/Esc/Enter: the
// job goes on in the background.
const ProgressBackground = "background"
// Progress shows a running job. b/Esc/Enter send it to the background
// (closes); c asks to cancel through OnCancel (the App opens a
// confirmation above it, so "c c" never cancels by itself; U-P1-16).
type Progress struct {
Title string
State ProgressState
OnCancel func() tea.Msg
Width int
}
func (p *Progress) Context() keys.Context { return keys.CtxModalProgress }
func (p *Progress) Key(a keys.ActionID, _ tea.KeyMsg) (Outcome, tea.Cmd) {
switch a {
case keys.ProgressBackground:
return Done(ProgressBackground), nil
case keys.ProgressCancel:
if p.OnCancel != nil {
return Keep, p.OnCancel
}
}
return Keep, nil
}
func (p *Progress) Mouse(tea.MouseMsg, int, int, int, int, *style.Styles) (Outcome, tea.Cmd) {
return Keep, nil
}
// HumanBytes formats a byte count (1.5 MiB).
func HumanBytes(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
div, exp := int64(unit), 0
for m := n / unit; m >= unit; m /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
}
func (p *Progress) View(maxW, maxH int, st *style.Styles) string {
w := p.Width
if w == 0 {
w = 60
}
cw := innerWidth(maxW, w)
s := p.State
var lines []string
phase := s.Phase
if s.Waiting {
phase = "waiting for your answer"
}
if phase != "" {
lines = append(lines, ansi.Truncate(phase, cw, "…"))
}
if s.Current != "" {
lines = append(lines, ansi.Truncate(s.Current, cw, "…"))
}
if s.FilesTotal > 0 {
lines = append(lines, fmt.Sprintf("Files: %d / %d", s.FilesDone, s.FilesTotal))
}
if s.BytesTotal > 0 {
lines = append(lines, fmt.Sprintf("Bytes: %s / %s", HumanBytes(s.BytesDone), HumanBytes(s.BytesTotal)))
frac := float64(s.BytesDone) / float64(s.BytesTotal)
filled := int(frac * float64(cw))
filled = max(0, min(cw, filled))
lines = append(lines, strings.Repeat("█", filled)+strings.Repeat("░", cw-filled))
}
lines = append(lines, "", st.Muted.Render(ansi.Truncate("b/Esc: background · c: cancel", cw, "…")))
if maxH > 2 && len(lines) > maxH-3 {
lines = lines[len(lines)-(maxH-3):]
}
return frame(st, p.Title, lines, maxW)
}