feat(main): Running a cmd & seeing output!!!!!

This commit is contained in:
punkfairie 2024-09-07 22:30:47 -07:00
parent c156ff321b
commit 43f5c55456
3 changed files with 116 additions and 17 deletions

48
install.go Normal file
View file

@ -0,0 +1,48 @@
package main
import (
"bufio"
"io"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
)
type cmdMsg string
type cmdDoneMsg struct{}
func installPackage(sub chan string) tea.Cmd {
return func() tea.Msg {
cmd := exec.Command("./test.sh")
out, err := cmd.StdoutPipe()
if err != nil {
return errMsg{err}
}
if err := cmd.Start(); err != nil {
return errMsg{err}
}
buf := bufio.NewReader(out)
for {
line, _, err := buf.ReadLine()
if err == io.EOF {
return cmdDoneMsg{}
}
if err != nil {
return errMsg{err}
}
sub <- string(line)
}
}
}
func waitForCmdResponses(sub chan string) tea.Cmd {
return func() tea.Msg {
return cmdMsg(<-sub)
}
}

77
main.go
View file

@ -17,16 +17,17 @@ import (
) )
type menu struct { type menu struct {
order []string order []string
current int current int
keys keyMap keys keyMap
help help.Model help help.Model
inputStyle gloss.Style spinner spinner.Model
spinner spinner.Model quitting bool
quitting bool width int
viewport viewport.Model height int
width int sub chan string
height int output *string
viewport viewport.Model
} }
const ( const (
@ -40,15 +41,22 @@ func initialModel() menu {
s.Style = gloss.NewStyle().Foreground(gloss.Color("3")) s.Style = gloss.NewStyle().Foreground(gloss.Color("3"))
width, height, _ := term.GetSize(int(os.Stdout.Fd())) width, height, _ := term.GetSize(int(os.Stdout.Fd()))
return menu{ m := menu{
current: 150, current: 0,
keys: keys, keys: keys,
help: help.New(), help: help.New(),
spinner: s, spinner: s,
quitting: false, quitting: false,
width: width, width: width,
height: height, height: height,
sub: make(chan string),
output: new(string),
viewport: viewport.New(0, 30),
} }
m.appendOutput("Running...")
return m
} }
type keyMap struct { type keyMap struct {
@ -80,10 +88,24 @@ type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() } func (e errMsg) Error() string { return e.err.Error() }
func (m *menu) appendOutput(s string) {
*m.output += "\n" + s
m.viewport.SetContent(*m.output)
m.viewport.GotoBottom()
}
func (m menu) Init() tea.Cmd { func (m menu) Init() tea.Cmd {
return tea.Batch(getSoftwareList(softwareInstructionsFile), m.spinner.Tick) return tea.Batch(getSoftwareList(softwareInstructionsFile), m.spinner.Tick)
} }
func (m menu) setDimensions() {
m.width, m.height, _ = term.GetSize(int(os.Stdout.Fd()))
}
func calcMainWidth(width int) int {
return int(float64(width) * 0.65)
}
func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
var cmds []tea.Cmd var cmds []tea.Cmd
@ -92,6 +114,14 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case softwareListMsg: case softwareListMsg:
m.order = msg m.order = msg
cmds = append(cmds, installPackage(m.sub), waitForCmdResponses(m.sub))
case cmdMsg:
m.appendOutput(string(msg))
cmds = append(cmds, waitForCmdResponses(m.sub))
case cmdDoneMsg:
m.appendOutput("Done!!")
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
@ -105,28 +135,34 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.width, m.height, _ = term.GetSize(int(os.Stdout.Fd())) m.setDimensions()
m.viewport.Width = calcMainWidth(m.width)
case errMsg:
m.appendOutput("Error: " + msg.Error())
} }
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
} }
func (m menu) View() string { func (m menu) View() string {
mainWidth := calcMainWidth(m.width)
borderStyle := gloss.NewStyle(). borderStyle := gloss.NewStyle().
BorderStyle(gloss.RoundedBorder()). BorderStyle(gloss.RoundedBorder()).
BorderForeground(gloss.Color("5")). BorderForeground(gloss.Color("5")).
Padding(0, 1) Padding(0, 1)
mainStyle := borderStyle. mainStyle := borderStyle.
Width(int(float64(m.width) * 0.65)) Width(mainWidth)
sidebarStyle := borderStyle. sidebarStyle := borderStyle.
Width(int(float64(m.width) * 0.3)) Width(int(float64(m.width) * 0.3))
topPadding := 1 topPadding := 1
mainContent := ""
helpView := m.help.View(m.keys) helpView := m.help.View(m.keys)
softwareListEnumerator := func(l list.Items, i int) string { softwareListEnumerator := func(l list.Items, i int) string {
@ -157,6 +193,10 @@ func (m menu) View() string {
sidebarContent := software.String() sidebarContent := software.String()
m.viewport.Height = sidebarHeight
m.viewport.Width = mainWidth
mainContent := m.viewport.View()
main := mainStyle.Render(mainContent) main := mainStyle.Render(mainContent)
sidebar := sidebarStyle.Render(sidebarContent) sidebar := sidebarStyle.Render(sidebarContent)
@ -174,7 +214,10 @@ func (m menu) View() string {
} }
func main() { func main() {
p := tea.NewProgram(initialModel(), tea.WithAltScreen()) p := tea.NewProgram(
initialModel(),
tea.WithAltScreen(),
)
if _, err := p.Run(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("There's been an error: %v", err) fmt.Printf("There's been an error: %v", err)

8
test.sh Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
i=0
while [ $i -le 100 ]; do
printf "line\n"
sleep 1
done