-
Notifications
You must be signed in to change notification settings - Fork 174
/
bytes.go
95 lines (82 loc) · 2.88 KB
/
bytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// forked from https://www.socketloop.com/tutorials/golang-byte-format-example
// Package utils provides generic utility functions.
package utils
import (
"fmt"
"math"
"strconv"
"time"
"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/pkg/message"
)
// RoundUp rounds a float64 to the given number of decimal places.
func RoundUp(input float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * input
round = math.Ceil(digit)
newVal = round / pow
return
}
// ByteFormat formats a number of bytes into a human readable string.
func ByteFormat(inputNum float64, precision int) string {
if precision <= 0 {
precision = 1
}
var unit string
var returnVal float64
// https://www.techtarget.com/searchstorage/definition/mebibyte-MiB
if inputNum >= 1000000000 {
returnVal = RoundUp(inputNum/1000000000, precision)
unit = " GB" // gigabyte
} else if inputNum >= 1000000 {
returnVal = RoundUp(inputNum/1000000, precision)
unit = " MB" // megabyte
} else if inputNum >= 1000 {
returnVal = RoundUp(inputNum/1000, precision)
unit = " KB" // kilobyte
} else {
returnVal = inputNum
unit = " Byte" // byte
}
if returnVal > 1 {
unit += "s"
}
return strconv.FormatFloat(returnVal, 'f', precision, 64) + unit
}
// RenderProgressBarForLocalDirWrite creates a progress bar that continuously tracks the progress of writing files to a local directory and all of its subdirectories.
// NOTE: This function runs infinitely until either completeChan or errChan is triggered, this function should be run in a goroutine while a different thread/process is writing to the directory.
func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, completeChan chan error, updateText string, successText string) {
// Create a progress bar
title := fmt.Sprintf("%s (%s of %s)", updateText, ByteFormat(float64(0), 2), ByteFormat(float64(expectedTotal), 2))
progressBar := message.NewProgressBar(expectedTotal, title)
for {
select {
case err := <-completeChan:
if err == nil {
// Send success message
progressBar.Successf("%s (%s)", successText, ByteFormat(float64(expectedTotal), 2))
completeChan <- nil
return
} else {
progressBar.Stop()
completeChan <- nil
return
}
default:
// Read the directory size
currentBytes, dirErr := helpers.GetDirSize(filepath)
if dirErr != nil {
message.Debugf("unable to get updated progress: %s", dirErr.Error())
time.Sleep(200 * time.Millisecond)
continue
}
// Update the progress bar with the current size
title := fmt.Sprintf("%s (%s of %s)", updateText, ByteFormat(float64(currentBytes), 2), ByteFormat(float64(expectedTotal), 2))
progressBar.Update(currentBytes, title)
time.Sleep(200 * time.Millisecond)
}
}
}