Development discussion of WireGuard
 help / color / mirror / Atom feed
* [PATCH] Always show "Last Handshake" and "Rx / Tx Bytes"
@ 2022-03-18 12:34 Kevin Seidel
  0 siblings, 0 replies; only message in thread
From: Kevin Seidel @ 2022-03-18 12:34 UTC (permalink / raw)
  To: wireguard; +Cc: moogle19

From: moogle19 <k.seidel@q1.eu>

---
 Sources/WireGuardApp/UI/TunnelViewModel.swift | 37 +++++++++----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/Sources/WireGuardApp/UI/TunnelViewModel.swift b/Sources/WireGuardApp/UI/TunnelViewModel.swift
index b65c8cc..1c578c5 100644
--- a/Sources/WireGuardApp/UI/TunnelViewModel.swift
+++ b/Sources/WireGuardApp/UI/TunnelViewModel.swift
@@ -309,7 +309,7 @@ class TunnelViewModel {
                 scratchpad[.preSharedKey] = preSharedKey
             }
             if !config.allowedIPs.isEmpty {
-                scratchpad[.allowedIPs] = config.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", ")
+                scratchpad[.allowedIPs] =  config.allowedIPs.map(\.stringRepresentation).joined(separator: ", ")
             }
             if let endpoint = config.endpoint {
                 scratchpad[.endpoint] = endpoint.stringRepresentation
@@ -317,15 +317,10 @@ class TunnelViewModel {
             if let persistentKeepAlive = config.persistentKeepAlive {
                 scratchpad[.persistentKeepAlive] = String(persistentKeepAlive)
             }
-            if let rxBytes = config.rxBytes {
-                scratchpad[.rxBytes] = prettyBytes(rxBytes)
-            }
-            if let txBytes = config.txBytes {
-                scratchpad[.txBytes] = prettyBytes(txBytes)
-            }
-            if let lastHandshakeTime = config.lastHandshakeTime {
-                scratchpad[.lastHandshakeTime] = prettyTimeAgo(timestamp: lastHandshakeTime)
-            }
+            scratchpad[.rxBytes] = config.rxBytes?.prettyBytes() ?? "-"
+            scratchpad[.txBytes] = config.txBytes?.prettyBytes() ?? "-"
+            scratchpad[.lastHandshakeTime] = config.lastHandshakeTime?.prettyTimeAgo() ?? "-"
+
             return scratchpad
         }
 
@@ -624,30 +619,34 @@ class TunnelViewModel {
     }
 }
 
-private func prettyBytes(_ bytes: UInt64) -> String {
-    switch bytes {
+private extension UInt64 {
+  func prettyBytes() -> String {
+    switch self {
     case 0..<1024:
-        return "\(bytes) B"
+      return "\(self) B"
     case 1024 ..< (1024 * 1024):
-        return String(format: "%.2f", Double(bytes) / 1024) + " KiB"
+      return String(format: "%.2f", Double(self) / 1024) + " KiB"
     case 1024 ..< (1024 * 1024 * 1024):
-        return String(format: "%.2f", Double(bytes) / (1024 * 1024)) + " MiB"
+      return String(format: "%.2f", Double(self) / (1024 * 1024)) + " MiB"
     case 1024 ..< (1024 * 1024 * 1024 * 1024):
-        return String(format: "%.2f", Double(bytes) / (1024 * 1024 * 1024)) + " GiB"
+      return String(format: "%.2f", Double(self) / (1024 * 1024 * 1024)) + " GiB"
     default:
-        return String(format: "%.2f", Double(bytes) / (1024 * 1024 * 1024 * 1024)) + " TiB"
+      return String(format: "%.2f", Double(self) / (1024 * 1024 * 1024 * 1024)) + " TiB"
     }
+  }
 }
 
-private func prettyTimeAgo(timestamp: Date) -> String {
+private extension Date {
+  func prettyTimeAgo() -> String {
     let now = Date()
-    let timeInterval = Int64(now.timeIntervalSince(timestamp))
+    let timeInterval = Int64(now.timeIntervalSince(self))
     switch timeInterval {
     case ..<0: return tr("tunnelHandshakeTimestampSystemClockBackward")
     case 0: return tr("tunnelHandshakeTimestampNow")
     default:
         return tr(format: "tunnelHandshakeTimestampAgo (%@)", prettyTime(secondsLeft: timeInterval))
     }
+  }
 }
 
 private func prettyTime(secondsLeft: Int64) -> String {
-- 
2.35.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-21 19:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-18 12:34 [PATCH] Always show "Last Handshake" and "Rx / Tx Bytes" Kevin Seidel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).