Skip to content

Commit 3ce5e17

Browse files
committed
added flags to show each value individually
1 parent 057605d commit 3ce5e17

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

diskspace/main.swift

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,79 @@ import ArgumentParser
1212
// https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity
1313

1414
struct DiskSpace : ParsableCommand {
15-
@Flag var humanReadable: Bool = false
15+
@Flag(name: [.customShort("H"), .long],
16+
help: "'Human-readable' output using unit suffixes")
17+
var humanReadable = false
1618

17-
18-
func format(_ int: Int) -> String {
19-
return format(Int64(int))
19+
@Flag(name: .shortAndLong,
20+
help: "Print only the value of the Available Capacity")
21+
var available = false
22+
23+
@Flag(name: .shortAndLong,
24+
help: "Print only the value of the Important Capacity")
25+
var important = false
26+
27+
@Flag(name: .shortAndLong,
28+
help: "Print only the value of the Opportunistic Capacity")
29+
var opportunistic = false
30+
31+
@Flag(name: .shortAndLong,
32+
help: "Print only the value of the total Capacity")
33+
var total = false
34+
35+
func printValue(value int: Int, label: String? = nil) {
36+
printValue(value: Int64(int), label: label)
2037
}
2138

22-
func format(_ int: Int64) -> String {
39+
func printValue(value int: Int64, label: String? = nil) {
40+
var value = ""
41+
2342
if humanReadable {
24-
return ByteCountFormatter().string(fromByteCount: Int64(int))
43+
value = ByteCountFormatter().string(fromByteCount: int)
44+
} else {
45+
value = String(int)
46+
}
47+
48+
if let label = label {
49+
print("\(label): \(value)")
2550
} else {
26-
return String(int)
51+
print(value)
2752
}
2853
}
2954

3055
func run() {
56+
let showAll = !(available || important || opportunistic || total)
57+
3158
let systemVolume = URL(fileURLWithPath:"/")
3259
do {
3360
let values = try systemVolume.resourceValues(forKeys: [.volumeAvailableCapacityKey,.volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityForOpportunisticUsageKey, .volumeTotalCapacityKey])
34-
if let totalCapacity = values.volumeAvailableCapacity {
35-
print("Available: \(format(totalCapacity))")
61+
if let availableCapacity = values.volumeAvailableCapacity {
62+
if available {
63+
printValue(value: availableCapacity)
64+
} else if showAll {
65+
printValue(value: availableCapacity, label: "Available")
66+
}
3667
}
3768
if let importantCapacity = values.volumeAvailableCapacityForImportantUsage {
38-
print("Important: \(format(importantCapacity))")
69+
if important {
70+
printValue(value: importantCapacity)
71+
} else if showAll {
72+
printValue(value: importantCapacity, label: "Important")
73+
}
3974
}
4075
if let opportunisticCapacity = values.volumeAvailableCapacityForOpportunisticUsage {
41-
print("Opportunistic: \(format(opportunisticCapacity))")
76+
if opportunistic {
77+
printValue(value: opportunisticCapacity)
78+
} else if showAll {
79+
printValue(value: opportunisticCapacity, label: "Opportunistic")
80+
}
4281
}
4382
if let totalCapacity = values.volumeTotalCapacity {
44-
print("Total: \(format(totalCapacity))")
83+
if total {
84+
printValue(value: totalCapacity)
85+
} else if showAll {
86+
printValue(value: totalCapacity, label: "Total")
87+
}
4588
}
4689
} catch {
4790
print("Error retrieving capacity: \(error.localizedDescription)")

0 commit comments

Comments
 (0)