reporting.deletedPerc   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2023 Clive Walkden <[email protected]>
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all
12
 * copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
 * OTHER DEALINGS IN THE SOFTWARE.
22
 */
23
24
package reporting
25
26
import (
27
	"fmt"
28
	"github.com/fatih/color"
29
	"github.com/rodaine/table"
30
	"github.com/spf13/viper"
31
	"math"
32
)
33
34
type Result struct {
35
	Name        string
36
	Kept        int
37
	KeptSize    string
38
	Deleted     int
39
	DeletedSize string
40
}
41
42
type Report struct {
43
	Result []Result
44
	DryRun bool
45
}
46
47
func Output(report Report) {
48
	if !viper.GetBool("verbose") {
49
		fmt.Print("\033[H\033[2J") //clear screen
50
	}
51
	headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
52
	columnFmt := color.New(color.FgYellow).SprintfFunc()
53
54
	heading := color.New(color.FgBlue, color.Bold).PrintlnFunc()
55
	if report.DryRun {
56
		heading("Potential Results: (DryRun Mode)")
57
	} else {
58
		heading("Results:")
59
	}
60
	fmt.Println("")
61
62
	tbl := table.New("Name", "Deleted Files", "Deleted Size", "Deleted (%)", "Remaining Files", "Remaining Size")
63
	tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt).WithPadding(5)
64
65
	for _, element := range report.Result {
66
		// Skip empty Structs
67
		if element.Name == "" {
68
			continue
69
		}
70
		tbl.AddRow(element.Name, element.Deleted, element.DeletedSize, deletedPerc(element), element.Kept, element.KeptSize)
71
	}
72
	tbl.Print()
73
	fmt.Println("")
74
	fmt.Println("")
75
}
76
77
func deletedPerc(result Result) (delta float64) {
78
	originalNumber := float64(result.Kept + result.Deleted)
79
	decrease := originalNumber - float64(result.Kept)
80
	delta = (decrease / originalNumber) * 100
81
	return math.Round(delta*100) / 100
82
}
83