mirror of
https://github.com/charmbracelet/gum
synced 2024-06-20 06:25:08 +02:00
52 lines
969 B
Go
52 lines
969 B
Go
|
package spin
|
||
|
|
||
|
import (
|
||
|
"os/exec"
|
||
|
|
||
|
"github.com/charmbracelet/bubbles/spinner"
|
||
|
tea "github.com/charmbracelet/bubbletea"
|
||
|
)
|
||
|
|
||
|
type model struct {
|
||
|
spinner spinner.Model
|
||
|
title string
|
||
|
command []string
|
||
|
}
|
||
|
|
||
|
type finishCommandMsg struct{ output string }
|
||
|
|
||
|
func commandStart(command []string) tea.Cmd {
|
||
|
return func() tea.Msg {
|
||
|
var args []string
|
||
|
if len(command) > 1 {
|
||
|
args = command[1:]
|
||
|
}
|
||
|
out, _ := exec.Command(command[0], args...).Output()
|
||
|
return finishCommandMsg{output: string(out)}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m model) Init() tea.Cmd {
|
||
|
return tea.Batch(
|
||
|
m.spinner.Tick,
|
||
|
commandStart(m.command),
|
||
|
)
|
||
|
}
|
||
|
func (m model) View() string { return m.spinner.View() + " " + m.title }
|
||
|
|
||
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||
|
var cmd tea.Cmd
|
||
|
switch msg := msg.(type) {
|
||
|
case finishCommandMsg:
|
||
|
return m, tea.Quit
|
||
|
case tea.KeyMsg:
|
||
|
switch msg.String() {
|
||
|
case "ctrl+c":
|
||
|
return m, tea.Quit
|
||
|
}
|
||
|
}
|
||
|
|
||
|
m.spinner, cmd = m.spinner.Update(msg)
|
||
|
return m, cmd
|
||
|
}
|