1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-09 16:48:38 +00:00

added speed units for network (#440)

Co-authored-by: akonst <akonst@cqg.com>
This commit is contained in:
ak0nst 2022-05-11 13:57:17 +04:00 committed by GitHub
parent 36bf749a46
commit a879498e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 15 deletions

View File

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

View File

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

View File

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

View File

@ -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
},
```