github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/pretty-table.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  // Field configuration: color theme and max content length
    27  type Field struct {
    28  	colorTheme string
    29  	maxLen     int
    30  }
    31  
    32  // PrettyTable - an easy struct to format a set of line
    33  type PrettyTable struct {
    34  	cols      []Field
    35  	separator string
    36  }
    37  
    38  // newPrettyTable - creates a new pretty table
    39  func newPrettyTable(separator string, cols ...Field) PrettyTable {
    40  	return PrettyTable{
    41  		cols:      cols,
    42  		separator: separator,
    43  	}
    44  }
    45  
    46  // buildRow - creates a string which represents a line table given
    47  // some fields contents.
    48  func (t PrettyTable) buildRow(contents ...string) (line string) {
    49  	dots := "..."
    50  
    51  	// totalColumns is the minimum of the number of fields config
    52  	// and the number of contents elements.
    53  	totalColumns := len(contents)
    54  	if len(t.cols) < totalColumns {
    55  		totalColumns = len(t.cols)
    56  	}
    57  
    58  	// Format fields and construct message
    59  	for i := 0; i < totalColumns; i++ {
    60  		// Default field format without pretty effect
    61  		fieldContent := ""
    62  		fieldFormat := "%s"
    63  		if t.cols[i].maxLen >= 0 {
    64  			// Override field format
    65  			fieldFormat = fmt.Sprintf("%%-%d.%ds", t.cols[i].maxLen, t.cols[i].maxLen)
    66  			// Cut field string and add '...' if length is greater than maxLen
    67  			if len(contents[i]) > t.cols[i].maxLen {
    68  				fieldContent = contents[i][:t.cols[i].maxLen-len(dots)] + dots
    69  			} else {
    70  				fieldContent = contents[i]
    71  			}
    72  		} else {
    73  			fieldContent = contents[i]
    74  		}
    75  
    76  		// Add separator if this is not the last column
    77  		if i < totalColumns-1 {
    78  			fieldFormat += t.separator
    79  		}
    80  
    81  		// Add the field to the resulted message
    82  		line += console.Colorize(t.cols[i].colorTheme, fmt.Sprintf(fieldFormat, fieldContent))
    83  	}
    84  	return
    85  }