github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/fetchutil/filesize.go (about)

     1  package fetchutil
     2  
     3  import "fmt"
     4  
     5  type FileSize int64
     6  
     7  func (f FileSize) String() string {
     8  	b := int64(f)
     9  	const unit = 1024
    10  	if b < unit {
    11  		return fmt.Sprintf("%d B", b)
    12  	}
    13  	div, exp := int64(unit), 0
    14  	for n := b / unit; n >= unit; n /= unit {
    15  		div *= unit
    16  		exp++
    17  	}
    18  	return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
    19  }