Launch: Commit before refactor

This commit is contained in:
Adam Cooper 2022-05-14 19:06:06 -04:00
parent a69342c27e
commit 96b5d98da2

37
main.go
View file

@ -5,6 +5,7 @@ import (
"io/fs" "io/fs"
"log" "log"
"os" "os"
"os/exec"
"strings" "strings"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@ -76,8 +77,8 @@ func initialModel() model {
choices: Choices{ choices: Choices{
getBrowserSessions(), getBrowserSessions(),
[]sessionOrCommand{{ []sessionOrCommand{{
displayString: "neomutt", displayString: "newsboat",
commandString: "neomutt", commandString: "newsboat",
}, { }, {
displayString: "bottom", displayString: "bottom",
commandString: "btm --group --battery --color gruvbox-light", commandString: "btm --group --battery --color gruvbox-light",
@ -90,8 +91,34 @@ func initialModel() model {
} }
} }
func launch() tea.Msg { func launch(m model) tea.Cmd {
return statusMsg("This is a test launch message.") return func() tea.Msg {
var result statusMsg
for j, item := range m.choices[0] {
if _, ok := m.selected[0][j]; ok {
cmd := exec.Command("qutebrowser", "--restore", item.displayString)
err := cmd.Start()
result += statusMsg(fmt.Sprintf("Launching Qutebrowser session: %v\n", item.displayString))
if err != nil {
result += statusMsg(fmt.Sprintf("%v\n", err))
log.Fatalf("Error starting browser session: %v\n", err)
}
}
}
for k, item := range m.choices[1] {
if _, ok := m.selected[1][k]; ok {
cmd := exec.Command("xterm", "-e", item.commandString)
err := cmd.Start()
result += statusMsg(fmt.Sprintf("Launching command: %v\n", item.displayString))
if err != nil {
result += statusMsg(fmt.Sprintf("%v\n", err))
log.Fatalf("Error starting command: %v\n", err)
}
}
}
return result
}
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -129,7 +156,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.selected[m.activeSection][m.cursor[m.activeSection]] = struct{}{} m.selected[m.activeSection][m.cursor[m.activeSection]] = struct{}{}
} }
case "enter": case "enter":
return m, launch return m, launch(m)
} }
} }