From 43f5c554566fd5788239a64bad484d806579ea50 Mon Sep 17 00:00:00 2001 From: punkfairie <23287005+punkfairie@users.noreply.github.com> Date: Sat, 7 Sep 2024 22:30:47 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(main):=20Running=20a=20cmd=20&?= =?UTF-8?q?=20seeing=20output!!!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.go | 48 ++++++++++++++++++++++++++++++++++ main.go | 77 ++++++++++++++++++++++++++++++++++++++++++------------ test.sh | 8 ++++++ 3 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 install.go create mode 100755 test.sh diff --git a/install.go b/install.go new file mode 100644 index 0000000..a0c80a7 --- /dev/null +++ b/install.go @@ -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) + } +} diff --git a/main.go b/main.go index c0a1892..2ee3813 100644 --- a/main.go +++ b/main.go @@ -17,16 +17,17 @@ import ( ) type menu struct { - order []string - current int - keys keyMap - help help.Model - inputStyle gloss.Style - spinner spinner.Model - quitting bool - viewport viewport.Model - width int - height int + order []string + current int + keys keyMap + help help.Model + spinner spinner.Model + quitting bool + width int + height int + sub chan string + output *string + viewport viewport.Model } const ( @@ -40,15 +41,22 @@ func initialModel() menu { s.Style = gloss.NewStyle().Foreground(gloss.Color("3")) width, height, _ := term.GetSize(int(os.Stdout.Fd())) - return menu{ - current: 150, + m := menu{ + current: 0, keys: keys, help: help.New(), spinner: s, quitting: false, width: width, height: height, + sub: make(chan string), + output: new(string), + viewport: viewport.New(0, 30), } + + m.appendOutput("Running...") + + return m } type keyMap struct { @@ -80,10 +88,24 @@ type errMsg struct{ 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 { 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) { var cmd tea.Cmd var cmds []tea.Cmd @@ -92,6 +114,14 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case softwareListMsg: 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: switch { @@ -105,28 +135,34 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, cmd) 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...) } func (m menu) View() string { + mainWidth := calcMainWidth(m.width) borderStyle := gloss.NewStyle(). BorderStyle(gloss.RoundedBorder()). BorderForeground(gloss.Color("5")). Padding(0, 1) mainStyle := borderStyle. - Width(int(float64(m.width) * 0.65)) + Width(mainWidth) sidebarStyle := borderStyle. Width(int(float64(m.width) * 0.3)) topPadding := 1 - mainContent := "" - helpView := m.help.View(m.keys) softwareListEnumerator := func(l list.Items, i int) string { @@ -157,6 +193,10 @@ func (m menu) View() string { sidebarContent := software.String() + m.viewport.Height = sidebarHeight + m.viewport.Width = mainWidth + mainContent := m.viewport.View() + main := mainStyle.Render(mainContent) sidebar := sidebarStyle.Render(sidebarContent) @@ -174,7 +214,10 @@ func (m menu) View() string { } func main() { - p := tea.NewProgram(initialModel(), tea.WithAltScreen()) + p := tea.NewProgram( + initialModel(), + tea.WithAltScreen(), + ) if _, err := p.Run(); err != nil { fmt.Printf("There's been an error: %v", err) diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..f0b59e9 --- /dev/null +++ b/test.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +i=0 + +while [ $i -le 100 ]; do + printf "line\n" + sleep 1 +done