mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
fixed parsing data from top output in cpu and memory reader
This commit is contained in:
@@ -84,15 +84,17 @@ class CPUReader: Reader {
|
||||
var processes: [TopProcess] = []
|
||||
outputString.enumerateLines { (line, stop) -> () in
|
||||
if line.matches("^\\d+ + .+ +\\d+.\\d *$") {
|
||||
let arr = line.condenseWhitespace().split(separator: " ")
|
||||
let pid = Int(arr[0]) ?? 0
|
||||
let command = String(arr[1])
|
||||
let usage = Double(arr[2]) ?? 0
|
||||
var str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidString = str.findAndCrop(pattern: "^\\d+")
|
||||
let usageString = str.findAndCrop(pattern: " [0-9]+\\.[0-9]*$")
|
||||
let command = str.trimmingCharacters(in: .whitespaces)
|
||||
|
||||
let pid = Int(pidString) ?? 0
|
||||
let usage = Double(usageString) ?? 0
|
||||
let process = TopProcess(pid: pid, command: command, usage: usage)
|
||||
processes.append(process)
|
||||
}
|
||||
}
|
||||
|
||||
self.processes << processes
|
||||
}
|
||||
|
||||
|
||||
@@ -76,10 +76,13 @@ class MemoryReader: Reader {
|
||||
var processes: [TopProcess] = []
|
||||
outputString.enumerateLines { (line, stop) -> () in
|
||||
if line.matches("^\\d+ + .+ +\\d+.\\d[M\\+\\-]+ *$") {
|
||||
let arr = line.condenseWhitespace().split(separator: " ")
|
||||
let pid = Int(arr[0]) ?? 0
|
||||
let command = String(arr[1])
|
||||
guard let usage = Double(arr[2].filter("01234567890.".contains)) else {
|
||||
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 pid = Int(pidString) ?? 0
|
||||
guard let usage = Double(usageString.filter("01234567890.".contains)) else {
|
||||
return
|
||||
}
|
||||
let process = TopProcess(pid: pid, command: command, usage: usage * Double(1024 * 1024))
|
||||
|
||||
@@ -205,3 +205,26 @@ extension NSColor {
|
||||
return String(format:"#%06x", rgb)
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
mutating func findAndCrop(pattern: String) -> String {
|
||||
let regex = try! NSRegularExpression(pattern: pattern)
|
||||
let stringRange = NSRange(location: 0, length: self.utf16.count)
|
||||
var line = self
|
||||
|
||||
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])
|
||||
line = self.replacingOccurrences(
|
||||
of: value,
|
||||
with: "",
|
||||
options: .regularExpression
|
||||
)
|
||||
self = line.trimmingCharacters(in: .whitespaces)
|
||||
return value.trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user