github.com/chain5j/chain5j-pkg@v1.0.7/types/storagesize.go (about)

     1  // Package types
     2  //
     3  // @author: xwc1125
     4  package types
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  // StorageSize is a wrapper around a float value that supports user friendly
    11  // formatting.
    12  type StorageSize float64
    13  
    14  // String implements the stringer interface.
    15  func (s StorageSize) String() string {
    16  	if s > 1000000 {
    17  		return fmt.Sprintf("%.2f mB", s/1000000)
    18  	} else if s > 1000 {
    19  		return fmt.Sprintf("%.2f kB", s/1000)
    20  	} else {
    21  		return fmt.Sprintf("%.2f B", s)
    22  	}
    23  }
    24  
    25  // TerminalString implements log.TerminalStringer, formatting a string for console
    26  // output during logging.
    27  func (s StorageSize) TerminalString() string {
    28  	if s > 1000000 {
    29  		return fmt.Sprintf("%.2fmB", s/1000000)
    30  	} else if s > 1000 {
    31  		return fmt.Sprintf("%.2fkB", s/1000)
    32  	} else {
    33  		return fmt.Sprintf("%.2fB", s)
    34  	}
    35  }
    36  
    37  type WriteCounter StorageSize
    38  
    39  func (c *WriteCounter) Write(b []byte) (int, error) {
    40  	*c += WriteCounter(len(b))
    41  	return len(b), nil
    42  }