Refactor: change command string to command struct

This commit is contained in:
Adam Cooper 2022-05-14 19:44:37 -04:00
parent 96b5d98da2
commit 68d82a3f86

18
main.go
View file

@ -19,7 +19,7 @@ const (
type sessionOrCommand struct {
displayString string
commandString string
command *exec.Cmd
}
type Choices [][]sessionOrCommand
@ -61,7 +61,7 @@ func getBrowserSessions() []sessionOrCommand {
for _, entry := range fileList {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".yml") {
log.Printf("INFO %s", entry.Name())
result = append(result, sessionOrCommand{displayString: strings.TrimSuffix(entry.Name(), ".yml"), commandString: ""})
result = append(result, sessionOrCommand{displayString: strings.TrimSuffix(entry.Name(), ".yml"), command: nil})
}
}
// log.Printf("result: %v", result)
@ -78,10 +78,10 @@ func initialModel() model {
getBrowserSessions(),
[]sessionOrCommand{{
displayString: "newsboat",
commandString: "newsboat",
command: exec.Command("xterm", "-maximized", "-e", "newsboat"),
}, {
displayString: "bottom",
commandString: "btm --group --battery --color gruvbox-light",
command: exec.Command("xterm", "-maximized", "-e", "btm", "--group", "--battery", "--color", "gruvbox-light"),
}},
},
// An array of maps which indicates which choices are selected.
@ -94,8 +94,8 @@ func initialModel() model {
func launch(m model) tea.Cmd {
return func() tea.Msg {
var result statusMsg
for j, item := range m.choices[0] {
if _, ok := m.selected[0][j]; ok {
for j, item := range m.choices[BrowserSessions] {
if _, ok := m.selected[BrowserSessions][j]; ok {
cmd := exec.Command("qutebrowser", "--restore", item.displayString)
err := cmd.Start()
result += statusMsg(fmt.Sprintf("Launching Qutebrowser session: %v\n", item.displayString))
@ -106,9 +106,9 @@ func launch(m model) tea.Cmd {
}
}
for k, item := range m.choices[1] {
if _, ok := m.selected[1][k]; ok {
cmd := exec.Command("xterm", "-e", item.commandString)
for k, item := range m.choices[Commands] {
if _, ok := m.selected[Commands][k]; ok {
cmd := item.command
err := cmd.Start()
result += statusMsg(fmt.Sprintf("Launching command: %v\n", item.displayString))
if err != nil {