Skip to content

Commit

Permalink
feat(paraprogress): Introduce WithTimeout option (#961)
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 edb2ffd + 91c6401 commit 3a23f90
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tui/paraprogress/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// You may not use this file except in compliance with the License.
package paraprogress

import "time"

type ParaProgressOption func(md *ParaProgress) error

func WithRenderer(norender bool) ParaProgressOption {
Expand Down Expand Up @@ -33,3 +35,10 @@ func WithNameWidth(width int) ParaProgressOption {
return nil
}
}

func WithTimeout(timeout time.Duration) ParaProgressOption {
return func(pp *ParaProgress) error {
pp.timeout = timeout
return nil
}
}
3 changes: 3 additions & 0 deletions tui/paraprogress/paraprogress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/barkimedes/go-deepcopy"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -34,6 +35,7 @@ type ParaProgress struct {
errChan chan error
failFast bool
nameWidth int
timeout time.Duration
}

func NewParaProgress(ctx context.Context, processes []*Process, opts ...ParaProgressOption) (*ParaProgress, error) {
Expand Down Expand Up @@ -71,6 +73,7 @@ func NewParaProgress(ctx context.Context, processes []*Process, opts ...ParaProg
for i := range processes {
processes[i].norender = md.norender
processes[i].NameWidth = maxNameLen
processes[i].timeout = md.timeout

pctx, err := deepcopy.Anything(ctx)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion tui/paraprogress/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package paraprogress

import (
"context"
"fmt"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -78,6 +79,7 @@ type Process struct {
err error
norender bool
ctx context.Context
timeout time.Duration

Name string
NameWidth int
Expand Down Expand Up @@ -213,6 +215,11 @@ func (d *Process) Update(msg tea.Msg) (*Process, tea.Cmd) {

// TickMsg is sent when the spinner wants to animate itself
case spinner.TickMsg:
if d.timeout != 0 && d.timer.Elapsed() > d.timeout {
d.err = fmt.Errorf("process timedout after %s", d.timeout.String())
d.Status = StatusFailed
}

d.spinner, cmd = d.spinner.Update(msg)
cmds = append(cmds, cmd)

Expand Down Expand Up @@ -291,7 +298,7 @@ func (p Process) View() string {
truncate := 0
loglen := len(p.logs) - LOGLEN

if loglen > 0 {
if p.Status != StatusFailed {
truncate = loglen
}

Expand Down

0 comments on commit 3a23f90

Please sign in to comment.