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>
125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"unicode/utf8"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"vcom/internal/tui/keys"
|
|
"vcom/internal/tui/modal"
|
|
"vcom/internal/tui/notify"
|
|
"vcom/internal/tui/panel"
|
|
"vcom/internal/vfs"
|
|
)
|
|
|
|
// miscState is the UI state of stage 6.6.
|
|
type miscState struct {
|
|
lastSaved string // fingerprint of the last saved session
|
|
shutDown bool
|
|
}
|
|
|
|
// openRegular is Enter on a file that is not an archive (§7.12): an
|
|
// executable opens the Run menu (never runs by itself; U-P1-3, N-P0-2),
|
|
// text goes to the editor, anything else to the desktop opener.
|
|
func (a *App) openRegular(e panel.Entry) tea.Cmd {
|
|
p := a.Active()
|
|
if e.Cat == panel.CatExec && p.FS().Caps().Exec {
|
|
return a.runMenu(e)
|
|
}
|
|
loc, fsys := p.Location(), p.FS()
|
|
path := fsys.Paths().Join(p.Path(), e.Name)
|
|
name := e.Name
|
|
return func() tea.Msg {
|
|
text, err := sniffText(fsys, path)
|
|
return openDecisionMsg{Loc: loc, Name: name, Text: text, Err: err}
|
|
}
|
|
}
|
|
|
|
type openDecisionMsg struct {
|
|
Loc vfs.Location
|
|
Name string
|
|
Text bool
|
|
Err error
|
|
}
|
|
|
|
// sniffText reads the head of the file: valid UTF-8 without NUL is text.
|
|
func sniffText(fsys vfs.FS, path string) (bool, error) {
|
|
f, err := fsys.Open(context.Background(), path)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer f.Close()
|
|
buf := make([]byte, 8192)
|
|
n, err := io.ReadFull(f, buf)
|
|
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
|
return false, err
|
|
}
|
|
b := buf[:n]
|
|
for _, c := range b {
|
|
if c == 0 {
|
|
return false, nil
|
|
}
|
|
}
|
|
if n == len(buf) { // the sample may end in the middle of a rune
|
|
for i := 0; i < 3 && len(b) > 0 && !utf8.Valid(b); i++ {
|
|
b = b[:len(b)-1]
|
|
}
|
|
}
|
|
return utf8.Valid(b), nil
|
|
}
|
|
|
|
func (a *App) onOpenDecision(m openDecisionMsg) tea.Cmd {
|
|
if m.Err != nil {
|
|
return a.notify(notify.Error, "Cannot open "+m.Name+": "+cleanErr(m.Err).Error())
|
|
}
|
|
if m.Text && !m.Loc.FS.Caps().ReadOnly {
|
|
return a.editFile(m.Loc, m.Name)
|
|
}
|
|
return a.openWithDesktop(m.Loc, m.Name)
|
|
}
|
|
|
|
// runMenu: Run / Edit / View / Cancel for an executable.
|
|
func (a *App) runMenu(e panel.Entry) tea.Cmd {
|
|
p := a.Active()
|
|
loc := p.Location()
|
|
path := p.FS().Paths().Join(p.Path(), e.Name)
|
|
items := []modal.Item{
|
|
{Label: "Run", Detail: "r", Value: "run"},
|
|
{Label: "Edit", Detail: "F4", Value: "edit"},
|
|
{Label: "View", Detail: "F3", Value: "view"},
|
|
{Label: "Cancel", Value: "cancel"},
|
|
}
|
|
a.openModal(modal.NewList(e.Name+" is executable", items, nil), func(o modal.Outcome) tea.Cmd {
|
|
if o.Cancelled {
|
|
return nil
|
|
}
|
|
switch o.Value {
|
|
case "run":
|
|
return a.deps.Exec(runCmd(path, loc.Path), func(err error) tea.Msg { return extDoneMsg{Loc: loc, What: "Run " + e.Name, Err: err} })
|
|
case "edit":
|
|
return a.editFile(loc, e.Name)
|
|
case "view":
|
|
return a.dispatch(keys.ScreenView)
|
|
}
|
|
return nil
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// extDoneMsg: a foreground program (editor, Run) returned.
|
|
type extDoneMsg struct {
|
|
Loc vfs.Location
|
|
What string
|
|
Err error
|
|
}
|
|
|
|
func (a *App) onExtDone(m extDoneMsg) tea.Cmd {
|
|
a.applied.mouse = mouseUnknown // the program may have reset the terminal (U-P1-3)
|
|
cmds := []tea.Cmd{a.invalidate([]vfs.Location{m.Loc})}
|
|
if m.Err != nil {
|
|
cmds = append(cmds, a.notify(notify.Warn, m.What+": "+m.Err.Error()))
|
|
}
|
|
return tea.Batch(cmds...)
|
|
}
|