github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/pretty-record.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "fmt" 22 23 "github.com/minio/pkg/v2/console" 24 ) 25 26 // Row specifies row description and theme 27 type Row struct { 28 desc string 29 descTheme string 30 } 31 32 // PrettyRecord - an easy struct to format a set of key-value 33 // pairs into a record 34 type PrettyRecord struct { 35 rows []Row 36 indent int 37 maxLen int 38 } 39 40 // newPrettyRecord - creates a new pretty record 41 func newPrettyRecord(indent int, rows ...Row) PrettyRecord { 42 maxDescLen := 0 43 for _, r := range rows { 44 if len(r.desc) > maxDescLen { 45 maxDescLen = len(r.desc) 46 } 47 } 48 return PrettyRecord{ 49 rows: rows, 50 indent: indent, 51 maxLen: maxDescLen, 52 } 53 } 54 55 // buildRecord - creates a string which represents a record table given 56 // some fields contents. 57 func (t PrettyRecord) buildRecord(contents ...string) (line string) { 58 // totalRows is the minimum of the number of fields config 59 // and the number of contents elements. 60 totalRows := len(contents) 61 if len(t.rows) < totalRows { 62 totalRows = len(t.rows) 63 } 64 var format, separator string 65 // Format fields and construct message 66 for i := 0; i < totalRows; i++ { 67 // default heading 68 indent := 0 69 format = "%s\n" 70 // optionally indented rows with key value pairs 71 if i > 0 { 72 indent = t.indent 73 format = fmt.Sprintf("%%%ds%%-%ds : %%s\n", indent, t.maxLen) 74 line += console.Colorize(t.rows[i].descTheme, fmt.Sprintf(format, separator, t.rows[i].desc, contents[i])) 75 } else { 76 line += console.Colorize(t.rows[i].descTheme, fmt.Sprintf(format, contents[i])) 77 } 78 } 79 return 80 }