Refine transfer progress and cancellation flow

This commit is contained in:
vrubelroman 2026-04-23 22:46:08 +03:00
parent 95847ad231
commit 6787a7a363
3 changed files with 179 additions and 24 deletions

View file

@ -23,6 +23,7 @@ type CopyProgress struct {
BytesDone int64
BytesTotal int64
CurrentPath string
Stage string
}
type copyProgressState struct {
@ -108,6 +109,9 @@ func CopyPathWithProgressContext(ctx context.Context, srcPath string, dstDir str
if err := copyDir(srcPath, targetPath, &tracker); err != nil {
return cleanupOnErr(err)
}
if err := ctx.Err(); err != nil {
return cleanupOnErr(err)
}
tracker.emit(srcPath, true)
return targetPath, nil
}
@ -215,6 +219,7 @@ func MovePathWithProgressContext(ctx context.Context, srcPath string, dstDir str
BytesDone: stats.BytesTotal,
BytesTotal: stats.BytesTotal,
CurrentPath: srcPath,
Stage: "Move completed",
})
return targetPath, nil
} else if !errors.Is(err, syscall.EXDEV) {
@ -229,6 +234,14 @@ func MovePathWithProgressContext(ctx context.Context, srcPath string, dstDir str
_ = os.RemoveAll(targetPath)
return "", err
}
progress(CopyProgress{
FilesDone: stats.FilesTotal,
FilesTotal: stats.FilesTotal,
BytesDone: stats.BytesTotal,
BytesTotal: stats.BytesTotal,
CurrentPath: srcPath,
Stage: "Finalizing move",
})
if err := DeletePath(srcPath); err != nil {
return "", err
}
@ -313,6 +326,12 @@ func copyDir(srcDir string, dstDir string, tracker *copyProgressState) error {
}
}
if tracker != nil && tracker.ctx != nil {
if err := tracker.ctx.Err(); err != nil {
return err
}
}
return nil
}
@ -343,6 +362,13 @@ func copyFile(srcPath string, dstPath string, mode os.FileMode, tracker *copyPro
_ = os.Remove(dstPath)
return err
}
if tracker != nil && tracker.ctx != nil {
if err := tracker.ctx.Err(); err != nil {
_ = dstFile.Close()
_ = os.Remove(dstPath)
return err
}
}
if tracker != nil {
tracker.finishFile(srcPath)
}
@ -392,6 +418,7 @@ func (s *copyProgressState) emit(currentPath string, force bool) {
BytesDone: s.bytesDone,
BytesTotal: s.stats.BytesTotal,
CurrentPath: currentPath,
Stage: "Transferring data",
})
}

88
internal/fs/ops_test.go Normal file
View file

@ -0,0 +1,88 @@
package vfs
import (
"context"
"errors"
"os"
"path/filepath"
"strconv"
"testing"
)
func TestCopyPathWithProgressContextRemovesPartialTargetOnCancel(t *testing.T) {
t.Parallel()
root := t.TempDir()
srcDir := filepath.Join(root, "src")
dstDir := filepath.Join(root, "dst")
if err := os.MkdirAll(srcDir, 0o755); err != nil {
t.Fatalf("mkdir src: %v", err)
}
if err := os.MkdirAll(dstDir, 0o755); err != nil {
t.Fatalf("mkdir dst: %v", err)
}
for idx := 0; idx < 64; idx++ {
path := filepath.Join(srcDir, "file-"+strconv.Itoa(idx)+".txt")
if err := os.WriteFile(path, []byte("payload-"+strconv.Itoa(idx)), 0o644); err != nil {
t.Fatalf("write source file %d: %v", idx, err)
}
}
stats, err := CopyStats(srcDir)
if err != nil {
t.Fatalf("copy stats: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = CopyPathWithProgressContext(ctx, srcDir, dstDir, false, stats, func(progress CopyProgress) {
if progress.FilesDone >= 1 {
cancel()
}
})
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context cancellation, got %v", err)
}
targetPath := filepath.Join(dstDir, filepath.Base(srcDir))
if _, statErr := os.Stat(targetPath); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("expected partial target to be removed, stat err=%v", statErr)
}
}
func TestMovePathWithProgressContextCancelledBeforeStartKeepsSource(t *testing.T) {
t.Parallel()
root := t.TempDir()
srcFile := filepath.Join(root, "source.txt")
dstDir := filepath.Join(root, "dst")
if err := os.WriteFile(srcFile, []byte("payload"), 0o644); err != nil {
t.Fatalf("write source: %v", err)
}
if err := os.MkdirAll(dstDir, 0o755); err != nil {
t.Fatalf("mkdir dst: %v", err)
}
stats, err := CopyStats(srcFile)
if err != nil {
t.Fatalf("copy stats: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = MovePathWithProgressContext(ctx, srcFile, dstDir, false, stats, nil)
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context cancellation, got %v", err)
}
if _, statErr := os.Stat(srcFile); statErr != nil {
t.Fatalf("expected source to remain in place, stat err=%v", statErr)
}
targetPath := filepath.Join(dstDir, filepath.Base(srcFile))
if _, statErr := os.Stat(targetPath); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("expected destination file to be absent, stat err=%v", statErr)
}
}

