From a879498e4cc7eb9bf45fb8224582504b9198996c Mon Sep 17 00:00:00 2001 From: ak0nst Date: Wed, 11 May 2022 13:57:17 +0400 Subject: [PATCH] added speed units for network (#440) Co-authored-by: akonst --- MTMR/ItemsParsing.swift | 5 ++-- MTMR/TouchBarController.swift | 4 +-- MTMR/Widgets/NetworkBarItem.swift | 49 ++++++++++++++++++++++++------- README.md | 3 +- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index ca8305a..3e31c67 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -274,7 +274,7 @@ enum ItemType: Decodable { case nightShift case dnd case pomodoro(workTime: Double, restTime: Double) - case network(flip: Bool) + case network(flip: Bool, units: String) case darkMode case swipe(direction: String, fingers: Int, minOffset: Float, sourceApple: SourceProtocol?, sourceBash: SourceProtocol?) case upnext(from: Double, to: Double, maxToShow: Int, autoResize: Bool) @@ -424,7 +424,8 @@ enum ItemType: Decodable { case .network: let flip = try container.decodeIfPresent(Bool.self, forKey: .flip) ?? false - self = .network(flip: flip) + let units = try container.decodeIfPresent(String.self, forKey: .units) ?? "dynamic" + self = .network(flip: flip, units: units) case .darkMode: self = .darkMode diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 155b028..1ff8095 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -382,8 +382,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = DnDBarItem(identifier: identifier) case let .pomodoro(workTime: workTime, restTime: restTime): barItem = PomodoroBarItem(identifier: identifier, workTime: workTime, restTime: restTime) - case let .network(flip: flip): - barItem = NetworkBarItem(identifier: identifier, flip: flip) + case let .network(flip: flip, units: units): + barItem = NetworkBarItem(identifier: identifier, flip: flip, units: units) case .darkMode: barItem = DarkModeBarItem(identifier: identifier) case let .swipe(direction: direction, fingers: fingers, minOffset: minOffset, sourceApple: sourceApple, sourceBash: sourceBash): diff --git a/MTMR/Widgets/NetworkBarItem.swift b/MTMR/Widgets/NetworkBarItem.swift index 57de0bf..ad44ba3 100644 --- a/MTMR/Widgets/NetworkBarItem.swift +++ b/MTMR/Widgets/NetworkBarItem.swift @@ -13,9 +13,11 @@ class NetworkBarItem: CustomButtonTouchBarItem, Widget { static var identifier: String = "com.toxblh.mtmr.network" private let flip: Bool + private let units: String - init(identifier: NSTouchBarItem.Identifier, flip: Bool = false) { + init(identifier: NSTouchBarItem.Identifier, flip: Bool = false, units: String) { self.flip = flip + self.units = units super.init(identifier: identifier, title: " ") startMonitoringProcess() } @@ -86,15 +88,42 @@ class NetworkBarItem: CustomButtonTouchBarItem, Widget { func getHumanizeSize(speed: UInt64) -> String { let humanText: String - - if speed < 1024 { - humanText = String(format: "%.0f", Double(speed)) + " B/s" - } else if speed < (1024 * 1024) { - humanText = String(format: "%.1f", Double(speed) / 1024) + " KB/s" - } else if speed < (1024 * 1024 * 1024) { - humanText = String(format: "%.1f", Double(speed) / (1024 * 1024)) + " MB/s" - } else { - humanText = String(format: "%.2f", Double(speed) / (1024 * 1024 * 1024)) + " GB/s" + + func speedB(speed: UInt64)-> String { + return String(format: "%.0f", Double(speed)) + " B/s" + } + + func speedKB(speed: UInt64)-> String { + return String(format: "%.1f", Double(speed) / 1024) + " KB/s" + } + + func speedMB(speed: UInt64)-> String { + return String(format: "%.1f", Double(speed) / (1024 * 1024)) + " MB/s" + } + + func speedGB(speed: UInt64)-> String { + return String(format: "%.2f", Double(speed) / (1024 * 1024 * 1024)) + " GB/s" + } + + switch self.units { + case "B/s": + humanText = speedB(speed: speed) + case "KB/s": + humanText = speedKB(speed: speed) + case "MB/s": + humanText = speedMB(speed: speed) + case "GB/s": + humanText = speedGB(speed: speed) + default: + if speed < 1024 { + humanText = speedB(speed: speed) + } else if speed < (1024 * 1024) { + humanText = speedKB(speed: speed) + } else if speed < (1024 * 1024 * 1024) { + humanText = speedMB(speed: speed) + } else { + humanText = speedGB(speed: speed) + } } return humanText diff --git a/README.md b/README.md index 996f419..28b8bd1 100644 --- a/README.md +++ b/README.md @@ -347,7 +347,8 @@ To close a group, use the button: ```js { "type": "network", - "flip": true + "flip": true, + "units": "dynamic" // or B/s, KB/s, MB/s, GB/s }, ```