-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpelpa-mode.el
176 lines (152 loc) · 5.88 KB
/
pelpa-mode.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;; pelpa-mode.el -- popkit elpa monitor mode-line
;; Copyright (C) 2016 Aborn Jiang
;; Author: Aborn Jiang <[email protected]>
;; Version: 1.0
;; Keywords: popkit, elpa, monitor
;; Homepage: https://github.com/popkit/pelpa-mode
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; for popkit elpa monitor
;; 监控 elpa.popkit.org的后台运行情况
(require 'cl-lib)
(require 'package)
(require 'lisp-mnt)
(require 'json)
;; 引入widget库
(require 'widget)
(require 'cus-edit)
(eval-when-compile
(require 'wid-edit))
;; 注意:这个pelpa-mode-map的定义一定要放在 define-derived-mode 之前
(defvar pelpa-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map widget-keymap)
(define-key map "r" 'pm/monitor) ;; refresh status
map)
"major key map for pelpa-mode")
(define-derived-mode pelpa-mode nil "pelpa-mode"
"Major for popkit elpa site (https://elpa.popkit.org/#/) running monitor."
(widen)
(setq buffer-read-only t)
:group 'pelpa-mode)
(defface pm/chart-bar-face
'((t . (:background "orange" :foreground "black")))
"face for chart bar" :group 'pelpa-mode)
(defface pm/default-button-green
'((t . (:background "green" :foreground "black")))
"default button" :group 'pelpa-mode)
(defcustom pm/build-status-url "http://pelpa.popkit.org/elpa/build/ajaxBuildStatus.json"
"build status url for ajax"
:group 'pelpa-mode
:type 'string)
(defcustom pm/pelpa-buffer-name "*pelpa*"
"the display buffer name"
:group 'pelpa-mode
:type 'string)
(defun pm/decode-region (arg)
"decode current buffer"
(interactive "P")
(decode-coding-region (point-min) (point-max) 'utf-8))
;; 读取http结果中的json值
(defun pm/read-http-data-as-json (http-data)
(with-temp-buffer
(insert http-data)
(goto-char (point-min))
(re-search-forward "^$")
(json-read)))
;; 将时间转成北京时间
(defun pm/convert-to-ut+8 (origin)
(if (stringp origin)
(let* ((time (date-to-time origin)))
(format-time-string "%Y-%m-%d %H:%M:%S %a. Week %W" time))
origin))
;; 解析出http返回的header中的timestamp信息
;; Date: Sat, 18 Jun 2016 14:43:59 GMT
(defun pm/read-http-timestamp-string (http-data)
(with-temp-buffer
(insert http-data)
(goto-char (point-min))
(let* ((start-point (+ 1 (search-forward "Date:")))
(end-point (search-forward "\n"))
(timestamp-string (buffer-substring start-point end-point)))
(pm/convert-to-ut+8 timestamp-string))
))
(defun pm/to-string (origin)
(cond ((numberp origin) (number-to-string origin))
(t origin)))
(defun pm/pelpa-mode-kill ()
(interactive)
(with-current-buffer pm/pelpa-buffer-name
(let ((buffer (current-buffer)))
(unless (one-window-p)
(when (get-buffer-window buffer)
(delete-window)))
(kill-buffer buffer)))
(message "%s buffer was killed!" pm/pelpa-buffer-name))
;; 获得构建状态信息
(defun pm/ajax-build-status ()
"get build status info"
(let* ((buffer (url-retrieve-synchronously pm/build-status-url))
(http-content nil)
(json-data nil)
(result-data nil))
(if (not buffer)
(error "请求%服务失败,请重试!" pm/build-status-url))
(with-current-buffer buffer
(unless (= 200 (url-http-parse-response)))
(setq http-content (decode-coding-string (buffer-string) 'utf-8))
(setq json-data (pm/read-http-data-as-json http-content))
(with-temp-buffer
(insert (format "%s" (pm/read-http-timestamp-string http-content)))
;; 插入json的key value值
(dolist (item (list 'currentRun 'percent 'percentDesc))
(insert
(format "\n%s:%s"
(symbol-name item)
(pm/to-string (assoc-default item json-data)))))
(setq result-data (buffer-string))))
result-data))
(defun pm/go-github-pelpa-repo (button)
"go elpa site"
(browse-url "https://github.com/popkit/pelpa"))
;; 显示所有的监控信息
(defun pm/monitor (arg)
"ajax pelpa building status"
(interactive "P")
(let* ((pelpa-buffer (get-buffer-create pm/pelpa-buffer-name))
(ajax-status-content nil))
(with-current-buffer pelpa-buffer
(pelpa-mode)
(setq buffer-read-only nil)
(erase-buffer) ;; 先清空原有的内容
(setq ajax-status-content (pm/ajax-build-status))
(put-text-property 0 9 'font-lock-faces 'pm/chart-bar-face ajax-status-content)
(insert ajax-status-content)
(insert "\n")
(insert-button "pelpa@github"
;;'face 'pm/default-button-green
'action 'pm/go-github-pelpa-repo)
(insert "\n")
(widget-create 'push-button
:format "查看历史访问的统计数据 %[ %t %]\n"
:button-face 'custom-button
:help-echo "访问统计链接"
:action (lambda (wid &rest ignore)
(browse-url "http://pelpa.popkit.org/elpa/data/index.html"))
:tag "点击")
(setq buffer-read-only t)
))
(unless (get-buffer-window pm/pelpa-buffer-name)
(if (one-window-p) ;; 只有一个window里,在当前window里显示buffer
(switch-to-buffer pm/pelpa-buffer-name)
(switch-to-buffer-other-window pm/pelpa-buffer-name))))
(provide 'pelpa-mode)