View file

@ -337,6 +337,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.busy = false
if msg.err != nil {
activeSelection := selectedName(m.activePane())
_ = m.reloadPane(PaneLeft, activeSelection)
_ = m.reloadPane(PaneRight, activeSelection)
if msg.err == context.Canceled {
m.status = strings.Title(operationVerb(msg.kind)) + " cancelled"
} else {
@ -346,7 +349,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.modal.kind == modalCopyProgress {
m.modal = modalState{}
}
return m, nil
return m, m.loadPreviewCmd()
}
m.status = fmt.Sprintf("%s %d entr%s to %s", operationDoneLabel(msg.kind), len(msg.sourcePaths), pluralSuffix(len(msg.sourcePaths), "y", "ies"), msg.targetDir)
@ -1699,7 +1702,6 @@ func renderCopyProgressModal(job copyJobState, palette theme.Palette, width int)
outerWidth := max(width, 8)
contentWidth := max(outerWidth-6, 1)
titleStyle := lipgloss.NewStyle().Width(contentWidth).Background(palette.Panel).Bold(true).Foreground(palette.Accent)
lineStyle := lipgloss.NewStyle().Width(contentWidth).Background(palette.Panel).Foreground(palette.Text)
mutedStyle := lipgloss.NewStyle().Width(contentWidth).Background(palette.Panel).Foreground(palette.Muted)
spacer := lipgloss.NewStyle().Width(contentWidth).Background(palette.Panel).Render(" ")
@ -1721,9 +1723,11 @@ func renderCopyProgressModal(job copyJobState, palette theme.Palette, width int)
lines := []string{
titleStyle.Render(progressTitle(job.kind)),
spacer,
lineStyle.Render(renderProgressBar(ratio, max(contentWidth-8, 10), palette)),
renderProgressBarLine(ratio, contentWidth, palette),
spacer,
renderProgressPercentLine(ratio, contentWidth, palette),
renderProgressStatLine("Stage:", progressStageLabel(progress, job.kind), contentWidth, palette),
spacer,
renderProgressStatLine("Files:", fmt.Sprintf("%d / %d", progress.FilesDone, progress.FilesTotal), contentWidth, palette),
renderProgressStatLine("Size:", fmt.Sprintf("%s / %s", formatSize(progress.BytesDone, true), formatSize(progress.BytesTotal, true)), contentWidth, palette),
renderProgressStatLine("Speed:", transferSpeed(progress.BytesDone, job.startedAt), contentWidth, palette),
@ -1760,6 +1764,28 @@ func renderProgressBar(ratio float64, width int, palette theme.Palette) string {
return bar + rest
}
func renderProgressBarLine(ratio float64, width int, palette theme.Palette) string {
sidePad := max(width/8, 6)
barWidth := max(width-(sidePad*2), 10)
rightPad := max(width-sidePad-barWidth, 0)
left := lipgloss.NewStyle().
Width(sidePad).
Background(palette.Panel).
Render("")
bar := lipgloss.NewStyle().
Width(barWidth).
Background(palette.Panel).
Render(renderProgressBar(ratio, barWidth, palette))
right := lipgloss.NewStyle().
Width(rightPad).
Background(palette.Panel).
Render("")
return lipgloss.NewStyle().
Width(width).
Background(palette.Panel).
Render(left + bar + right)
}
func renderProgressStatLine(label string, value string, width int, palette theme.Palette) string {
keyWidth := min(max(lipgloss.Width(label)+1, 8), width)
valueWidth := max(width-keyWidth, 0)
@ -1778,10 +1804,10 @@ func renderProgressStatLine(label string, value string, width int, palette theme
func renderProgressActions(width int, palette theme.Palette) string {
const minButtonWidth = 14
const maxButtonWidth = 18
const gapWidth = 4
labelWidth := max(lipgloss.Width("Background / b"), lipgloss.Width("Cancel / c"))
buttonWidth := min(max(labelWidth+2, minButtonWidth), maxButtonWidth)
buttonWidth = min(buttonWidth, max((width-gapWidth)/2, labelWidth))
edgePad := max(width/8, 6)
buttonWidth = min(buttonWidth, max((width-(edgePad*2)-4)/2, labelWidth))
backgroundBtn := lipgloss.NewStyle().
Width(buttonWidth).
@ -1798,25 +1824,21 @@ func renderProgressActions(width int, palette theme.Palette) string {
Bold(true).
Render("Cancel / c")
gap := lipgloss.NewStyle().
Width(gapWidth).
leftPad := lipgloss.NewStyle().
Width(edgePad).
Background(palette.Panel).
Render("")
backgroundBias := lipgloss.NewStyle().
Width(10).
rightPadWidth := edgePad
centerGapWidth := max(width-edgePad-rightPadWidth-(buttonWidth*2), 0)
centerGap := lipgloss.NewStyle().
Width(centerGapWidth).
Background(palette.Panel).
Render("")
cancelBias := lipgloss.NewStyle().
Width(4).
rightPad := lipgloss.NewStyle().
Width(rightPadWidth).
Background(palette.Panel).
Render("")
group := lipgloss.JoinHorizontal(lipgloss.Top, backgroundBtn, backgroundBias, gap, cancelBtn, cancelBias)
row := lipgloss.PlaceHorizontal(
width,
lipgloss.Center,
group,
lipgloss.WithWhitespaceBackground(palette.Panel),
)
row := leftPad + backgroundBtn + centerGap + cancelBtn + rightPad
return lipgloss.NewStyle().
Width(width).
Background(palette.Panel).
@ -1825,14 +1847,12 @@ func renderProgressActions(width int, palette theme.Palette) string {
func renderProgressPercentLine(ratio float64, width int, palette theme.Palette) string {
percent := lipgloss.NewStyle().
Foreground(palette.Info).
Bold(true).
Render(fmt.Sprintf("%3.0f%%", ratio*100))
return lipgloss.NewStyle().
Width(width).
Background(palette.Panel).
Align(lipgloss.Right).
Render(percent)
Foreground(palette.Info).
Bold(true).
Render(fmt.Sprintf("%.0f%%", ratio*100))
return percent
}
func transferSpeed(bytesDone int64, startedAt time.Time) string {
@ -1850,6 +1870,25 @@ func transferSpeed(bytesDone int64, startedAt time.Time) string {
return fmt.Sprintf("%s/s", vfs.HumanSize(perSecond))
}
func progressStageLabel(progress vfs.CopyProgress, kind fileOpKind) string {
if strings.TrimSpace(progress.Stage) != "" {
if progress.Stage == "Transferring data" && progress.BytesTotal > 0 && progress.BytesDone >= progress.BytesTotal {
if progress.FilesDone < progress.FilesTotal {
return "Finalizing file"
}
if kind == opMove {
return "Preparing move finalization"
}
return "Finalizing transfer"
}
return progress.Stage
}
if kind == opMove {
return "Preparing move"
}
return "Transferring data"
}
func overlayCenter(base string, overlay string, width int) string {
if width <= 0 {
return base
@ -2057,6 +2096,7 @@ func (m *Model) startCopyJob(kind fileOpKind, sourcePaths []string, targetDir st
BytesDone: doneBytes + progress.BytesDone,
BytesTotal: stats.BytesTotal,
CurrentPath: progress.CurrentPath,
Stage: progress.Stage,
},
}
}