github.com/mckael/restic@v0.8.3/cmd/restic/table.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  )
     8  
     9  // Table contains data for a table to be printed.
    10  type Table struct {
    11  	Header string
    12  	Rows   [][]interface{}
    13  	Footer string
    14  
    15  	RowFormat string
    16  }
    17  
    18  // NewTable initializes a new Table.
    19  func NewTable() Table {
    20  	return Table{
    21  		Rows: [][]interface{}{},
    22  	}
    23  }
    24  
    25  func (t Table) printSeparationLine(w io.Writer) error {
    26  	_, err := fmt.Fprintln(w, strings.Repeat("-", 70))
    27  	return err
    28  }
    29  
    30  // Write prints the table to w.
    31  func (t Table) Write(w io.Writer) error {
    32  	_, err := fmt.Fprintln(w, t.Header)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	err = t.printSeparationLine(w)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	for _, row := range t.Rows {
    43  		_, err = fmt.Fprintf(w, t.RowFormat+"\n", row...)
    44  		if err != nil {
    45  			return err
    46  		}
    47  	}
    48  
    49  	err = t.printSeparationLine(w)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	_, err = fmt.Fprintln(w, t.Footer)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  // TimeFormat is the format used for all timestamps printed by restic.
    63  const TimeFormat = "2006-01-02 15:04:05"