mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
47 lines
1.3 KiB
Swift
Executable File
47 lines
1.3 KiB
Swift
Executable File
//
|
|
// DiskUsage.swift
|
|
// Mini Stats
|
|
//
|
|
// Created by Serhiy Mytrovtsiy on 29/05/2019.
|
|
// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class DiskUsage {
|
|
var updateTimer: Timer!
|
|
|
|
init() {
|
|
read()
|
|
updateTimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(read), userInfo: nil, repeats: true)
|
|
}
|
|
|
|
@objc func read() {
|
|
let total = totalDiskSpaceInBytes()
|
|
let free = freeDiskSpaceInBytes()
|
|
let usedSpace = total - free
|
|
|
|
store.diskUsage << (Float(usedSpace) / Float(total))
|
|
}
|
|
|
|
func totalDiskSpaceInBytes() -> Int64 {
|
|
do {
|
|
let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String)
|
|
let space = (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value
|
|
return space!
|
|
} catch {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func freeDiskSpaceInBytes() -> Int64 {
|
|
do {
|
|
let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String)
|
|
let freeSpace = (systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value
|
|
return freeSpace!
|
|
} catch {
|
|
return 0
|
|
}
|
|
}
|
|
}
|