output.go
changeset 39 24ca1bf4a0bf
parent 36 e918b7e63748
child 42 3fa13770e970
equal deleted inserted replaced
38:25db238bf03f 39:24ca1bf4a0bf
    22 import (
    22 import (
    23 	"encoding/json"
    23 	"encoding/json"
    24 	"fmt"
    24 	"fmt"
    25 )
    25 )
    26 
    26 
       
    27 // formatSize returns the size in a string with a human-readable format.
       
    28 func formatSize(sizeBytes uint64, short bool) string {
       
    29 	var units = map[int]string{
       
    30 		0: "B",
       
    31 		1: "KiB",
       
    32 		2: "MiB",
       
    33 		3: "GiB",
       
    34 		4: "TiB",
       
    35 		5: "PiB",
       
    36 	}
       
    37 	humanSize := sizeBytes
       
    38 	var n int
       
    39 	for n < len(units)-1 {
       
    40 		if humanSize < 10000 {
       
    41 			break
       
    42 		}
       
    43 		humanSize /= 1024
       
    44 		n++
       
    45 	}
       
    46 	if n < 1 {
       
    47 		return fmt.Sprintf("%d bytes", sizeBytes)
       
    48 	}
       
    49 	if short {
       
    50 		return fmt.Sprintf("%d %s", humanSize, units[n])
       
    51 	}
       
    52 	return fmt.Sprintf("%d bytes (%d %s)", sizeBytes, humanSize, units[n])
       
    53 }
       
    54 
       
    55 // displayResults formats results to plaintext or JSON and sends them to stdout
    27 func displayResults(results Results, jsonOutput bool, summaryOnly bool) {
    56 func displayResults(results Results, jsonOutput bool, summaryOnly bool) {
    28 	if jsonOutput {
    57 	if jsonOutput {
    29 		displayResultsJSON(results)
    58 		displayResultsJSON(results)
    30 		return
    59 		return
    31 	}
    60 	}