feat(tui): archives as read-only mounts, create and extract via jobs (6.5)

Enter on an archive indexes it in a job (progress, cancel; Update never
waits: U-P1-9) and pushes an archivefs mount; ".." from its root returns
with the cursor on the archive. Nothing is unpacked to a temp directory
and every mutating action is refused inside (U-P0-2). a: format list →
name proposed by archive.Name → archive.Create in a job (refuses to write
over or into a source: U-P0-1). e/F11 extracts the archive under the
cursor into the other panel (local or SSH: N-P1-15) through the same
plan → execute flow as copy, so existing files are conflicts (U-P1-11);
inside an archive it copies the marked members out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-11-01 14:01:45 +03:00
parent 280f3880ef
commit b5bad66e36
5 changed files with 430 additions and 4 deletions

View file

@ -0,0 +1,87 @@
package tui
import (
"context"
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/archive"
"vcom/internal/jobs"
"vcom/internal/transfer"
"vcom/internal/tui/modal"
"vcom/internal/tui/panel"
)
func (a *App) canCreateArchive() error {
p := a.Active()
switch {
case p.Source != panel.SourceDir:
return errors.New("nothing to archive 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
}
// createArchive is a: format list → name (proposed by archive.Name) → a
// job writing the archive into this panel's directory through
// archive.Create (temp + rename; never over or into a source: U-P0-1).
func (a *App) createArchive() tea.Cmd {
p := a.Active()
paths, names, _ := a.opTargets()
here := p.Here()
items := []modal.Item{
{Label: "zip", Value: archive.Zip},
{Label: "tar.gz", Value: archive.TarGz},
{Label: "tar", Value: archive.Tar},
}
a.openModal(modal.NewList("Archive format", items, nil), func(o modal.Outcome) tea.Cmd {
if o.Cancelled {
return nil
}
f := o.Value.(archive.Format)
in := modal.NewInput("Create archive", modal.Field{Label: "Name", Value: archive.Name(names, f)})
in.Text = fmt.Sprintf("%s → %s", describe(names), displayPath(here.Location()))
in.Validate = func(v []string) error { return validName(v[0]) }
a.openModal(in, func(o modal.Outcome) tea.Cmd {
if o.Cancelled {
return nil
}
name := o.Value.([]string)[0]
fsys := here.FS()
target := fsys.Paths().Join(here.Path, name)
fn := func(ctx context.Context, h *jobs.Handle) (any, error) {
rep := archive.Create(ctx, fsys, paths, fsys, target, f, transferHooks(h))
if rep.Cancelled {
return rep, context.Canceled
}
return rep, rep.Err
}
return a.startJob("archive", "Archive "+name, fn, nil, holdMounts(here)...)
})
return nil
})
return nil
}
// unpackJob indexes the archive, then runs the same plan → execute flow as
// copy, with the archive as the source (one pass over it).
func unpackJob(src panel.Target, archivePath string, to panel.Target, opts transfer.Options) jobs.Func {
return func(ctx context.Context, h *jobs.Handle) (any, error) {
h.SetProgress(jobs.Progress{Phase: "reading the archive index", Current: archivePath})
idx, err := archive.IndexFile(ctx, src.FS(), archivePath, func(n int) {
h.SetProgress(jobs.Progress{Phase: "reading the archive index", Current: archivePath, FilesDone: n})
})
if err != nil {
return transfer.Report{Err: err}, err
}
req := transfer.Request{Op: transfer.Copy, From: archive.NewSource(src.FS(), archivePath, idx, nil),
To: to.FS(), ToDir: to.Path, Opts: opts}
return transferJob(req, "Extract", 0)(ctx, h)
}
}

View file

@ -0,0 +1,77 @@
package tui
import (
"context"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/archive"
"vcom/internal/jobs"
"vcom/internal/tui/notify"
"vcom/internal/tui/panel"
"vcom/internal/vfs"
"vcom/internal/vfs/archivefs"
)
// isArchive reports whether the entry looks like an archive vcom can open.
func isArchive(e panel.Entry) bool {
if !e.IsRegularLike() {
return false
}
_, ok := archive.DetectFormat(e.Name)
return ok
}
type mountResult struct {
Pane int
Origin panel.Target // where the archive file lies
Name string
Index *archive.Index
}
// openArchive is Enter on an archive: a job indexes it (progress, cancel;
// the UI never waits — U-P1-9) and the panel enters it as a read-only
// mount; ".." from its root comes back with the cursor on the archive.
// Nothing is unpacked to a temp directory (U-P0-2). An archive on a remote
// FS is indexed and read over the connection.
func (a *App) openArchive(e panel.Entry) tea.Cmd {
p := a.Active()
pane, origin, name := a.active, p.Here(), e.Name
fsys, path := p.FS(), p.FS().Paths().Join(p.Path(), e.Name)
fn := func(ctx context.Context, h *jobs.Handle) (any, error) {
h.SetProgress(jobs.Progress{Phase: "reading the archive index", Current: path})
idx, err := archive.IndexFile(ctx, fsys, path, func(n int) {
h.SetProgress(jobs.Progress{Phase: "reading the archive index", Current: path, FilesDone: n})
})
return mountResult{Pane: pane, Origin: origin, Name: name, Index: idx}, err
}
return a.startJob("mount", "Open "+name, fn, (*App).finishMount, holdMounts(origin)...)
}
func (a *App) finishMount(s jobs.Snapshot) tea.Cmd {
res, _ := s.Result.(mountResult)
switch {
case s.State == jobs.Cancelled:
return nil
case s.Err != nil || res.Index == nil:
return a.notify(notify.Error, fmt.Sprintf("Cannot open %s: %v", res.Name, cleanErr(s.Err)))
}
p := a.panels[res.Pane]
if !p.Location().Same(res.Origin.Location()) {
return a.notify(notify.Info, res.Name+" indexed, but the panel moved on")
}
outer := res.Origin.FS()
afs := archivefs.New(outer, outer.Paths().Join(res.Origin.Path, res.Name), res.Index)
ret := res.Origin.Location()
m := panel.NewMount(afs, "/", &ret, res.Name, nil, nil)
t := panel.Target{Mounts: append(res.Origin.Mounts, m), Path: "/"}
var cmd tea.Cmd
if n := len(res.Index.Rejected); n > 0 {
cmd = a.notify(notify.Warn, fmt.Sprintf("%s: %d unsafe member name(s) ignored", res.Name, n))
}
return tea.Batch(a.navigate(res.Pane, t, "", panel.NavPush), cmd)
}
// inArchive reports whether the panel shows an archive.
func inArchive(p *panel.Model) bool { return p.FS().Kind() == vfs.KindArchive }

View file

@ -0,0 +1,196 @@
package tui
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"vcom/internal/archive"
"vcom/internal/testutil/fstree"
"vcom/internal/transfer"
"vcom/internal/tui/modal"
"vcom/internal/vfs"
"vcom/internal/vfs/localfs"
"vcom/internal/vfs/vfstest"
)
// makeArchive archives src/<names> into dir/<name>.
func makeArchive(t *testing.T, src string, names []string, dst string, f archive.Format) {
t.Helper()
var paths []string
for _, n := range names {
paths = append(paths, filepath.Join(src, n))
}
if rep := archive.Create(context.Background(), localfs.New(), paths, localfs.New(), dst, f, transfer.Hooks{}); rep.Err != nil {
t.Fatal(rep.Err)
}
}
func (h *opsH) enterArchive(name string) {
h.t.Helper()
h.cursorTo(name)
h.press("enter")
h.until("archive mounted", func() bool { return inArchive(h.app.Active()) })
}
func archiveDir(t *testing.T) string {
dir := tree(t, fstree.Spec{fstree.File("src/inner/a.txt", "alpha"), fstree.File("src/b.txt", "beta"), fstree.File("w.txt", "w")})
makeArchive(t, filepath.Join(dir, "src"), []string{"inner", "b.txt"}, filepath.Join(dir, "box.zip"), archive.Zip)
return dir
}
// U-P0-2: inside an archive every mutating action is refused, and the
// archive panel is never a target.
func TestUP02_ArchivePaneReadOnlyActionsDisabled(t *testing.T) {
t.Setenv("TMPDIR", t.TempDir()) // the old UI extracted archives here
dir := archiveDir(t)
h := newOps(t, localfs.New(), dir, dir)
h.enterArchive("box.zip")
h.cursorTo("b.txt")
for _, k := range []string{"f6", "f7", "f8", "X", "f2", "a"} {
h.press(k)
if h.app.modals.Len() != 0 || h.mgr.Active() != 0 {
t.Fatalf("%s acted inside the archive", k)
}
}
h.press("tab")
h.cursorTo("w.txt")
h.press("f5")
if h.app.modals.Len() != 0 || !h.d.ScreenContains("read-only") {
t.Fatalf("copy into the archive not refused:\n%s", h.d.Screen())
}
if ents, _ := os.ReadDir(os.TempDir()); len(ents) != 0 {
t.Fatalf("temp extraction dirs: %v", ents)
}
// Reading works: copy a member out of the archive.
h.press("tab")
h.cursorTo("b.txt")
h.press("tab")
h.press("tab")
h.press("f5")
h.waitDialog("Copy 1 item(s)?")
h.press("y")
h.idle()
// The other panel is dir itself: b.txt lands next to box.zip.
if read(t, filepath.Join(dir, "b.txt")) != "beta" {
t.Fatal("copy out of the archive failed")
}
}
func TestLeaveArchiveCursorOnArchive(t *testing.T) {
dir := archiveDir(t)
h := newOps(t, localfs.New(), dir, dir)
h.enterArchive("box.zip")
h.cursorTo("inner")
h.press("enter")
if h.app.Active().Path() != "/inner" {
t.Fatalf("path %s", h.app.Active().Path())
}
h.press("backspace", "backspace")
if inArchive(h.app.Active()) || h.app.Active().Path() != dir || h.selected() != "box.zip" {
t.Fatalf("left to %s on %q", h.app.Active().Path(), h.selected())
}
}
func TestEnterArchiveNonBlocking(t *testing.T) {
var spec fstree.Spec
for i := range 300 {
spec = append(spec, fstree.File(fmt.Sprintf("big/f%03d", i), "").WithSize(4<<10))
}
dir := tree(t, spec)
makeArchive(t, dir, []string{"big"}, filepath.Join(dir, "big.tar.gz"), archive.TarGz)
slow := &vfstest.FaultFS{FS: localfs.New(), Hook: func(op vfstest.Op, p string) error {
if op == vfstest.OpRead && filepath.Base(p) == "big.tar.gz" {
time.Sleep(2 * time.Millisecond)
}
return nil
}}
h := newOps(t, slow, dir, dir)
h.cursorTo("big.tar.gz")
start := time.Now()
h.d.Press("enter")
if el := time.Since(start); el > 20*time.Millisecond {
t.Fatalf("Enter on an archive took %v in Update (U-P1-9)", el)
}
h.d.Settle(settle)
if _, ok := h.app.modals.Top().(*modal.Progress); !ok {
t.Fatalf("no progress while indexing: %T", h.app.modals.Top())
}
start = time.Now()
h.d.Press("j") // keys still handled (the progress modal owns them)
if el := time.Since(start); el > 20*time.Millisecond {
t.Fatalf("key took %v while indexing", el)
}
h.until("archive mounted", func() bool { return inArchive(h.app.Active()) })
h.cursorTo("big")
h.press("enter")
if h.app.Active().VisibleLen() < 300 {
t.Fatalf("%d entries", h.app.Active().VisibleLen())
}
}
// U-P0-1 through the UI: archiving data.tar as tar into its own directory
// proposes the same name; Create refuses, the source stays intact.
func TestArchiveSameDirDoesNotDestroySource(t *testing.T) {
dir := tree(t, fstree.Spec{fstree.File("data.tar", "").WithSize(64 << 10)})
before := read(t, filepath.Join(dir, "data.tar"))
h := newOps(t, localfs.New(), dir, dir)
h.cursorTo("data.tar")
h.press("a", "j", "j", "enter") // format: tar
in, ok := h.app.modals.Top().(*modal.Input)
if !ok || in.Values()[0] != "data.tar" {
t.Fatalf("name input %T %v", h.app.modals.Top(), in)
}
h.press("enter")
h.idle()
if read(t, filepath.Join(dir, "data.tar")) != before {
t.Fatal("source destroyed (U-P0-1)")
}
if !h.d.ScreenContains("overwrite one of its sources") {
t.Fatalf("no error:\n%s", h.d.Screen())
}
// Another name works and the archive is listed.
h.press("a", "enter", "enter") // zip, proposed name data.zip
h.idle()
if _, err := archive.IndexFile(context.Background(), localfs.New(), filepath.Join(dir, "data.zip"), nil); err != nil {
t.Fatalf("data.zip: %v", err)
}
found := false
for i := 0; i < h.app.Active().VisibleLen(); i++ {
e, _ := h.app.Active().Visible(i)
found = found || e.Name == "data.zip"
}
if !found {
t.Fatal("panel not reloaded after archiving")
}
}
// N-P1-15: extracting into a remote panel.
func TestUnpackToRemotePane(t *testing.T) {
dir := archiveDir(t)
rdir := t.TempDir()
h := newOps(t, localfs.New(), dir, dir)
h.mountRemote(1, standFS(t), rdir)
h.cursorTo("box.zip")
h.press("e")
h.waitDialog("Extract")
h.press("y")
h.idle()
if read(t, filepath.Join(rdir, "inner", "a.txt")) != "alpha" || read(t, filepath.Join(rdir, "b.txt")) != "beta" {
t.Fatal("not extracted to the remote panel")
}
noParts(t, rdir)
// Again: the existing files are conflicts, not silent overwrites (U-P1-11).
h.press("e")
d := h.waitDialog("Extract")
if len(d.Buttons) < 3 {
t.Fatalf("plan dialog without conflict choices: %v", fieldsOf(d))
}
h.press("s") // skip existing
h.idle()
}
var _ = vfs.KindArchive

View file

@ -0,0 +1,42 @@
package tui
import (
"errors"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/transfer"
)
// canUnpack: an archive under the cursor (or a panel inside an archive)
// and a writable directory in the other panel.
func (a *App) canUnpack() error {
if inArchive(a.Active()) {
return a.canTransfer(false) // extracting members = copying out of the archive FS
}
e, ok := a.Active().Selected()
if !ok || !isArchive(e) {
return errors.New("put the cursor on an archive")
}
dst := a.Passive()
if dst.FS().Caps().ReadOnly {
return errors.New("the other panel is read-only (archive)")
}
return a.canTransfer(false)
}
// unpack is e/F11: extract the archive under the cursor into the other
// panel (any FS, N-P1-15) through the transfer engine: conflicts are asked,
// files are written via temp + rename, existing symlinks in the target are
// never written through (U-P1-11). Inside an archive it copies the marked
// members out.
func (a *App) unpack() tea.Cmd {
if inArchive(a.Active()) {
return a.startTransfer(transfer.Copy)
}
src, dst := a.Active(), a.Passive()
e, _ := src.Selected()
path := src.FS().Paths().Join(src.Path(), e.Name)
return a.startJob("unpack", "Extract "+e.Name, unpackJob(src.Here(), path, dst.Here(), a.transferOptions()), nil,
holdMounts(src.Here(), dst.Here())...)
}

View file

@ -1,7 +1,31 @@
package tui
// registerArchive adds the action handlers of stage 6.5 (stub until then).
func registerArchive(t actionTable) {}
import (
tea "github.com/charmbracelet/bubbletea"
// registerArchiveHooks adds message and job-event handlers of stage 6.5.
func registerArchiveHooks(a *App) {}
"vcom/internal/tui/keys"
"vcom/internal/tui/notify"
"vcom/internal/tui/panel"
)
// registerArchive adds the archive actions of stage 6.5.
func registerArchive(t actionTable) {
t.add(keys.ArchiveNew, (*App).createArchive, (*App).canCreateArchive)
t.add(keys.ArchiveUnzip, (*App).unpack, (*App).canUnpack)
}
// registerArchiveHooks makes Enter on an archive open it. Another openFile
// set before (or after, if it chains the same way) handles other files.
func registerArchiveHooks(a *App) {
registerOpsState(a)
prev := a.openFile
a.openFile = func(a *App, e panel.Entry) tea.Cmd {
if isArchive(e) {
return a.openArchive(e)
}
if prev != nil {
return prev(a, e)
}
return a.notify(notify.Info, "Opening files: not available yet")
}
}