mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
- fix Network stats base in the popup view (#176)
This commit is contained in:
@@ -49,7 +49,7 @@ public class SpeedWidget: Widget {
|
||||
if self.store != nil {
|
||||
self.valueState = store!.pointee.bool(key: "\(self.title)_\(self.type.rawValue)_value", defaultValue: self.valueState)
|
||||
self.icon = store!.pointee.string(key: "\(self.title)_\(self.type.rawValue)_icon", defaultValue: self.baseValue)
|
||||
self.baseValue = store!.pointee.string(key: "\(self.title)_\(self.type.rawValue)_base", defaultValue: self.baseValue)
|
||||
self.baseValue = store!.pointee.string(key: "\(self.title)_base", defaultValue: self.baseValue)
|
||||
}
|
||||
|
||||
if self.valueState && self.icon != "none" {
|
||||
@@ -279,7 +279,7 @@ public class SpeedWidget: Widget {
|
||||
return
|
||||
}
|
||||
self.baseValue = key
|
||||
self.store?.pointee.set(key: "\(self.title)_\(self.type.rawValue)_base", value: self.baseValue)
|
||||
self.store?.pointee.set(key: "\(self.title)_base", value: self.baseValue)
|
||||
}
|
||||
|
||||
public func setValue(upload: Int64, download: Int64) {
|
||||
|
||||
@@ -52,9 +52,9 @@ internal class Popup: NSView, Popup_p {
|
||||
private var chart: NetworkChartView? = nil
|
||||
private var processes: [NetworkProcessView] = []
|
||||
|
||||
private var base: String {
|
||||
private var base: DataSizeBase {
|
||||
get {
|
||||
return store.pointee.string(key: "\(self.title)_base", defaultValue: "byte")
|
||||
return DataSizeBase(rawValue: store.pointee.string(key: "\(self.title)_base", defaultValue: "byte")) ?? .byte
|
||||
}
|
||||
}
|
||||
private var numberOfProcesses: Int {
|
||||
@@ -270,8 +270,8 @@ internal class Popup: NSView, Popup_p {
|
||||
}
|
||||
|
||||
private func setUploadDownloadFields() {
|
||||
let upload = Units(bytes: self.uploadValue).getReadableTuple()
|
||||
let download = Units(bytes: self.downloadValue).getReadableTuple()
|
||||
let upload = Units(bytes: self.uploadValue).getReadableTuple(base: self.base)
|
||||
let download = Units(bytes: self.downloadValue).getReadableTuple(base: self.base)
|
||||
|
||||
var valueWidth = "\(upload.0)".widthOfString(usingFont: .systemFont(ofSize: 26, weight: .light)) + 5
|
||||
var unitWidth = upload.1.widthOfString(usingFont: .systemFont(ofSize: 13, weight: .light)) + 5
|
||||
@@ -357,8 +357,8 @@ internal class Popup: NSView, Popup_p {
|
||||
let index = list.count-i-1
|
||||
if self.processes.indices.contains(index) {
|
||||
self.processes[index].label = process.name
|
||||
self.processes[index].upload = Units(bytes: Int64(process.upload)).getReadableSpeed(base: DataSizeBase(rawValue: self.base) ?? .byte)
|
||||
self.processes[index].download = Units(bytes: Int64(process.download)).getReadableSpeed(base: DataSizeBase(rawValue: self.base) ?? .byte)
|
||||
self.processes[index].upload = Units(bytes: Int64(process.upload)).getReadableSpeed(base: self.base)
|
||||
self.processes[index].download = Units(bytes: Int64(process.download)).getReadableSpeed(base: self.base)
|
||||
self.processes[index].icon = process.icon
|
||||
self.processes[index].toolTip = "pid: \(process.pid)"
|
||||
}
|
||||
|
||||
@@ -103,20 +103,23 @@ public struct Units {
|
||||
return gigabytes / 1_024
|
||||
}
|
||||
|
||||
public func getReadableTuple() -> (String, String) {
|
||||
public func getReadableTuple(base: DataSizeBase = .byte) -> (String, String) {
|
||||
let stringBase = base == .byte ? "B" : "b"
|
||||
let multiplier: Double = base == .byte ? 1 : 8
|
||||
|
||||
switch bytes {
|
||||
case 0..<1_024:
|
||||
return ("0", "KB/s")
|
||||
return ("0", "K\(stringBase)/s")
|
||||
case 1_024..<(1_024 * 1_024):
|
||||
return (String(format: "%.0f", kilobytes), "KB/s")
|
||||
return (String(format: "%.0f", kilobytes*multiplier), "K\(stringBase)/s")
|
||||
case 1_024..<(1_024 * 1_024 * 100):
|
||||
return (String(format: "%.1f", megabytes), "MB/s")
|
||||
return (String(format: "%.1f", megabytes*multiplier), "M\(stringBase)/s")
|
||||
case (1_024 * 1_024 * 100)..<(1_024 * 1_024 * 1_024):
|
||||
return (String(format: "%.0f", megabytes), "MB/s")
|
||||
return (String(format: "%.0f", megabytes*multiplier), "M\(stringBase)/s")
|
||||
case (1_024 * 1_024 * 1_024)...Int64.max:
|
||||
return (String(format: "%.1f", gigabytes), "GB/s")
|
||||
return (String(format: "%.1f", gigabytes*multiplier), "G\(stringBase)/s")
|
||||
default:
|
||||
return (String(format: "%.0f", kilobytes), "KB/s")
|
||||
return (String(format: "%.0f", kilobytes*multiplier), "K\(stringBase)B/s")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user