author | Mikael Berthe <mikael@lilotux.net> |
Sun, 14 Oct 2018 17:27:37 +0200 | |
changeset 42 | 3fa13770e970 |
parent 39 | 24ca1bf4a0bf |
child 45 | ea6a9ba7a3c8 |
permissions | -rw-r--r-- |
36 | 1 |
/* |
2 |
* Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net> |
|
3 |
* |
|
4 |
* This program is free software; you can redistribute it and/or modify |
|
5 |
* it under the terms of the GNU General Public License as published by |
|
6 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
7 |
* your option) any later version. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, but |
|
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 |
* General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
17 |
* USA |
|
18 |
*/ |
|
19 |
||
20 |
package main |
|
21 |
||
22 |
import ( |
|
23 |
"encoding/json" |
|
24 |
"fmt" |
|
25 |
) |
|
26 |
||
39
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
27 |
// formatSize returns the size in a string with a human-readable format. |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
28 |
func formatSize(sizeBytes uint64, short bool) string { |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
29 |
var units = map[int]string{ |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
30 |
0: "B", |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
31 |
1: "KiB", |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
32 |
2: "MiB", |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
33 |
3: "GiB", |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
34 |
4: "TiB", |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
35 |
5: "PiB", |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
36 |
} |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
37 |
humanSize := sizeBytes |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
38 |
var n int |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
39 |
for n < len(units)-1 { |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
40 |
if humanSize < 10000 { |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
41 |
break |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
42 |
} |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
43 |
humanSize /= 1024 |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
44 |
n++ |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
45 |
} |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
46 |
if n < 1 { |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
47 |
return fmt.Sprintf("%d bytes", sizeBytes) |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
48 |
} |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
49 |
if short { |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
50 |
return fmt.Sprintf("%d %s", humanSize, units[n]) |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
51 |
} |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
52 |
return fmt.Sprintf("%d bytes (%d %s)", sizeBytes, humanSize, units[n]) |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
53 |
} |
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
54 |
|
24ca1bf4a0bf
Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents:
36
diff
changeset
|
55 |
// displayResults formats results to plaintext or JSON and sends them to stdout |
36 | 56 |
func displayResults(results Results, jsonOutput bool, summaryOnly bool) { |
57 |
if jsonOutput { |
|
58 |
displayResultsJSON(results) |
|
59 |
return |
|
60 |
} |
|
61 |
||
62 |
if !summaryOnly { |
|
63 |
for i, g := range results.Groups { |
|
64 |
fmt.Printf("\nGroup #%d (%d files * %v):\n", i+1, |
|
42 | 65 |
len(g.Paths), formatSize(g.FileSize, true)) |
36 | 66 |
for _, f := range g.Paths { |
67 |
fmt.Println(f) |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
// We're done if we do not display statistics |
|
73 |
if myLog.verbosity < 1 && !summaryOnly { |
|
74 |
return |
|
75 |
} |
|
76 |
||
77 |
// Add a trailing newline |
|
78 |
if len(results.Groups) > 0 && myLog.verbosity > 0 { |
|
79 |
fmt.Println() |
|
80 |
} |
|
81 |
myLog.Println(0, "Final count:", results.Duplicates, |
|
82 |
"duplicate files in", len(results.Groups), "sets") |
|
83 |
myLog.Println(0, "Redundant data size:", |
|
42 | 84 |
formatSize(results.RedundantDataSizeBytes, false)) |
36 | 85 |
} |
86 |
||
87 |
func displayResultsJSON(results Results) { |
|
88 |
b, err := json.Marshal(results) |
|
89 |
if err != nil { |
|
90 |
panic(err) |
|
91 |
} |
|
92 |
fmt.Println(string(b)) |
|
93 |
} |