forked from sean-hut/eping
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eping.el
110 lines (90 loc) · 3.81 KB
/
eping.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;;; eping.el --- Ping websites to check internet connectivity -*- lexical-binding: t -*-
;; Copyright © 2020 Sean Hutchings <[email protected]>
;; Copyright © 2022 Jen-Chieh Shen <[email protected]>
;; Author: Sean Hutchings <[email protected]>
;; Maintainer: Jen-Chieh Shen <[email protected]>
;; Created: 2020-10-16
;; Keywords: comm, processes, terminals, unix
;; Package-Requires: ((emacs "25.1"))
;; Version: 0.1.1-git
;; Homepage: https://github.com/elp-revive/eping
;; License: BSD Zero Clause License (SPDX: 0BSD)
;; Permission to use, copy, modify, and/or distribute this software
;; for any purpose with or without fee is hereby granted.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;; Change Log: For all notable changes see CHANGELOG.md
;;; Commentary:
;; Ping websites to check internet connectivity.
;;
;; Documentation: https://sean-hut.github.io/eping/
;;; Code:
(require 'subr-x)
(defvar eping-domain-options
'("wikipedia.org" "startpage.com" "gnu.org")
"List of domains that Eping will present as options.")
(defvar eping-number-pings-options '("5" "1" "10" "15" "20")
"List of how many times to ping the domain.
Eping will present this as list to select from for users.")
(defcustom eping-complete-hook nil
"Hook runs after eping is completed."
:type 'hook
:group 'eping)
(defcustom eping-success-hook nil
"Hook runs after eping is succeed."
:type 'hook
:group 'eping)
(defcustom eping-fail-hook nil
"Hook runs after eping is failed."
:type 'hook
:group 'eping)
(defun eping--sentinel-minibuffer-output (process event)
"Output the process name and event with minibuffer.
PROCESS is the process the sentinel is watching.
EVENT is the processes change event."
(message "%s %s" process (string-trim-right event))
(eping--run-callback event))
(defun eping--sentinel-espeak-output (process event)
"Output the process name and event with eSpeak.
PROCESS is the process the sentinel is watching.
EVENT is the processes change event."
(let* ((espeak-text (format "%s %s" process event))
(command (list "espeak" espeak-text)))
(make-process :name "eping-sentinel-espeak-output"
:command command))
(eping--run-callback event))
(defun eping--run-callback (event)
"Execute callback from EVENT."
(if (string-match-p "finished" event)
(run-hooks 'eping-success-hook)
(run-hooks 'eping-fail-hook))
(run-hooks 'eping-completed-hook))
;;;###autoload
(defun eping (domain number-pings &optional speak)
"Check internet connectivity with ping.
DOMAIN is the domain to ping.
NUMBER-PINGS is how many times to ping the domain.
With prefix arg SPEAK, the output is spoken by espeak."
(interactive
(list (completing-read "Domain to ping: " eping-domain-options nil t)
(completing-read "Number of pings: " eping-number-pings-options nil t)
current-prefix-arg))
(let* ((number-pings (format "%s" number-pings)) ; ensure string
(command (list "ping" (if (eq system-type 'windows-nt)
"-n"
"-c")
number-pings domain)))
(make-process :name "eping"
:command command
:sentinel (if speak
'eping--sentinel-espeak-output
'eping--sentinel-minibuffer-output))))
(provide 'eping)
;;; eping.el ends here