From d91bc785a7d2669014dd6509bcf6e4816756806c Mon Sep 17 00:00:00 2001 From: jao Date: Wed, 30 Sep 2020 02:38:49 +0100 Subject: improvements on the above --- net/jao-proton-utils.el | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/net/jao-proton-utils.el b/net/jao-proton-utils.el index f6d7f3b..5c817ac 100644 --- a/net/jao-proton-utils.el +++ b/net/jao-proton-utils.el @@ -58,8 +58,10 @@ (setq mode-name "proton-vpn") (read-only-mode 1)) +(defvar jao-proton-vpn--buffer "*pvpn*") + (defun jao-proton-vpn--do (things &optional buffer) - (let ((b (or buffer (pop-to-buffer (get-buffer-create "*pvpn*"))))) + (let ((b (or buffer (pop-to-buffer (get-buffer-create jao-proton-vpn--buffer))))) (let ((inhibit-read-only t) (cmd (format "sudo protonvpn %s" things))) (delete-region (point-min) (point-max)) @@ -74,11 +76,12 @@ (jao-proton-vpn--do "s")) (defun proton-vpn--get-status () - (with-temp-buffer - (jao-proton-vpn--do "s" (current-buffer)) - (goto-char (point-min)) - (when (re-search-forward "^Status: *\\(.+\\)$" nil t) - (match-string 1)))) + (or (when-let ((b (get-buffer jao-proton-vpn--buffer))) + (with-current-buffer b + (goto-char (point-min)) + (when (re-search-forward "^Status: *\\(.+\\)$" nil t) + (match-string-no-properties 1)))) + "Disconnected")) ;;;###autoload (defun proton-vpn-connect (cc) @@ -87,11 +90,17 @@ (jao-proton-vpn--do (if cc (format "c --cc %s" cc) "c --sc")) (proton-vpn-status))) +(defun proton-vpn-reconnect () + (interactive) + (jao-proton-vpn--do "r")) + ;;;###autoload (defun proton-vpn-maybe-reconnect () + (interactive) (when (string= "Connected" (proton-vpn--get-status)) - (with-temp-buffer - (jao-proton-vpn--do "r" (current-buffer))))) + (message "Reconnecting VPN...") + (proton-vpn-reconnect) + (message "Reconnecting VPN... done"))) ;;;###autoload (defun proton-vpn-disconnect () @@ -105,6 +114,7 @@ (define-key map [?n] 'next-line) (define-key map [?p] 'previous-line) (define-key map [?g] 'proton-vpn-status) + (define-key map [?r] 'proton-vpn-reconnect) (define-key map [?d] (lambda () (interactive) (when (y-or-n-p "Disconnect?") -- cgit v1.2.3