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