├── .gitignore ├── LICENSE ├── README.md ├── diskspace.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ ├── IDEWorkspaceChecks.plist │ │ └── swiftpm │ │ └── Package.resolved ├── xcshareddata │ └── xcschemes │ │ └── diskspace.xcscheme └── xcuserdata │ └── armin.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── diskspace └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.pkg 3 | notary.log 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Licensed under the Apache License, Version 2.0 (the "License"); 2 | you may not use this source code except in compliance with the License. 3 | You may obtain a copy of the License at 4 | 5 | https://www.apache.org/licenses/LICENSE-2.0 6 | 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # diskspace 2 | 3 | ![](https://img.shields.io/github/v/release/scriptingosx/diskspace) ![](https://img.shields.io/github/downloads/scriptingosx/diskspace/latest/total) ![](https://img.shields.io/badge/macOS-10.14%2B-success) ![](https://img.shields.io/github/license/scriptingosx/diskspace) 4 | 5 | Returns available disk space 6 | 7 | With the various APFS features the value for free disk space returned from 8 | tools such as `du` or `df` will not be accurate. This tool uses system 9 | functions to get various measures of available disk space. 10 | 11 | The 'Important' value matches the free disk space value shown in Finder. 12 | 13 | You can get the details from Apple's documentation: 14 | 15 | https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity 16 | 17 | ``` 18 | USAGE: diskspace [--human-readable] [--available] [--important] [--opportunistic] [--total] [] 19 | 20 | ARGUMENTS: 21 | path to the volume (default: /) 22 | 23 | OPTIONS: 24 | -H, --human-readable Human readable output using unit suffixes 25 | -a, --available Print only the value of the Available Capacity 26 | -i, --important Print only the value of the Important Capacity 27 | -o, --opportunistic Print only the value of the Opportunistic Capacity 28 | -t, --total Print only the value of the Total Capacity 29 | --version Show the version. 30 | -h, --help Show help information. 31 | ``` -------------------------------------------------------------------------------- /diskspace.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C6F43F032731718900747000 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6F43F022731718900747000 /* main.swift */; }; 11 | C6F43F0B2731762C00747000 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = C6F43F0A2731762C00747000 /* ArgumentParser */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXCopyFilesBuildPhase section */ 15 | C6F43EFD2731718900747000 /* CopyFiles */ = { 16 | isa = PBXCopyFilesBuildPhase; 17 | buildActionMask = 2147483647; 18 | dstPath = /usr/share/man/man1/; 19 | dstSubfolderSpec = 0; 20 | files = ( 21 | ); 22 | runOnlyForDeploymentPostprocessing = 1; 23 | }; 24 | /* End PBXCopyFilesBuildPhase section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | C6F43EFF2731718900747000 /* diskspace */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = diskspace; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | C6F43F022731718900747000 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | C6F43EFC2731718900747000 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | C6F43F0B2731762C00747000 /* ArgumentParser in Frameworks */, 37 | ); 38 | runOnlyForDeploymentPostprocessing = 0; 39 | }; 40 | /* End PBXFrameworksBuildPhase section */ 41 | 42 | /* Begin PBXGroup section */ 43 | C6F43EF62731718900747000 = { 44 | isa = PBXGroup; 45 | children = ( 46 | C6F43F012731718900747000 /* diskspace */, 47 | C6F43F002731718900747000 /* Products */, 48 | ); 49 | sourceTree = ""; 50 | }; 51 | C6F43F002731718900747000 /* Products */ = { 52 | isa = PBXGroup; 53 | children = ( 54 | C6F43EFF2731718900747000 /* diskspace */, 55 | ); 56 | name = Products; 57 | sourceTree = ""; 58 | }; 59 | C6F43F012731718900747000 /* diskspace */ = { 60 | isa = PBXGroup; 61 | children = ( 62 | C6F43F022731718900747000 /* main.swift */, 63 | ); 64 | path = diskspace; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | C6F43EFE2731718900747000 /* diskspace */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = C6F43F062731718900747000 /* Build configuration list for PBXNativeTarget "diskspace" */; 73 | buildPhases = ( 74 | C6F43EFB2731718900747000 /* Sources */, 75 | C6F43EFC2731718900747000 /* Frameworks */, 76 | C6F43EFD2731718900747000 /* CopyFiles */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = diskspace; 83 | packageProductDependencies = ( 84 | C6F43F0A2731762C00747000 /* ArgumentParser */, 85 | ); 86 | productName = diskspace; 87 | productReference = C6F43EFF2731718900747000 /* diskspace */; 88 | productType = "com.apple.product-type.tool"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | C6F43EF72731718900747000 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | BuildIndependentTargetsInParallel = 1; 97 | KnownAssetTags = ( 98 | New, 99 | ); 100 | LastSwiftUpdateCheck = 1310; 101 | LastUpgradeCheck = 1310; 102 | TargetAttributes = { 103 | C6F43EFE2731718900747000 = { 104 | CreatedOnToolsVersion = 13.1; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = C6F43EFA2731718900747000 /* Build configuration list for PBXProject "diskspace" */; 109 | compatibilityVersion = "Xcode 13.0"; 110 | developmentRegion = en; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | Base, 115 | ); 116 | mainGroup = C6F43EF62731718900747000; 117 | packageReferences = ( 118 | C6F43F092731762C00747000 /* XCRemoteSwiftPackageReference "swift-argument-parser" */, 119 | ); 120 | productRefGroup = C6F43F002731718900747000 /* Products */; 121 | projectDirPath = ""; 122 | projectRoot = ""; 123 | targets = ( 124 | C6F43EFE2731718900747000 /* diskspace */, 125 | ); 126 | }; 127 | /* End PBXProject section */ 128 | 129 | /* Begin PBXSourcesBuildPhase section */ 130 | C6F43EFB2731718900747000 /* Sources */ = { 131 | isa = PBXSourcesBuildPhase; 132 | buildActionMask = 2147483647; 133 | files = ( 134 | C6F43F032731718900747000 /* main.swift in Sources */, 135 | ); 136 | runOnlyForDeploymentPostprocessing = 0; 137 | }; 138 | /* End PBXSourcesBuildPhase section */ 139 | 140 | /* Begin XCBuildConfiguration section */ 141 | C6F43F042731718900747000 /* Debug */ = { 142 | isa = XCBuildConfiguration; 143 | buildSettings = { 144 | ALWAYS_SEARCH_USER_PATHS = NO; 145 | CLANG_ANALYZER_NONNULL = YES; 146 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 147 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 148 | CLANG_CXX_LIBRARY = "libc++"; 149 | CLANG_ENABLE_MODULES = YES; 150 | CLANG_ENABLE_OBJC_ARC = YES; 151 | CLANG_ENABLE_OBJC_WEAK = YES; 152 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 153 | CLANG_WARN_BOOL_CONVERSION = YES; 154 | CLANG_WARN_COMMA = YES; 155 | CLANG_WARN_CONSTANT_CONVERSION = YES; 156 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 157 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 158 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 159 | CLANG_WARN_EMPTY_BODY = YES; 160 | CLANG_WARN_ENUM_CONVERSION = YES; 161 | CLANG_WARN_INFINITE_RECURSION = YES; 162 | CLANG_WARN_INT_CONVERSION = YES; 163 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 164 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 165 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 166 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 167 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 168 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 169 | CLANG_WARN_STRICT_PROTOTYPES = YES; 170 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 171 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 172 | CLANG_WARN_UNREACHABLE_CODE = YES; 173 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 174 | COPY_PHASE_STRIP = NO; 175 | DEBUG_INFORMATION_FORMAT = dwarf; 176 | ENABLE_STRICT_OBJC_MSGSEND = YES; 177 | ENABLE_TESTABILITY = YES; 178 | GCC_C_LANGUAGE_STANDARD = gnu11; 179 | GCC_DYNAMIC_NO_PIC = NO; 180 | GCC_NO_COMMON_BLOCKS = YES; 181 | GCC_OPTIMIZATION_LEVEL = 0; 182 | GCC_PREPROCESSOR_DEFINITIONS = ( 183 | "DEBUG=1", 184 | "$(inherited)", 185 | ); 186 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 187 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 188 | GCC_WARN_UNDECLARED_SELECTOR = YES; 189 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 190 | GCC_WARN_UNUSED_FUNCTION = YES; 191 | GCC_WARN_UNUSED_VARIABLE = YES; 192 | MACOSX_DEPLOYMENT_TARGET = 10.14; 193 | MARKETING_VERSION = 1; 194 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 195 | MTL_FAST_MATH = YES; 196 | ONLY_ACTIVE_ARCH = YES; 197 | SDKROOT = macosx; 198 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 199 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 200 | }; 201 | name = Debug; 202 | }; 203 | C6F43F052731718900747000 /* Release */ = { 204 | isa = XCBuildConfiguration; 205 | buildSettings = { 206 | ALWAYS_SEARCH_USER_PATHS = NO; 207 | CLANG_ANALYZER_NONNULL = YES; 208 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 209 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 210 | CLANG_CXX_LIBRARY = "libc++"; 211 | CLANG_ENABLE_MODULES = YES; 212 | CLANG_ENABLE_OBJC_ARC = YES; 213 | CLANG_ENABLE_OBJC_WEAK = YES; 214 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 215 | CLANG_WARN_BOOL_CONVERSION = YES; 216 | CLANG_WARN_COMMA = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 219 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 220 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 221 | CLANG_WARN_EMPTY_BODY = YES; 222 | CLANG_WARN_ENUM_CONVERSION = YES; 223 | CLANG_WARN_INFINITE_RECURSION = YES; 224 | CLANG_WARN_INT_CONVERSION = YES; 225 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 226 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 227 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 228 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 229 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 230 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 231 | CLANG_WARN_STRICT_PROTOTYPES = YES; 232 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 233 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 234 | CLANG_WARN_UNREACHABLE_CODE = YES; 235 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 236 | COPY_PHASE_STRIP = NO; 237 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 238 | ENABLE_NS_ASSERTIONS = NO; 239 | ENABLE_STRICT_OBJC_MSGSEND = YES; 240 | GCC_C_LANGUAGE_STANDARD = gnu11; 241 | GCC_NO_COMMON_BLOCKS = YES; 242 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 243 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 244 | GCC_WARN_UNDECLARED_SELECTOR = YES; 245 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 246 | GCC_WARN_UNUSED_FUNCTION = YES; 247 | GCC_WARN_UNUSED_VARIABLE = YES; 248 | MACOSX_DEPLOYMENT_TARGET = 10.14; 249 | MARKETING_VERSION = 1; 250 | MTL_ENABLE_DEBUG_INFO = NO; 251 | MTL_FAST_MATH = YES; 252 | SDKROOT = macosx; 253 | SWIFT_COMPILATION_MODE = wholemodule; 254 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 255 | }; 256 | name = Release; 257 | }; 258 | C6F43F072731718900747000 /* Debug */ = { 259 | isa = XCBuildConfiguration; 260 | buildSettings = { 261 | CODE_SIGN_IDENTITY = "Developer ID Application"; 262 | CODE_SIGN_STYLE = Manual; 263 | DEVELOPMENT_TEAM = JME5BW3F3R; 264 | ENABLE_HARDENED_RUNTIME = YES; 265 | PRODUCT_BUNDLE_IDENTIFIER = com.scriptingosx.diskspace; 266 | PRODUCT_NAME = "$(TARGET_NAME)"; 267 | PROVISIONING_PROFILE_SPECIFIER = ""; 268 | SWIFT_VERSION = 5.0; 269 | }; 270 | name = Debug; 271 | }; 272 | C6F43F082731718900747000 /* Release */ = { 273 | isa = XCBuildConfiguration; 274 | buildSettings = { 275 | CODE_SIGN_IDENTITY = "Developer ID Application"; 276 | CODE_SIGN_STYLE = Manual; 277 | DEVELOPMENT_TEAM = JME5BW3F3R; 278 | ENABLE_HARDENED_RUNTIME = YES; 279 | PRODUCT_BUNDLE_IDENTIFIER = com.scriptingosx.diskspace; 280 | PRODUCT_NAME = "$(TARGET_NAME)"; 281 | PROVISIONING_PROFILE_SPECIFIER = ""; 282 | SWIFT_VERSION = 5.0; 283 | }; 284 | name = Release; 285 | }; 286 | /* End XCBuildConfiguration section */ 287 | 288 | /* Begin XCConfigurationList section */ 289 | C6F43EFA2731718900747000 /* Build configuration list for PBXProject "diskspace" */ = { 290 | isa = XCConfigurationList; 291 | buildConfigurations = ( 292 | C6F43F042731718900747000 /* Debug */, 293 | C6F43F052731718900747000 /* Release */, 294 | ); 295 | defaultConfigurationIsVisible = 0; 296 | defaultConfigurationName = Release; 297 | }; 298 | C6F43F062731718900747000 /* Build configuration list for PBXNativeTarget "diskspace" */ = { 299 | isa = XCConfigurationList; 300 | buildConfigurations = ( 301 | C6F43F072731718900747000 /* Debug */, 302 | C6F43F082731718900747000 /* Release */, 303 | ); 304 | defaultConfigurationIsVisible = 0; 305 | defaultConfigurationName = Release; 306 | }; 307 | /* End XCConfigurationList section */ 308 | 309 | /* Begin XCRemoteSwiftPackageReference section */ 310 | C6F43F092731762C00747000 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = { 311 | isa = XCRemoteSwiftPackageReference; 312 | repositoryURL = "https://github.com/apple/swift-argument-parser.git"; 313 | requirement = { 314 | kind = upToNextMajorVersion; 315 | minimumVersion = 1.0.0; 316 | }; 317 | }; 318 | /* End XCRemoteSwiftPackageReference section */ 319 | 320 | /* Begin XCSwiftPackageProductDependency section */ 321 | C6F43F0A2731762C00747000 /* ArgumentParser */ = { 322 | isa = XCSwiftPackageProductDependency; 323 | package = C6F43F092731762C00747000 /* XCRemoteSwiftPackageReference "swift-argument-parser" */; 324 | productName = ArgumentParser; 325 | }; 326 | /* End XCSwiftPackageProductDependency section */ 327 | }; 328 | rootObject = C6F43EF72731718900747000 /* Project object */; 329 | } 330 | -------------------------------------------------------------------------------- /diskspace.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /diskspace.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /diskspace.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "object": { 3 | "pins": [ 4 | { 5 | "package": "swift-argument-parser", 6 | "repositoryURL": "https://github.com/apple/swift-argument-parser.git", 7 | "state": { 8 | "branch": null, 9 | "revision": "d2930e8fcf9c33162b9fcc1d522bc975e2d4179b", 10 | "version": "1.0.1" 11 | } 12 | } 13 | ] 14 | }, 15 | "version": 1 16 | } 17 | -------------------------------------------------------------------------------- /diskspace.xcodeproj/xcshareddata/xcschemes/diskspace.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 43 | 45 | 51 | 52 | 53 | 54 | 57 | 58 | 61 | 62 | 63 | 64 | 70 | 72 | 78 | 79 | 80 | 81 | 83 | 84 | 87 | 88 | 90 | 93 | 94 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /diskspace.xcodeproj/xcuserdata/armin.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | diskspace.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | C6F43EFE2731718900747000 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /diskspace/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // diskspace 4 | // 5 | // Created by Armin Briegel on 2021-11-02. 6 | // 7 | 8 | import Foundation 9 | import ArgumentParser 10 | 11 | // Code based on sample from here: 12 | // https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity 13 | 14 | struct DiskSpace : ParsableCommand { 15 | static let configuration = CommandConfiguration( 16 | commandName: "diskspace", 17 | abstract: "Returns available disk space", 18 | discussion: """ 19 | With the various APFS features the value for free disk space returned from tools such as `du` or `df` will not be accurate. This tool uses system functions to get various measures of available disk space. 20 | 21 | The 'Important' value matches the free disk space value shown in Finder. 22 | 23 | You can get the details from Apple's documentation: 24 | 25 | https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity 26 | """, 27 | version: "1" 28 | ) 29 | 30 | // MARK: Flags and Arguments 31 | 32 | @Flag(name: [.customShort("H"), .long], 33 | help: "Human readable output using unit suffixes") 34 | var humanReadable = false 35 | 36 | @Flag(name: .shortAndLong, 37 | help: "Print only the value of the Available Capacity") 38 | var available = false 39 | 40 | @Flag(name: .shortAndLong, 41 | help: "Print only the value of the Important Capacity") 42 | var important = false 43 | 44 | @Flag(name: .shortAndLong, 45 | help: "Print only the value of the Opportunistic Capacity") 46 | var opportunistic = false 47 | 48 | @Flag(name: .shortAndLong, 49 | help: "Print only the value of the Total Capacity") 50 | var total = false 51 | 52 | @Argument(help: "path to the volume") var volumePath = "/" 53 | 54 | // MARK: Functions 55 | 56 | func printValue(value int: Int, label: String? = nil) { 57 | printValue(value: Int64(int), label: label) 58 | } 59 | 60 | func printValue(value int: Int64, label: String? = nil) { 61 | var value = "" 62 | 63 | if humanReadable { 64 | value = ByteCountFormatter().string(fromByteCount: int) 65 | } else { 66 | value = String(int) 67 | } 68 | 69 | if let label = label { 70 | let paddedLabel = "\(label):".padding(toLength: 15, withPad: " ", startingAt: 0) 71 | print("\(paddedLabel) \(value)") 72 | } else { 73 | print(value) 74 | } 75 | } 76 | 77 | // MARK: Run the command 78 | 79 | func run() { 80 | let showAll = !(available || important || opportunistic || total) 81 | 82 | let systemVolume = URL(fileURLWithPath: volumePath) 83 | do { 84 | let values = try systemVolume.resourceValues(forKeys: [.volumeAvailableCapacityKey,.volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityForOpportunisticUsageKey, .volumeTotalCapacityKey]) 85 | if let availableCapacity = values.volumeAvailableCapacity { 86 | if available { 87 | printValue(value: availableCapacity) 88 | } else if showAll { 89 | printValue(value: availableCapacity, label: "Available") 90 | } 91 | } 92 | if let importantCapacity = values.volumeAvailableCapacityForImportantUsage { 93 | if important { 94 | printValue(value: importantCapacity) 95 | } else if showAll { 96 | printValue(value: importantCapacity, label: "Important") 97 | } 98 | } 99 | if let opportunisticCapacity = values.volumeAvailableCapacityForOpportunisticUsage { 100 | if opportunistic { 101 | printValue(value: opportunisticCapacity) 102 | } else if showAll { 103 | printValue(value: opportunisticCapacity, label: "Opportunistic") 104 | } 105 | } 106 | if let totalCapacity = values.volumeTotalCapacity { 107 | if total { 108 | printValue(value: totalCapacity) 109 | } else if showAll { 110 | printValue(value: totalCapacity, label: "Total") 111 | } 112 | } 113 | } catch { 114 | print("Error retrieving capacity: \(error.localizedDescription)") 115 | } 116 | } 117 | } 118 | 119 | // call the struct's main function 120 | DiskSpace.main() 121 | --------------------------------------------------------------------------------