...

Source file src/github.com/jedib0t/go-pretty/v6/progress/units.go

Documentation: github.com/jedib0t/go-pretty/v6/progress

     1  package progress
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // UnitsNotationPosition determines notation position relative to unit value.
     8  type UnitsNotationPosition int
     9  
    10  // Supported unit positions relative to tracker value;
    11  // default: UnitsNotationPositionBefore
    12  const (
    13  	UnitsNotationPositionBefore UnitsNotationPosition = iota
    14  	UnitsNotationPositionAfter
    15  )
    16  
    17  // UnitsFormatter defines a function that prints a value in a specific style.
    18  type UnitsFormatter func(value int64) string
    19  
    20  // Units defines the "type" of the value being tracked by the Tracker.
    21  type Units struct {
    22  	Formatter        UnitsFormatter // default: FormatNumber
    23  	Notation         string
    24  	NotationPosition UnitsNotationPosition // default: UnitsNotationPositionBefore
    25  }
    26  
    27  // Sprint prints the value as defined by the Units.
    28  func (tu Units) Sprint(value int64) string {
    29  	formatter := tu.Formatter
    30  	if formatter == nil {
    31  		formatter = FormatNumber
    32  	}
    33  
    34  	formattedValue := formatter(value)
    35  	switch tu.NotationPosition {
    36  	case UnitsNotationPositionAfter:
    37  		return formattedValue + tu.Notation
    38  	default: // UnitsNotationPositionBefore
    39  		return tu.Notation + formattedValue
    40  	}
    41  }
    42  
    43  var (
    44  	// UnitsDefault doesn't define any units. The value will be treated as any
    45  	// other number.
    46  	UnitsDefault = Units{
    47  		Notation:         "",
    48  		NotationPosition: UnitsNotationPositionBefore,
    49  		Formatter:        FormatNumber,
    50  	}
    51  
    52  	// UnitsBytes defines the value as a storage unit. Values will be converted
    53  	// and printed in one of these forms: B, KB, MB, GB, TB, PB
    54  	UnitsBytes = Units{
    55  		Notation:         "",
    56  		NotationPosition: UnitsNotationPositionBefore,
    57  		Formatter:        FormatBytes,
    58  	}
    59  
    60  	// UnitsCurrencyDollar defines the value as a Dollar amount. Values will be
    61  	// converted and printed in one of these forms: $x.yz, $x.yzK, $x.yzM,
    62  	// $x.yzB, $x.yzT
    63  	UnitsCurrencyDollar = Units{
    64  		Notation:         "$",
    65  		NotationPosition: UnitsNotationPositionBefore,
    66  		Formatter:        FormatNumber,
    67  	}
    68  
    69  	// UnitsCurrencyEuro defines the value as a Euro amount. Values will be
    70  	// converted and printed in one of these forms: ₠x.yz, ₠x.yzK, ₠x.yzM,
    71  	// ₠x.yzB, ₠x.yzT
    72  	UnitsCurrencyEuro = Units{
    73  		Notation:         "₠",
    74  		NotationPosition: UnitsNotationPositionBefore,
    75  		Formatter:        FormatNumber,
    76  	}
    77  
    78  	// UnitsCurrencyPound defines the value as a Pound amount. Values will be
    79  	// converted and printed in one of these forms: £x.yz, £x.yzK, £x.yzM,
    80  	// £x.yzB, £x.yzT
    81  	UnitsCurrencyPound = Units{
    82  		Notation:         "£",
    83  		NotationPosition: UnitsNotationPositionBefore,
    84  		Formatter:        FormatNumber,
    85  	}
    86  )
    87  
    88  // FormatBytes formats the given value as a "Byte".
    89  func FormatBytes(value int64) string {
    90  	return formatNumber(value, map[int64]string{
    91  		1000000000000000: "PB",
    92  		1000000000000:    "TB",
    93  		1000000000:       "GB",
    94  		1000000:          "MB",
    95  		1000:             "KB",
    96  		0:                "B",
    97  	})
    98  }
    99  
   100  // FormatNumber formats the given value as a "regular number".
   101  func FormatNumber(value int64) string {
   102  	return formatNumber(value, map[int64]string{
   103  		1000000000000000: "Q",
   104  		1000000000000:    "T",
   105  		1000000000:       "B",
   106  		1000000:          "M",
   107  		1000:             "K",
   108  		0:                "",
   109  	})
   110  }
   111  
   112  var (
   113  	unitScales = []int64{
   114  		1000000000000000,
   115  		1000000000000,
   116  		1000000000,
   117  		1000000,
   118  		1000,
   119  	}
   120  )
   121  
   122  func formatNumber(value int64, notations map[int64]string) string {
   123  	for _, unitScale := range unitScales {
   124  		if value >= unitScale {
   125  			return fmt.Sprintf("%.2f%s", float64(value)/float64(unitScale), notations[unitScale])
   126  		}
   127  	}
   128  	return fmt.Sprintf("%d%s", value, notations[0])
   129  }
   130  

View as plain text