github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/size.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser 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 // The go-aigar library 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 Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package common 19 20 import ( 21 "fmt" 22 ) 23 24 // StorageSize is a wrapper around a float value that supports user friendly 25 // formatting. 26 type StorageSize float64 27 28 // String implements the stringer interface. 29 func (s StorageSize) String() string { 30 if s > 1099511627776 { 31 return fmt.Sprintf("%.2f TiB", s/1099511627776) 32 } else if s > 1073741824 { 33 return fmt.Sprintf("%.2f GiB", s/1073741824) 34 } else if s > 1048576 { 35 return fmt.Sprintf("%.2f MiB", s/1048576) 36 } else if s > 1024 { 37 return fmt.Sprintf("%.2f KiB", s/1024) 38 } else { 39 return fmt.Sprintf("%.2f B", s) 40 } 41 } 42 43 // TerminalString implements log.TerminalStringer, formatting a string for console 44 // output during logging. 45 func (s StorageSize) TerminalString() string { 46 if s > 1099511627776 { 47 return fmt.Sprintf("%.2fTiB", s/1099511627776) 48 } else if s > 1073741824 { 49 return fmt.Sprintf("%.2fGiB", s/1073741824) 50 } else if s > 1048576 { 51 return fmt.Sprintf("%.2fMiB", s/1048576) 52 } else if s > 1024 { 53 return fmt.Sprintf("%.2fKiB", s/1024) 54 } else { 55 return fmt.Sprintf("%.2fB", s) 56 } 57 }