Enter on a file (archives still go to 6.5): an executable opens a Run / Edit / View menu and never runs by itself (U-P1-3, N-P0-2; Run pauses for Enter afterwards); text goes to $VISUAL/$EDITOR (parsed with quotes); other files go to xdg-open, reaped in the background (no zombies: U-P1-15). A file on SSH (or in an archive) is downloaded to the run dir; edits are uploaded back via WriteAtomic only if the copy changed, and if the server file changed meanwhile the user chooses overwrite / save a copy beside / keep the local copy (U-P1-12). p mirrors with a reference per mount (U-P1-14, N-P1-12); S sorts (saved to the config); y copies paths. The session goes through internal/state: restored at start (remote and archive panels as their local place), saved every 30 s when changed, on SIGHUP (tui.Signal) and in main's teardown (App.Shutdown). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
309 lines
9.5 KiB
Go
309 lines
9.5 KiB
Go
package tui
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"vcom/internal/archive"
|
|
"vcom/internal/jobs"
|
|
"vcom/internal/transfer"
|
|
"vcom/internal/tui/modal"
|
|
"vcom/internal/tui/notify"
|
|
"vcom/internal/tui/panel"
|
|
"vcom/internal/vfs"
|
|
)
|
|
|
|
// editFile is F4 (and Enter on text): local files go straight to the
|
|
// editor; a file on another FS is downloaded to the run dir, edited there
|
|
// and uploaded back with a check that it did not change on the server in
|
|
// the meantime (§7.12, N-P0-2).
|
|
func (a *App) editFile(loc vfs.Location, name string) tea.Cmd {
|
|
path := loc.FS.Paths().Join(loc.Path, name)
|
|
switch {
|
|
case loc.FS.Caps().ReadOnly:
|
|
return a.notify(notify.Warn, name+" is read-only here (archive): use F3 to view it")
|
|
case loc.FS.Kind() == vfs.KindLocal:
|
|
cmd, err := editorCmd(a.deps.Getenv, path)
|
|
if err != nil {
|
|
return a.notify(notify.Error, err.Error())
|
|
}
|
|
return a.deps.Exec(cmd, func(err error) tea.Msg { return extDoneMsg{Loc: loc, What: "Editor", Err: err} })
|
|
}
|
|
return a.startJob("edit-download", "Download "+name, downloadJob(loc, name, a.deps.LocalFS, a.deps.RunDir),
|
|
(*App).finishEditDownload, a.holdAt(loc)...)
|
|
}
|
|
|
|
// holdAt keeps the mounts of a panel standing at loc for a job.
|
|
func (a *App) holdAt(loc vfs.Location) []jobs.Releaser {
|
|
if p := a.panelAt(loc); p != nil {
|
|
return holdMounts(p.Here())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) canEdit() error {
|
|
p := a.Active()
|
|
e, ok := p.Selected()
|
|
switch {
|
|
case p.Source != panel.SourceDir || !ok || e.Parent || e.NavigableDir():
|
|
return fmt.Errorf("put the cursor on a file")
|
|
case !e.IsRegularLike():
|
|
return fmt.Errorf("%s is not a regular file", e.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) editSelected() tea.Cmd {
|
|
p := a.Active()
|
|
e, _ := p.Selected()
|
|
return a.editFile(p.Location(), e.Name)
|
|
}
|
|
|
|
// editCopy is a downloaded copy of a remote file.
|
|
type editCopy struct {
|
|
Loc vfs.Location // remote directory
|
|
Name string
|
|
LocalDir string // run-dir subdirectory holding the copy
|
|
Local string
|
|
Size int64
|
|
ModTime time.Time
|
|
Stat vfs.FileInfo // of the server file when downloaded (mode)
|
|
Sum [32]byte
|
|
Open bool // for the desktop opener (no upload)
|
|
}
|
|
|
|
// downloadJob copies loc/name into a private directory of runDir.
|
|
func downloadJob(loc vfs.Location, name string, local vfs.FS, runDir string) jobs.Func {
|
|
return func(ctx context.Context, h *jobs.Handle) (any, error) {
|
|
rp := loc.FS.Paths().Join(loc.Path, name)
|
|
fi, err := loc.FS.Stat(ctx, rp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var b [4]byte
|
|
_, _ = rand.Read(b[:])
|
|
dir := local.Paths().Join(runDir, "edit-"+hex.EncodeToString(b[:]))
|
|
if err := local.Mkdir(ctx, dir, 0o700); err != nil {
|
|
return nil, err
|
|
}
|
|
c := editCopy{Loc: loc, Name: name, LocalDir: dir, Local: local.Paths().Join(dir, name), Size: fi.Size, ModTime: fi.ModTime, Stat: fi}
|
|
src, err := loc.FS.Open(ctx, rp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer src.Close()
|
|
dst, err := local.Create(ctx, c.Local, 0o600, vfs.CreateExcl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sum := sha256.New()
|
|
n, err := io.Copy(io.MultiWriter(dst, sum), &progressCount{ctx: ctx, r: src, h: h, total: fi.Size, name: name})
|
|
if cerr := dst.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
_ = vfs.RemoveTree(context.WithoutCancel(ctx), local, dir, nil)
|
|
return nil, err
|
|
}
|
|
c.Size = n
|
|
copy(c.Sum[:], sum.Sum(nil))
|
|
return c, nil
|
|
}
|
|
}
|
|
|
|
type progressCount struct {
|
|
ctx context.Context
|
|
r io.Reader
|
|
h *jobs.Handle
|
|
n int64
|
|
total int64
|
|
name string
|
|
}
|
|
|
|
func (p *progressCount) Read(b []byte) (int, error) {
|
|
if err := p.ctx.Err(); err != nil {
|
|
return 0, err
|
|
}
|
|
n, err := p.r.Read(b)
|
|
p.n += int64(n)
|
|
p.h.SetProgress(jobs.Progress{Phase: "downloading", Current: p.name, BytesDone: p.n, BytesTotal: p.total})
|
|
return n, err
|
|
}
|
|
|
|
func (a *App) finishEditDownload(s jobs.Snapshot) tea.Cmd {
|
|
c, ok := s.Result.(editCopy)
|
|
if !ok || s.Err != nil {
|
|
if s.State == jobs.Cancelled {
|
|
return nil
|
|
}
|
|
return a.notify(notify.Error, fmt.Sprintf("%s: %v", s.Title, cleanErr(s.Err)))
|
|
}
|
|
cmd, err := editorCmd(a.deps.Getenv, c.Local)
|
|
if err != nil {
|
|
return a.notify(notify.Error, err.Error())
|
|
}
|
|
return a.deps.Exec(cmd, func(err error) tea.Msg { return remoteEditedMsg{Copy: c, Err: err} })
|
|
}
|
|
|
|
type remoteEditedMsg struct {
|
|
Copy editCopy
|
|
Err error
|
|
}
|
|
|
|
func (a *App) onRemoteEdited(m remoteEditedMsg) tea.Cmd {
|
|
a.applied.mouse = mouseUnknown
|
|
c := m.Copy
|
|
var warn tea.Cmd
|
|
if m.Err != nil {
|
|
warn = a.notify(notify.Warn, "Editor: "+m.Err.Error())
|
|
}
|
|
return tea.Batch(warn, a.startJob("edit-upload", "Upload "+c.Name, uploadJob(c, a.deps.LocalFS), (*App).finishEditUpload, a.holdAt(c.Loc)...))
|
|
}
|
|
|
|
type editUpload struct {
|
|
Copy editCopy
|
|
Outcome string // "unchanged", "uploaded", "copy", "kept"
|
|
Copied string // name of the copy saved beside
|
|
}
|
|
|
|
// uploadJob writes the edited copy back with WriteAtomic. If the copy did
|
|
// not change, nothing is written. If the server file changed since the
|
|
// download, the user chooses: overwrite, save a copy beside, or keep the
|
|
// local copy (U-P1-12).
|
|
func uploadJob(c editCopy, local vfs.FS) jobs.Func {
|
|
return func(ctx context.Context, h *jobs.Handle) (any, error) {
|
|
res := editUpload{Copy: c}
|
|
data, sum, err := readAll(ctx, local, c.Local)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
if sum == c.Sum {
|
|
res.Outcome = "unchanged"
|
|
_ = vfs.RemoveTree(ctx, local, c.LocalDir, nil)
|
|
return res, nil
|
|
}
|
|
fsys := c.Loc.FS
|
|
target := fsys.Paths().Join(c.Loc.Path, c.Name)
|
|
replace := true
|
|
if fi, err := fsys.Stat(ctx, target); err == nil && (fi.Size != c.Size || !fi.ModTime.Equal(c.ModTime)) {
|
|
d := &modal.Dialog{
|
|
Title: "The file changed on the server",
|
|
Fields: []modal.KV{{Label: "File", Value: displayPath(vfs.Location{FS: fsys, Path: target})}, {Label: "Your copy", Value: c.Local}},
|
|
Text: []string{"It was modified after you opened it."},
|
|
Buttons: []modal.Button{
|
|
{Label: "Overwrite", Key: "o", Value: "overwrite", Danger: true},
|
|
{Label: "Save a copy beside", Key: "c", Value: "copy", Default: true},
|
|
{Label: "Keep local copy", Key: "n", Cancel: true},
|
|
},
|
|
}
|
|
ans, err := h.Ask(ctx, askConfirm{D: d})
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
switch ans {
|
|
case "overwrite":
|
|
case "copy":
|
|
free, err := archive.FreeName(ctx, fsys, target)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
target, replace, res.Copied = free, false, fsys.Paths().Base(free)
|
|
default:
|
|
res.Outcome = "kept"
|
|
return res, nil
|
|
}
|
|
}
|
|
h.SetProgress(jobs.Progress{Phase: "uploading", Current: c.Name, BytesTotal: int64(len(data))})
|
|
err = transfer.WriteAtomic(ctx, fsys, target, transfer.WriteOptions{Perm: c.Stat.Mode.Perm(), Replace: replace, Fsync: true},
|
|
func(w io.Writer) error { _, err := w.Write(data); return err })
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.Outcome = "uploaded"
|
|
if res.Copied != "" {
|
|
res.Outcome = "copy"
|
|
}
|
|
_ = vfs.RemoveTree(ctx, local, c.LocalDir, nil)
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func readAll(ctx context.Context, fsys vfs.FS, p string) ([]byte, [32]byte, error) {
|
|
f, err := fsys.Open(ctx, p)
|
|
if err != nil {
|
|
return nil, [32]byte{}, err
|
|
}
|
|
defer f.Close()
|
|
var buf bytes.Buffer
|
|
if _, err := io.Copy(&buf, f); err != nil {
|
|
return nil, [32]byte{}, err
|
|
}
|
|
return buf.Bytes(), sha256.Sum256(buf.Bytes()), nil
|
|
}
|
|
|
|
func (a *App) finishEditUpload(s jobs.Snapshot) tea.Cmd {
|
|
res, _ := s.Result.(editUpload)
|
|
c := res.Copy
|
|
if s.Err != nil {
|
|
if s.State == jobs.Cancelled {
|
|
return a.notify(notify.Warn, fmt.Sprintf("Upload of %s cancelled; your copy: %s", c.Name, c.Local))
|
|
}
|
|
return a.notify(notify.Error, fmt.Sprintf("Upload of %s failed: %v; your copy: %s", c.Name, cleanErr(s.Err), c.Local))
|
|
}
|
|
reload := a.invalidate([]vfs.Location{c.Loc})
|
|
switch res.Outcome {
|
|
case "unchanged":
|
|
return a.notify(notify.Info, c.Name+": no changes")
|
|
case "copy":
|
|
return tea.Batch(reload, a.notify(notify.Warn, fmt.Sprintf("%s changed on the server: your version saved as %s", c.Name, res.Copied)))
|
|
case "kept":
|
|
return a.notify(notify.Warn, fmt.Sprintf("%s not uploaded; your copy: %s", c.Name, c.Local))
|
|
}
|
|
return tea.Batch(reload, a.notify(notify.Success, "Uploaded "+c.Name))
|
|
}
|
|
|
|
// openWithDesktop opens a file with xdg-open (reaped, no zombies:
|
|
// U-P1-15). A file on another FS (SSH, archive) is downloaded to the run
|
|
// dir first; edits of that copy are not synced back.
|
|
func (a *App) openWithDesktop(loc vfs.Location, name string) tea.Cmd {
|
|
if loc.FS.Kind() == vfs.KindLocal {
|
|
opener, path := a.deps.Opener, loc.FS.Paths().Join(loc.Path, name)
|
|
return func() tea.Msg { return openedMsg{Name: name, Err: openDetached(opener, path)} }
|
|
}
|
|
return a.startJob("open-download", "Download "+name, downloadJob(loc, name, a.deps.LocalFS, a.deps.RunDir),
|
|
func(a *App, s jobs.Snapshot) tea.Cmd {
|
|
c, ok := s.Result.(editCopy)
|
|
if !ok || s.Err != nil {
|
|
if s.State == jobs.Cancelled {
|
|
return nil
|
|
}
|
|
return a.notify(notify.Error, fmt.Sprintf("%s: %v", s.Title, cleanErr(s.Err)))
|
|
}
|
|
opener := a.deps.Opener
|
|
return func() tea.Msg { return openedMsg{Name: name, Copy: true, Err: openDetached(opener, c.Local)} }
|
|
}, a.holdAt(loc)...)
|
|
}
|
|
|
|
type openedMsg struct {
|
|
Name string
|
|
Copy bool
|
|
Err error
|
|
}
|
|
|
|
func (a *App) onOpened(m openedMsg) tea.Cmd {
|
|
switch {
|
|
case m.Err != nil:
|
|
return a.notify(notify.Error, fmt.Sprintf("Cannot open %s: %v", m.Name, m.Err))
|
|
case m.Copy:
|
|
return a.notify(notify.Info, "Opened a local copy of "+m.Name+" (changes are not synced back)")
|
|
}
|
|
return a.notify(notify.Info, "Opened "+m.Name)
|
|
}
|