Skip to content

Commit

Permalink
feat(paraprogress): Add WithTimeout option and add ellipsis (#962)
Browse files Browse the repository at this point in the history
Reviewed-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
Approved-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed Nov 1, 2023
2 parents 3a23f90 + 9ec2dac commit 99cb86e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tui/processtree/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// You may not use this file expect in compliance with the License.
package processtree

import "time"

type ProcessTreeOption func(pt *ProcessTree) error

func WithVerb(verb string) ProcessTreeOption {
Expand Down Expand Up @@ -40,3 +42,10 @@ func WithHideOnSuccess(hide bool) ProcessTreeOption {
return nil
}
}

func WithTimeout(timeout time.Duration) ProcessTreeOption {
return func(pt *ProcessTree) error {
pt.timeout = timeout
return nil
}
}
7 changes: 7 additions & 0 deletions tui/processtree/processtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type ProcessTreeItem struct {
timer stopwatch.Model
norender bool
ctx context.Context
timeout time.Duration
err error
ellipsis string
}

type ProcessTree struct {
Expand All @@ -77,6 +80,7 @@ type ProcessTree struct {
failFast bool
oldOut iostreams.FileWriter
hide bool
timeout time.Duration
}

func NewProcessTree(ctx context.Context, opts []ProcessTreeOption, tree ...*ProcessTreeItem) (*ProcessTree, error) {
Expand Down Expand Up @@ -105,6 +109,7 @@ func NewProcessTree(ctx context.Context, opts []ProcessTreeOption, tree ...*Proc
_ = pt.traverseTreeAndCall(tree, func(item *ProcessTreeItem) error {
total++
item.norender = pt.norender
item.timeout = pt.timeout
return nil
})

Expand Down Expand Up @@ -189,6 +194,8 @@ func (pt *ProcessTree) Init() tea.Cmd {
children := pt.getNextReadyChildren(pt.tree)
for _, pti := range children {
pti := pti
pti.timeout = pt.timeout

cmds = append(cmds, pt.waitForProcessCmd(pti))
cmds = append(cmds, pti.timer.Init())
}
Expand Down
17 changes: 16 additions & 1 deletion tui/processtree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package processtree

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -42,7 +43,21 @@ func (pt *ProcessTree) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case spinner.TickMsg:
_ = pt.traverseTreeAndCall(pt.tree, func(pti *ProcessTreeItem) error {
pti.spinner, cmd = pti.spinner.Update(msg)
if pti.timeout != 0 && pti.timer.Elapsed() > pti.timeout {
pti.err = fmt.Errorf("process timedout after %s", pti.timeout.String())
pti.status = StatusFailed
} else {
pti.spinner, cmd = pti.spinner.Update(msg)
}

if pti.status == StatusRunning ||
pti.status == StatusRunningChild ||
pti.status == StatusRunningButAChildHasFailed {
pti.ellipsis = strings.Repeat(".", int(pti.timer.Elapsed().Seconds())%4)
} else {
pti.ellipsis = ""
}

cmds = append(cmds, cmd)

return nil
Expand Down
4 changes: 4 additions & 0 deletions tui/processtree/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (stm ProcessTree) printItem(pti *ProcessTreeItem, offset uint) string {

textLeft += " " + pti.textLeft

if pti.status == StatusRunning || pti.status == StatusRunningChild {
textLeft += pti.ellipsis
}

elapsed := utils.HumanizeDuration(pti.timer.Elapsed())
rightTimerWidth := width(elapsed)
if rightTimerWidth > stm.rightPad {
Expand Down

0 comments on commit 99cb86e

Please sign in to comment.