From c293ad0e573280b0401766e177ecccda3338bdda Mon Sep 17 00:00:00 2001 From: Casual Date: Sat, 11 May 2024 04:53:43 +0300 Subject: [PATCH] add running command indicator --- tui.go | 91 +++++++++++++++++++++++++++++++++++++++----------- tui_choices.go | 4 +++ 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/tui.go b/tui.go index 83f9078..18cc252 100644 --- a/tui.go +++ b/tui.go @@ -10,6 +10,7 @@ import ( // "strings" // "time" + // "log" "github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/viewport" @@ -74,11 +75,33 @@ func (m *model) Init() tea.Cmd { return nil } +type continueExec string + +var fileUpdatedBool bool +var choiceActionBool bool + func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - // var ( - // // cmd tea.Cmd - // cmds []tea.Cmd - // ) + var ( + cmd tea.Cmd + // cmds []tea.Cmd + // continueExec string + + ) + + if fileUpdatedBool { + m.viewport.SetContent(watchRun()) + m.viewport.GotoBottom() + fileUpdatedBool = false + } + if choiceActionBool { + i, ok := m.list.SelectedItem().(item) + if ok { + m.viewport.SetContent(choiceAction(string(i))) + m.viewport.GotoBottom() + } + fileUpdatedBool = false + choiceActionBool = false + } switch msg := msg.(type) { @@ -106,15 +129,18 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // The "enter" key and the spacebar (a literal space) toggle // the selected state for the item that the cursor is pointing at. case "enter", " ": - i, ok := m.list.SelectedItem().(item) - if ok { - // m.choice = string(i) - skipNextViewportUpdate = true - m.viewport.SetContent("[Running...]") - m.viewport.SetContent(choiceAction(string(i))) - m.viewport.GotoBottom() + skipNextViewportUpdate = true + m.viewport.SetContent("[Running...]") //TODO not working, probably need do in another update, so need to mkae it set here and return msg + // continueExecVar = "choiceAction" + // TUI.Send(func() tea.Msg{return continueExecVar}) + // continueExecVar = "" + go TUI.Send(func() tea.Msg { // foroce rerender probably there are better solutions + // time.Sleep(time.Second / 10) + return "" + }) + choiceActionBool = true + return m, nil - } } case tea.WindowSizeMsg: headerHeight := lipgloss.Height(m.headerView()) @@ -154,18 +180,43 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // } case fileUpdated: - if !skipNextViewportUpdate { - m.viewport.SetContent("[Running...]") - m.viewport.SetContent(watchRun()) - m.viewport.GotoBottom() - return m, nil - } - skipNextViewportUpdate = false + // if !skipNextViewportUpdate { //TODO useless + m.viewport.SetContent("[Running...]") + + go TUI.Send(func() tea.Msg { //probably there are better solutions + // time.Sleep(time.Second / 10) + return "" + }) + fileUpdatedBool = true + // time.Sleep(time.Second*2) + + // continueExecVar = "" + // return m, nil + // } + // skipNextViewportUpdate = false //TODO useless + + // case continueExec: + // switch msg{ + // case "choiceAction": + // i, ok := m.list.SelectedItem().(item) + // if ok { + // m.viewport.SetContent(choiceAction(string(i))) + // m.viewport.GotoBottom() + // } + // // return m, nil + // case "watchRun": + // // log.Println("tets") + // m.viewport.SetContent(watchRun()) + // m.viewport.GotoBottom() + // // return m, nil + // + // } } + // Return the updated model to the Bubble Tea runtime for processing. // Note that we're not returning a command. - var cmd tea.Cmd + // var cmd tea.Cmd m.list, cmd = m.list.Update(msg) return m, cmd // return m, nil diff --git a/tui_choices.go b/tui_choices.go index 4772f12..73f1509 100644 --- a/tui_choices.go +++ b/tui_choices.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os/exec" + // "os" "strings" "github.com/charmbracelet/bubbles/list" @@ -108,6 +109,9 @@ func choiceAction(input string) string { case "git commit": // TODO add input message box case "git push": + // cmd := exec.Command("git","push") + // cmd.Env = os.Environ() // for using ssh keys + // output, err := cmd.CombinedOutput() output, err := exec.Command("git", "push").CombinedOutput() if err != nil { return "[ERROR] - executing command failed\n" + string(output)