github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/pretty/pretty.go (about)

     1  package pretty
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/docker/go-units"
     9  	"github.com/fatih/color"
    10  	"github.com/gogo/protobuf/types"
    11  )
    12  
    13  // UnescapeHTML returns s with < and > unescaped.
    14  func UnescapeHTML(s string) string {
    15  	s = strings.Replace(s, "\\u003c", "<", -1)
    16  	s = strings.Replace(s, "\\u003e", ">", -1)
    17  	return s
    18  }
    19  
    20  // Since pretty-prints the amount of time that has passed since timestamp as a
    21  // human-readable string.
    22  func Since(timestamp *types.Timestamp) string {
    23  	t, _ := types.TimestampFromProto(timestamp)
    24  	if t.Equal(time.Time{}) {
    25  		return ""
    26  	}
    27  	return units.HumanDuration(time.Since(t))
    28  }
    29  
    30  // Ago pretty-prints the amount of time that has passed since timestamp as a
    31  // human-readable string, and adds "ago" to the end.
    32  func Ago(timestamp *types.Timestamp) string {
    33  	since := Since(timestamp)
    34  	if since == "" {
    35  		return since
    36  	}
    37  	return fmt.Sprintf("%s ago", since)
    38  }
    39  
    40  // TimeDifference pretty-prints the duration of time between from
    41  // and to as a human-reabable string.
    42  func TimeDifference(from *types.Timestamp, to *types.Timestamp) string {
    43  	tFrom, _ := types.TimestampFromProto(from)
    44  	tTo, _ := types.TimestampFromProto(to)
    45  	return units.HumanDuration(tTo.Sub(tFrom))
    46  }
    47  
    48  // Duration pretty prints a duration in a human readable way.
    49  func Duration(d *types.Duration) string {
    50  	duration, _ := types.DurationFromProto(d)
    51  	return units.HumanDuration(duration)
    52  }
    53  
    54  // Size pretty-prints size amount of bytes as a human readable string.
    55  func Size(size uint64) string {
    56  	return units.BytesSize(float64(size))
    57  }
    58  
    59  // ProgressBar pretty prints a progress bar with given width and green, yellow
    60  // and red segments.  green, yellow and red need not add to width, they will be
    61  // normalized. If red is nonzero there will always be at least one red segment,
    62  // even if red is less than 1/width of the total bar.
    63  func ProgressBar(width, green, yellow, red int) string {
    64  	total := green + yellow + red
    65  	var sb strings.Builder
    66  	for i := 0; i < width; i++ {
    67  		switch {
    68  		case i == width-1 && red != 0:
    69  			// if there is nonzero red then the final segment is always red,
    70  			// this ensures that we don't present something as totally
    71  			// successful when it wasn't
    72  			sb.WriteString(color.RedString("▇"))
    73  		case i*total < green*width:
    74  			sb.WriteString(color.GreenString("▇"))
    75  		case i*total < (green+yellow)*width:
    76  			sb.WriteString(color.YellowString("▇"))
    77  		case i*total < (green+yellow+red)*width:
    78  			sb.WriteString(color.RedString("▇"))
    79  		default:
    80  			sb.WriteString(" ")
    81  		}
    82  	}
    83  	return sb.String()
    84  }