github.com/ldez/golangci-lint@v1.10.1/pkg/printers/tab.go (about)

     1  package printers
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"text/tabwriter"
     8  
     9  	"github.com/fatih/color"
    10  	"github.com/golangci/golangci-lint/pkg/logutils"
    11  	"github.com/golangci/golangci-lint/pkg/result"
    12  )
    13  
    14  type Tab struct {
    15  	printLinterName bool
    16  	log             logutils.Log
    17  }
    18  
    19  func NewTab(printLinterName bool, log logutils.Log) *Tab {
    20  	return &Tab{
    21  		printLinterName: printLinterName,
    22  		log:             log,
    23  	}
    24  }
    25  
    26  func (p Tab) SprintfColored(ca color.Attribute, format string, args ...interface{}) string {
    27  	c := color.New(ca)
    28  	return c.Sprintf(format, args...)
    29  }
    30  
    31  func (p *Tab) Print(ctx context.Context, issues <-chan result.Issue) error {
    32  	w := tabwriter.NewWriter(logutils.StdOut, 0, 0, 2, ' ', 0)
    33  
    34  	for i := range issues {
    35  		p.printIssue(&i, w)
    36  	}
    37  
    38  	if err := w.Flush(); err != nil {
    39  		p.log.Warnf("Can't flush tab writer: %s", err)
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  func (p Tab) printIssue(i *result.Issue, w io.Writer) {
    46  	text := p.SprintfColored(color.FgRed, "%s", i.Text)
    47  	if p.printLinterName {
    48  		text = fmt.Sprintf("%s\t%s", i.FromLinter, text)
    49  	}
    50  
    51  	pos := p.SprintfColored(color.Bold, "%s:%d", i.FilePath(), i.Line())
    52  	if i.Pos.Column != 0 {
    53  		pos += fmt.Sprintf(":%d", i.Pos.Column)
    54  	}
    55  
    56  	fmt.Fprintf(w, "%s\t%s\n", pos, text)
    57  }