code-intelligence.com/cifuzz@v0.40.0/internal/cmd/run/reporthandler/metrics/line_printer.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/pterm/pterm"
    10  	"golang.org/x/term"
    11  
    12  	"code-intelligence.com/cifuzz/pkg/report"
    13  )
    14  
    15  func NewLinePrinter(output io.Writer) *LinePrinter {
    16  	p := pterm.DefaultBasicText.WithWriter(output)
    17  	return &LinePrinter{
    18  		BasicTextPrinter: p,
    19  		startedAt:        time.Now(),
    20  	}
    21  }
    22  
    23  type LinePrinter struct {
    24  	*pterm.BasicTextPrinter
    25  	startedAt time.Time
    26  }
    27  
    28  func (p *LinePrinter) PrintMetrics(metrics *report.FuzzingMetric) {
    29  	s := fmt.Sprint(
    30  		MetricsToString(metrics),
    31  		DelimString(" ("),
    32  		pterm.LightYellow(time.Since(p.startedAt).Round(time.Second).String()),
    33  		DelimString(")"),
    34  	)
    35  	if len(s) == 0 || s[len(s)-1] != '\n' {
    36  		s += "\n"
    37  	}
    38  	// Print without color if the output is not a TTY
    39  	if file, ok := p.BasicTextPrinter.Writer.(*os.File); !ok || !term.IsTerminal(int(file.Fd())) {
    40  		s = pterm.RemoveColorFromString(s)
    41  	}
    42  	p.Print(s)
    43  }