Skip to content

Commit

Permalink
feat: moved from custom byte formatter to the internal
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Dec 18, 2024
1 parent 3887157 commit e103111
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
25 changes: 10 additions & 15 deletions Kit/helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions Modules/RAM/popup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit e103111

Please sign in to comment.