Development discussion of WireGuard
 help / color / mirror / Atom feed
From: Kevin Seidel <k.seidel@q1.eu>
To: wireguard@lists.zx2c4.com
Cc: moogle19 <k.seidel@q1.eu>
Subject: [PATCH] Always show "Last Handshake" and "Rx / Tx Bytes"
Date: Fri, 18 Mar 2022 13:34:26 +0100	[thread overview]
Message-ID: <20220318123426.12414-1-k.seidel@q1.eu> (raw)

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


                 reply	other threads:[~2022-03-21 19:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220318123426.12414-1-k.seidel@q1.eu \
    --to=k.seidel@q1.eu \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).