Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster (non-blocking) update of processes #9

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -60,7 +61,9 @@ var (
MarginRight(2).
Underline(true)

docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
processes []list.Item
numOfGoroutines = 0
)

type item struct {
Expand Down Expand Up @@ -123,8 +126,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case tickMsg:
cmd := m.list.SetItems(getProcesses())
return m, tea.Batch(tickCmd(), cmd)
if numOfGoroutines == 0 {
numOfGoroutines = runtime.NumGoroutine()
} else if numOfGoroutines >= runtime.NumGoroutine() {
go func() {
processes = getProcesses()
}()
}
return m, tea.Batch(tickCmd(), m.list.SetItems(processes))

case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
Expand All @@ -149,7 +158,7 @@ func (m model) View() string {

func main() {
// Get processes running on listening ports
processes := getProcesses()
processes = getProcesses()

//Initialize the model
m := model{
Expand All @@ -173,7 +182,7 @@ 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 tea.Tick(time.Millisecond*600, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
Expand Down