github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/tables.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/aquasecurity/table"
     9  	isatty "github.com/mattn/go-isatty"
    10  )
    11  
    12  func shouldWeColorize() bool {
    13  	if csConfig.Cscli.Color == "yes" {
    14  		return true
    15  	}
    16  	if csConfig.Cscli.Color == "no" {
    17  		return false
    18  	}
    19  	return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
    20  }
    21  
    22  func newTable(out io.Writer) *table.Table {
    23  	if out == nil {
    24  		panic("newTable: out is nil")
    25  	}
    26  	t := table.New(out)
    27  	if shouldWeColorize() {
    28  		t.SetLineStyle(table.StyleBrightBlack)
    29  		t.SetHeaderStyle(table.StyleItalic)
    30  	}
    31  
    32  	if shouldWeColorize() {
    33  		t.SetDividers(table.UnicodeRoundedDividers)
    34  	} else {
    35  		t.SetDividers(table.ASCIIDividers)
    36  	}
    37  
    38  	return t
    39  }
    40  
    41  func newLightTable(out io.Writer) *table.Table {
    42  	if out == nil {
    43  		panic("newTable: out is nil")
    44  	}
    45  	t := newTable(out)
    46  	t.SetRowLines(false)
    47  	t.SetBorderLeft(false)
    48  	t.SetBorderRight(false)
    49  	// This leaves three spaces between columns:
    50  	// left padding, invisible border, right padding
    51  	// There is no way to make two spaces without
    52  	// a SetColumnLines() method, but it's close enough.
    53  	t.SetPadding(1)
    54  
    55  	if shouldWeColorize() {
    56  		t.SetDividers(table.Dividers{
    57  			ALL: "─",
    58  			NES: "─",
    59  			NSW: "─",
    60  			NEW: "─",
    61  			ESW: "─",
    62  			NE:  "─",
    63  			NW:  "─",
    64  			SW:  "─",
    65  			ES:  "─",
    66  			EW:  "─",
    67  			NS:  " ",
    68  		})
    69  	} else {
    70  		t.SetDividers(table.Dividers{
    71  			ALL: "-",
    72  			NES: "-",
    73  			NSW: "-",
    74  			NEW: "-",
    75  			ESW: "-",
    76  			NE:  "-",
    77  			NW:  "-",
    78  			SW:  "-",
    79  			ES:  "-",
    80  			EW:  "-",
    81  			NS:  " ",
    82  		})
    83  	}
    84  	return t
    85  }
    86  
    87  func renderTableTitle(out io.Writer, title string) {
    88  	if out == nil {
    89  		panic("renderTableTitle: out is nil")
    90  	}
    91  	if title == "" {
    92  		return
    93  	}
    94  	fmt.Fprintln(out, title)
    95  }