github.com/mattevans/edward@v1.9.2/output/rendering_inprogress.go (about)

     1  package output
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/fatih/color"
     8  	"github.com/pkg/errors"
     9  	"github.com/mattevans/edward/tracker"
    10  )
    11  
    12  type InProgressRenderer struct {
    13  	indent     string
    14  	minSpacing int
    15  }
    16  
    17  func NewInProgressRenderer() *InProgressRenderer {
    18  	return &InProgressRenderer{
    19  		indent:     "  ",
    20  		minSpacing: 3,
    21  	}
    22  }
    23  
    24  func (r *InProgressRenderer) Render(w io.Writer, task tracker.Task) error {
    25  	task = task.Lineage()[0]
    26  	return errors.WithStack(r.doRenderWithPrefix("", 0, w, task))
    27  }
    28  
    29  func (r *InProgressRenderer) doRenderWithPrefix(prefix string, maxNameWidth int, w io.Writer, task tracker.Task) error {
    30  	newMax := r.getLongestName(w, prefix, task)
    31  	if newMax > maxNameWidth {
    32  		maxNameWidth = newMax
    33  	}
    34  
    35  	children := task.Children()
    36  	for _, child := range children {
    37  		r.doRenderWithPrefix(extendPrefix(prefix, child), maxNameWidth, w, child)
    38  	}
    39  
    40  	if len(children) > 0 || task.State() != tracker.TaskStateInProgress {
    41  		return nil
    42  	}
    43  
    44  	ts := task.State()
    45  	// Print name
    46  	nameFormat := fmt.Sprintf("%%-%ds", maxNameWidth+r.minSpacing)
    47  	fmt.Fprintf(w, nameFormat, fmt.Sprintf("%v:", prefix))
    48  
    49  	tmpOutput := color.Output
    50  	defer func() {
    51  		color.Output = tmpOutput
    52  	}()
    53  	color.Output = w
    54  	fmt.Fprint(w, "[")
    55  	switch ts {
    56  	case tracker.TaskStateSuccess:
    57  		color.Set(color.FgGreen)
    58  		fmt.Fprint(w, "OK")
    59  	case tracker.TaskStateFailed:
    60  		color.Set(color.FgRed)
    61  		fmt.Fprint(w, "Failed")
    62  	case tracker.TaskStateWarning:
    63  		color.Set(color.FgYellow)
    64  		fmt.Fprint(w, "Warning")
    65  	case tracker.TaskStatePending:
    66  		color.Set(color.FgCyan)
    67  		fmt.Fprint(w, "Pending")
    68  	default:
    69  		color.Set(color.FgCyan)
    70  		fmt.Fprint(w, "In Progress")
    71  	}
    72  	color.Unset()
    73  	fmt.Fprint(w, "]")
    74  	fmt.Fprintln(w)
    75  
    76  	output := task.Output()
    77  	if len(output) > 4 {
    78  		output = output[len(output)-4:]
    79  	}
    80  
    81  	if len(output) > 0 {
    82  		for _, line := range output {
    83  			if len(line) > 80 {
    84  				line = line[:80]
    85  			}
    86  			fmt.Fprintln(w, line)
    87  		}
    88  	}
    89  	return nil
    90  }
    91  
    92  func (r *InProgressRenderer) getLongestName(w io.Writer, prefix string, task tracker.Task) int {
    93  	children := task.Children()
    94  	var max = len(prefix)
    95  	for _, child := range children {
    96  		childMax := r.getLongestName(w, extendPrefix(prefix, child), child)
    97  		if childMax > max {
    98  			max = childMax
    99  		}
   100  	}
   101  	return max
   102  }