- fix Network stats base in the popup view (#176)

This commit is contained in:
Serhiy Mytrovtsiy
2020-11-20 17:15:10 +01:00
parent 147d3ee2cc
commit e31f9934f6
3 changed files with 18 additions and 15 deletions

View File

@@ -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) {

View File

@@ -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)"
}

View File

@@ -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")
}
}