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