restic/cmd/restic/table.go

64 lines
1.0 KiB
Go
Raw Normal View History

2016-09-17 10:36:05 +00:00
package main
import (
"fmt"
"io"
"strings"
)
2016-09-27 18:22:01 +00:00
// Table contains data for a table to be printed.
2016-09-17 10:36:05 +00:00
type Table struct {
Header string
Rows [][]interface{}
2017-10-01 15:13:58 +00:00
Footer string
2016-09-17 10:36:05 +00:00
RowFormat string
}
2016-09-27 18:22:01 +00:00
// NewTable initializes a new Table.
2016-09-17 10:36:05 +00:00
func NewTable() Table {
return Table{
Rows: [][]interface{}{},
}
}
2017-10-01 15:13:58 +00:00
func (t Table) printSeparationLine(w io.Writer) error {
_, err := fmt.Fprintln(w, strings.Repeat("-", 70))
return err
}
2016-09-27 18:22:01 +00:00
// Write prints the table to w.
2016-09-17 10:36:05 +00:00
func (t Table) Write(w io.Writer) error {
_, err := fmt.Fprintln(w, t.Header)
if err != nil {
return err
}
2017-10-01 15:13:58 +00:00
err = t.printSeparationLine(w)
2016-09-17 10:36:05 +00:00
if err != nil {
return err
}
for _, row := range t.Rows {
_, err = fmt.Fprintf(w, t.RowFormat+"\n", row...)
if err != nil {
return err
}
}
2017-10-01 15:13:58 +00:00
err = t.printSeparationLine(w)
if err != nil {
return err
}
_, err = fmt.Fprintln(w, t.Footer)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
return nil
}
2016-09-27 18:22:01 +00:00
// TimeFormat is the format used for all timestamps printed by restic.
2016-09-17 10:36:05 +00:00
const TimeFormat = "2006-01-02 15:04:05"