@ -1,4 +1,4 @@
// Copyright 20 18 The Gitea Authors. All rights reserved.
// Copyright 20 20 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
@ -7,19 +7,67 @@ package print
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
var (
showLog bool
)
// table provides infrastructure to easily print (sorted) lists in different formats
type table struct {
headers [ ] string
values [ ] [ ] string
sortDesc bool // used internally by sortable interface
sortColumn uint // ↑
}
func tableWithHeader ( header ... string ) table {
return table { headers : header }
}
// it's the callers responsibility to ensure row length is equal to header length!
func ( t * table ) addRow ( row ... string ) {
t . addRowSlice ( row )
}
// it's the callers responsibility to ensure row length is equal to header length!
func ( t * table ) addRowSlice ( row [ ] string ) {
t . values = append ( t . values , row )
}
// errorf printf content as an error information
func errorf ( format string , a ... interface { } ) {
fmt . Printf ( format , a ... )
func ( t * table ) sort ( column uint , desc bool ) {
t . sortColumn = column
t . sortDesc = desc
sort . Stable ( t ) // stable to allow multiple calls to sort
}
// sortable interface
func ( t table ) Len ( ) int { return len ( t . values ) }
func ( t table ) Swap ( i , j int ) { t . values [ i ] , t . values [ j ] = t . values [ j ] , t . values [ i ] }
func ( t table ) Less ( i , j int ) bool {
const column = 0
if t . sortDesc {
i , j = j , i
}
return t . values [ i ] [ t . sortColumn ] < t . values [ j ] [ t . sortColumn ]
}
func ( t * table ) print ( output string ) {
switch {
case output == "" || output == "table" :
outputtable ( t . headers , t . values )
case output == "csv" :
outputdsv ( t . headers , t . values , "," )
case output == "simple" :
outputsimple ( t . headers , t . values )
case output == "tsv" :
outputdsv ( t . headers , t . values , "\t" )
case output == "yaml" :
outputyaml ( t . headers , t . values )
default :
fmt . Printf ( "unknown output type '" + output + "', available types are:\n- csv: comma-separated values\n- simple: space-separated values\n- table: auto-aligned table format (default)\n- tsv: tab-separated values\n- yaml: YAML format\n" )
}
}
// outputtable prints structured data as table
@ -71,22 +119,3 @@ func outputyaml(headers []string, values [][]string) {
}
}
}
// outputList provides general function to convert given list of items
// into several outputs (table, csv, simple, tsv, yaml)
func outputList ( output string , headers [ ] string , values [ ] [ ] string ) {
switch {
case output == "" || output == "table" :
outputtable ( headers , values )
case output == "csv" :
outputdsv ( headers , values , "," )
case output == "simple" :
outputsimple ( headers , values )
case output == "tsv" :
outputdsv ( headers , values , "\t" )
case output == "yaml" :
outputyaml ( headers , values )
default :
errorf ( "unknown output type '" + output + "', available types are:\n- csv: comma-separated values\n- simple: space-separated values\n- table: auto-aligned table format (default)\n- tsv: tab-separated values\n- yaml: YAML format\n" )
}
}