Skip to content

Commit

Permalink
add refresh for processes
Browse files Browse the repository at this point in the history
  • Loading branch information
savannahostrowski committed Feb 27, 2023
1 parent 1e3999a commit b44c45b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -80,9 +81,11 @@ type model struct {

var doc = strings.Builder{}

type tickMsg time.Time

func (m model) Init() tea.Cmd {
renderTitle()
return nil
return tickCmd()
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down Expand Up @@ -120,6 +123,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.activeButton = "yes"
}

case tickMsg:
cmd := m.list.SetItems(getProcesses())
return m, tea.Batch(tickCmd(), cmd)

case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
Expand Down Expand Up @@ -165,6 +172,13 @@ func main() {
}
}

// Used to refresh the running processes on listening ports in the list view
func tickCmd() tea.Cmd {
return tea.Tick(time.Second*1, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}

func getProcesses() []list.Item {
out, err := exec.Command("lsof", "-i", "-P", "-n", "-sTCP:LISTEN").Output()
strStdout := string(out)
Expand Down Expand Up @@ -202,7 +216,6 @@ func killPort(pid string) {
if err != nil {
log.Error("Could not kill process")
}

}

func confirmationView(m model) string {
Expand Down

0 comments on commit b44c45b

Please sign in to comment.