diff --git a/Kit/helpers.swift b/Kit/helpers.swift index 6c4a3530..1b8db070 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -190,21 +190,16 @@ public struct Units { } } - public func getReadableMemory(to round: Int = 1) -> String { - switch bytes { - case 0..<1_000: - return "0 KB" - case 1_000..<(1_000 * 1_000): - return String(format: "%.0f KB", kilobytes) - case 1_000..<(1_000 * 1_000 * 1_000): - return String(format: "%.0f MB", megabytes) - case 1_000..<(1_000 * 1_000 * 1_000 * 1_000): - return String(format: "%.1f GB", gigabytes) - case (1_000 * 1_000 * 1_000 * 1_000)...Int64.max: - return String(format: "%.1f TB", terabytes) - default: - return String(format: "%.0f KB", kilobytes) - } + public func getReadableMemory(style: ByteCountFormatter.CountStyle = .file) -> String { + let formatter: ByteCountFormatter = ByteCountFormatter() + formatter.countStyle = style + formatter.includesUnit = true + formatter.isAdaptive = true + + var value = formatter.string(fromByteCount: Int64(bytes)) + value = value.replacingOccurrences(of: ",", with: ".") + + return value } public func toUnit(_ unit: SizeUnit) -> Double { diff --git a/Modules/RAM/popup.swift b/Modules/RAM/popup.swift index 96d5e7a4..35cc5bb3 100644 --- a/Modules/RAM/popup.swift +++ b/Modules/RAM/popup.swift @@ -241,14 +241,14 @@ internal class Popup: PopupWrapper { public func loadCallback(_ value: RAM_Usage) { DispatchQueue.main.async(execute: { if (self.window?.isVisible ?? false) || !self.initialized { - self.appField?.stringValue = Units(bytes: Int64(value.app)).getReadableMemory() - self.inactiveField?.stringValue = Units(bytes: Int64(value.inactive)).getReadableMemory() - self.wiredField?.stringValue = Units(bytes: Int64(value.wired)).getReadableMemory() - self.compressedField?.stringValue = Units(bytes: Int64(value.compressed)).getReadableMemory() - self.swapField?.stringValue = Units(bytes: Int64(value.swap.used)).getReadableMemory() + self.appField?.stringValue = Units(bytes: Int64(value.app)).getReadableMemory(style: .memory) + self.inactiveField?.stringValue = Units(bytes: Int64(value.inactive)).getReadableMemory(style: .memory) + self.wiredField?.stringValue = Units(bytes: Int64(value.wired)).getReadableMemory(style: .memory) + self.compressedField?.stringValue = Units(bytes: Int64(value.compressed)).getReadableMemory(style: .memory) + self.swapField?.stringValue = Units(bytes: Int64(value.swap.used)).getReadableMemory(style: .memory) - self.usedField?.stringValue = Units(bytes: Int64(value.used)).getReadableMemory() - self.freeField?.stringValue = Units(bytes: Int64(value.free)).getReadableMemory() + self.usedField?.stringValue = Units(bytes: Int64(value.used)).getReadableMemory(style: .memory) + self.freeField?.stringValue = Units(bytes: Int64(value.free)).getReadableMemory(style: .memory) self.circle?.setValue(value.usage) self.circle?.setSegments([