-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommands.go
36 lines (31 loc) · 925 Bytes
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package tui
import (
"errors"
"fmt"
"os"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
)
// NavigateTo sends an instruction to navigate to a page with the given model
// kind, and optionally parent resource.
func NavigateTo(kind Kind, opts ...NavigateOption) tea.Cmd {
return CmdHandler(NewNavigationMsg(kind, opts...))
}
func ReportInfo(msg string, args ...any) tea.Cmd {
return CmdHandler(InfoMsg(fmt.Sprintf(msg, args...)))
}
func OpenEditor(path string) tea.Cmd {
// TODO: check for side effects of exec blocking the tui - do
// messages get queued up?
editor, ok := os.LookupEnv("EDITOR")
if !ok {
return ReportError(errors.New("cannot open editor: environment variable EDITOR not set"))
}
cmd := exec.Command(editor, path)
return tea.ExecProcess(cmd, func(err error) tea.Msg {
if err != nil {
return ReportError(fmt.Errorf("opening %s in editor: %w", path, err))()
}
return nil
})
}