fixed values visibility in network widget

This commit is contained in:
Serhiy Mytrovtsiy
2019-09-10 18:19:17 +02:00
parent 30635a4d08
commit c7cd2288ca
6 changed files with 36 additions and 15 deletions

View File

@@ -78,8 +78,12 @@ class MemoryReader: Reader {
if line.matches("^\\d+ + .+ +\\d+.\\d[M\\+\\-]+ *$") {
var str = line.trimmingCharacters(in: .whitespaces)
let pidString = str.findAndCrop(pattern: "^\\d+")
let usageString = str.findAndCrop(pattern: " [0-9]+M(\\+)*(\\-)*$")
let command = str.trimmingCharacters(in: .whitespaces)
let usageString = str.findAndCrop(pattern: " [0-9]+M(\\+|\\-)*$")
var command = str.trimmingCharacters(in: .whitespaces)
if let regex = try? NSRegularExpression(pattern: " (\\+|\\-)*$", options: .caseInsensitive) {
command = regex.stringByReplacingMatches(in: command, options: [], range: NSRange(location: 0, length: command.count), withTemplate: "")
}
let pid = Int(pidString) ?? 0
guard let usage = Double(usageString.filter("01234567890.".contains)) else {

View File

@@ -149,9 +149,9 @@ extension Memory {
self.tabView.view?.addSubview(vertical)
(self.reader as! MemoryReader).usage.subscribe(observer: self) { (value, _) in
totalValue.stringValue = Units(bytes: Int64(value.total)).getReadableUnit()
usedValue.stringValue = Units(bytes: Int64(value.used)).getReadableUnit()
freeValue.stringValue = Units(bytes: Int64(value.free)).getReadableUnit()
totalValue.stringValue = Units(bytes: Int64(value.total)).getReadableMemory()
usedValue.stringValue = Units(bytes: Int64(value.used)).getReadableMemory()
freeValue.stringValue = Units(bytes: Int64(value.free)).getReadableMemory()
}
}
@@ -210,7 +210,7 @@ extension Memory {
let processView = processViewList[i]
(processView.subviews[0] as! NSTextField).stringValue = process.command
(processView.subviews[1] as! NSTextField).stringValue = Units(bytes: Int64(process.usage)).getReadableUnit()
(processView.subviews[1] as! NSTextField).stringValue = Units(bytes: Int64(process.usage)).getReadableMemory()
}
}
}

View File

@@ -94,11 +94,11 @@ class NetworkArrowsTextView: NSView, Widget {
if self.download != download {
self.download = download
downloadValue.stringValue = "\(Units(bytes: self.download).getReadableUnit())/s"
downloadValue.stringValue = Units(bytes: self.download).getReadableSpeed()
}
if self.upload != upload {
self.upload = upload
uploadValue.stringValue = "\(Units(bytes: self.upload).getReadableUnit())/s"
uploadValue.stringValue = Units(bytes: self.upload).getReadableSpeed()
}
}

View File

@@ -80,11 +80,11 @@ class NetworkDotsTextView: NSView, Widget {
if self.download != download {
self.download = download
downloadValue.stringValue = "\(Units(bytes: self.download).getReadableUnit())/s"
downloadValue.stringValue = Units(bytes: self.download).getReadableSpeed()
}
if self.upload != upload {
self.upload = upload
uploadValue.stringValue = "\(Units(bytes: self.upload).getReadableUnit())/s"
uploadValue.stringValue = Units(bytes: self.upload).getReadableSpeed()
}
}

View File

@@ -44,8 +44,8 @@ class NetworkTextView: NSView, Widget {
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])
downloadValue.stringValue = "\(Units(bytes: download).getReadableUnit())/s"
uploadValue.stringValue = "\(Units(bytes: upload).getReadableUnit())/s"
downloadValue.stringValue = Units(bytes: download).getReadableSpeed()
uploadValue.stringValue = Units(bytes: upload).getReadableSpeed()
}
func valueView() {

View File

@@ -104,14 +104,31 @@ public struct Units {
}
}
public func getReadableUnit() -> String {
public func getReadableSpeed() -> String {
switch bytes {
case 0..<1_024:
return "0 KB"
case 1_024..<(1_024 * 1_024):
return String(format: "%.0f KB/s", kilobytes)
case 1_024..<(1_024 * 1_024 * 100):
return String(format: "%.1f MB/s", megabytes)
case (1_024 * 1_024 * 100)..<(1_024 * 1_024 * 1_024):
return String(format: "%.0f MB/s", megabytes)
case (1_024 * 1_024 * 1_024)...Int64.max:
return String(format: "%.1f GB/s", gigabytes)
default:
return String(format: "%.0f KB/s", kilobytes)
}
}
public func getReadableMemory() -> String {
switch bytes {
case 0..<1_024:
return "0 KB"
case 1_024..<(1_024 * 1_024):
return String(format: "%.0f KB", kilobytes)
case 1_024..<(1_024 * 1_024 * 1_024):
return String(format: "%.2f MB", megabytes)
return String(format: "%.0f MB", megabytes)
case (1_024 * 1_024 * 1_024)...Int64.max:
return String(format: "%.2f GB", gigabytes)
default:
@@ -215,7 +232,7 @@ extension String {
if let searchRange = regex.firstMatch(in: self, options: [], range: stringRange) {
let start = self.index(self.startIndex, offsetBy: searchRange.range.lowerBound)
let end = self.index(self.startIndex, offsetBy: searchRange.range.upperBound)
let value = String(self[start..<end])
let value = String(self[start..<end]).trimmingCharacters(in: .whitespaces)
line = self.replacingOccurrences(
of: value,
with: "",