vcom/internal/tui/session.go
vrubelroman 066d3494c2 feat(tui): Enter dispatch, external programs, remote edit, mirror, sort, session (6.6)
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>
2026-11-01 14:10:52 +03:00

99 lines
2.4 KiB
Go

package tui
import (
"fmt"
"time"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/state"
)
// sessionInterval: the session is saved this often when it changed (§9.2).
const sessionInterval = 30 * time.Second
type (
sessionTickMsg struct{}
sessionSavedMsg struct {
fp string
err error
}
)
// miscInit starts the periodic session save.
func (a *App) miscInit() tea.Cmd {
return tea.Tick(sessionInterval, func(time.Time) tea.Msg { return sessionTickMsg{} })
}
// SessionSnapshot is what would be saved now. A panel inside an archive or
// on a host is saved as the local directory it was entered from (no
// auto-connect on start).
func (a *App) SessionSnapshot() state.Session {
s := state.Session{ActivePane: "left"}
if a.active == 1 {
s.ActivePane = "right"
}
vol := a.deps.LocalFS.Volume()
for i, p := range a.panels {
path, focus := p.LocalPlace()
ps := state.PaneSession{Path: path, EntryName: focus, CursorMemory: p.MemoryOf(vol)}
if i == 0 {
s.Left = ps
} else {
s.Right = ps
}
}
return s
}
func fingerprint(s state.Session) string { return fmt.Sprintf("%v", s) } // maps print sorted
// SaveSession writes the session now (teardown, signals).
func (a *App) SaveSession() error {
s := a.SessionSnapshot()
if err := a.deps.SaveSession(s); err != nil {
return err
}
a.misc.lastSaved = fingerprint(s)
return nil
}
// Shutdown saves the session once; main calls it after Program.Run returns
// (quit, signal, recovered panic).
func (a *App) Shutdown() error {
if a.misc.shutDown {
return nil
}
a.misc.shutDown = true
return a.SaveSession()
}
func (a *App) onSessionTick() tea.Cmd {
next := a.miscInit()
s := a.SessionSnapshot()
fp := fingerprint(s)
if fp == a.misc.lastSaved || a.misc.shutDown {
return next
}
save := a.deps.SaveSession
return tea.Batch(next, func() tea.Msg { return sessionSavedMsg{fp: fp, err: save(s)} })
}
// restoreSession applies the saved active pane and cursor memory (the start
// directories were chosen by DefaultDeps).
func (a *App) restoreSession() {
s := a.deps.Session
if s == nil {
return
}
if s.ActivePane == "right" {
a.active = 1
}
vol := a.deps.LocalFS.Volume()
for i, ps := range []state.PaneSession{s.Left, s.Right} {
a.panels[i].SeedMemory(vol, ps.CursorMemory)
if ps.EntryName != "" && ps.Path == a.deps.StartDirs[i] {
a.panels[i].SeedMemory(vol, map[string]string{ps.Path: ps.EntryName})
}
}
}