mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
fix: moved findAndCrop to non mutation function (#1700)
This commit is contained in:
@@ -36,27 +36,24 @@ extension String: LocalizedError {
|
||||
return components.filter { !$0.isEmpty }.joined(separator: " ")
|
||||
}
|
||||
|
||||
public mutating func findAndCrop(pattern: String) -> String {
|
||||
public func findAndCrop(pattern: String) -> (cropped: String, remain: String) {
|
||||
do {
|
||||
let regex = try NSRegularExpression(pattern: pattern)
|
||||
let stringRange = NSRange(location: 0, length: self.utf16.count)
|
||||
var line = self
|
||||
let range = NSRange(self.startIndex..., in: 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]).trimmingCharacters(in: .whitespaces)
|
||||
line = self.replacingOccurrences(
|
||||
of: value,
|
||||
with: "",
|
||||
options: .regularExpression
|
||||
)
|
||||
self = line.trimmingCharacters(in: .whitespaces)
|
||||
return value.trimmingCharacters(in: .whitespaces)
|
||||
if let match = regex.firstMatch(in: self, options: [], range: range) {
|
||||
let matchRange = Range(match.range, in: self)
|
||||
if let range = matchRange {
|
||||
let croppedString = String(self[range])
|
||||
let remainingString = String(self[range.upperBound...])
|
||||
return (croppedString.trimmingCharacters(in: .whitespaces), remainingString.trimmingCharacters(in: .whitespaces))
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
} catch {
|
||||
print("Error creating regex: \(error.localizedDescription)")
|
||||
}
|
||||
|
||||
return ""
|
||||
return ("", self)
|
||||
}
|
||||
|
||||
public func find(pattern: String) -> String {
|
||||
|
||||
@@ -191,14 +191,12 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
var processes: [TopProcess] = []
|
||||
output.enumerateLines { (line, _) -> Void in
|
||||
if line.matches("^\\d+ *[^(\\d)]*\\d+\\.*\\d* *$") {
|
||||
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
|
||||
guard let usage = Double(usageString.filter("01234567890.".contains)) else {
|
||||
let str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidFind = str.findAndCrop(pattern: "^\\d+")
|
||||
let usageFind = pidFind.remain.findAndCrop(pattern: " +[0-9]+.*[0-9]*$")
|
||||
let command = usageFind.remain.trimmingCharacters(in: .whitespaces)
|
||||
let pid = Int(pidFind.cropped) ?? 0
|
||||
guard let usage = Double(usageFind.cropped.filter("01234567890.".contains)) else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -223,13 +223,12 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
var processes: [TopProcess] = []
|
||||
output.enumerateLines { (line, stop) -> Void in
|
||||
if index != 0 {
|
||||
var str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidString = str.findAndCrop(pattern: "^\\d+")
|
||||
let usageString = str.findAndCrop(pattern: "^[0-9,.]+ ")
|
||||
let command = str.trimmingCharacters(in: .whitespaces)
|
||||
|
||||
let pid = Int(pidString) ?? 0
|
||||
let usage = Double(usageString.replacingOccurrences(of: ",", with: ".")) ?? 0
|
||||
let str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidFind = str.findAndCrop(pattern: "^\\d+")
|
||||
let usageFind = pidFind.remain.findAndCrop(pattern: "^[0-9,.]+ ")
|
||||
let command = usageFind.remain.trimmingCharacters(in: .whitespaces)
|
||||
let pid = Int(pidFind.cropped) ?? 0
|
||||
let usage = Double(usageFind.cropped.replacingOccurrences(of: ",", with: ".")) ?? 0
|
||||
|
||||
var name: String? = nil
|
||||
if let app = NSRunningApplication(processIdentifier: pid_t(pid) ) {
|
||||
@@ -525,8 +524,9 @@ public class AverageReader: Reader<[Double]> {
|
||||
return
|
||||
}
|
||||
|
||||
var str = line.trimmingCharacters(in: .whitespaces)
|
||||
let strArr = str.findAndCrop(pattern: "(\\d+(.|,)\\d+ *){3}$").split(separator: " ")
|
||||
let str = line.trimmingCharacters(in: .whitespaces)
|
||||
let strFind = str.findAndCrop(pattern: "(\\d+(.|,)\\d+ *){3}$")
|
||||
let strArr = strFind.cropped.split(separator: " ")
|
||||
guard strArr.count == 3 else {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -383,13 +383,10 @@ public class ProcessReader: Reader<[Disk_process]> {
|
||||
|
||||
var processes: [Disk_process] = []
|
||||
output.enumerateLines { (line, _) -> Void in
|
||||
var str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidString = str.findAndCrop(pattern: "^\\d+")
|
||||
if let range = str.range(of: pidString) {
|
||||
str = str.replacingCharacters(in: range, with: "")
|
||||
}
|
||||
let name = str.findAndCrop(pattern: "^[^ ]+")
|
||||
guard let pid = Int32(pidString) else { return }
|
||||
let str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidFind = str.findAndCrop(pattern: "^\\d+")
|
||||
guard let pid = Int32(pidFind.cropped) else { return }
|
||||
let name = pidFind.remain.findAndCrop(pattern: "^[^ ]+").cropped
|
||||
|
||||
var usage = rusage_info_current()
|
||||
let result = withUnsafeMutablePointer(to: &usage) {
|
||||
|
||||
Reference in New Issue
Block a user