summaryrefslogtreecommitdiff
path: root/view-conf.el
blob: ca08409956c6e323ef2fd375e02967a54780b9fc (plain)
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
177
178
179
180
181
182
;;; view-conf.el --- My configurations to view things -*- lexical-binding: t; -*-

;;;###autoload
(defun durand-view-timers-or-temps (&optional arg)
  "View the list of timers or view the CPU temperature info.
If ARG is nil, view the list of timers.
If ARG is '(4), view the information about CPU temperature and
fans speed and some others.
If ARG is '(16), view the battery information."
  (interactive "P")
  (cond
   ((null arg)
    (let (fan-speed
          cpu-die-temperature
          remain
          full-capacity
          fullp
          charging
          cycle
          condition
          connected
          battery-temp)
      (with-temp-buffer
;;; NOTE: First fans information
        (insert (funcall
                 (plist-get (car (auth-source-search :host "local-computer"))
                            :secret)))
        (call-process-region
         nil nil "sudo"
         nil t nil "-S" "powermetrics"
         "-i1" "-n1" "-ssmc")
        (goto-char (point-min))
        (search-forward "Fan" nil t)
        (setf fan-speed
              (progn (re-search-forward "[[:digit:]]+" (line-end-position) t)
                     (string-to-number (match-string 0)))
              cpu-die-temperature
              (progn (re-search-forward "temperature: \\([[:digit:]]+\\.[[:digit:]]+\\)" nil t)
                     (string-to-number (match-string 1))))
;;; NOTE: Now battery charge information
        (erase-buffer)
        (call-process "system_profiler" nil t nil
                      "SPPowerDataType")
        (goto-char (point-min))
        (re-search-forward "Fully Charged: \\(.+\\)$" nil t)
        (setf fullp (match-string-no-properties 1))
        (re-search-forward "Charging: \\(.+\\)$" nil t)
        (setf charging (match-string-no-properties 1))
        (re-search-forward "Full Charge Capacity (mAh): \\([[:digit:]]+\\)$" nil t)
        (setf full-capacity (string-to-number (match-string-no-properties 1)))
        (re-search-forward "State of Charge (%): \\([[:digit:]]+\\)" nil t)
        (setf remain (string-to-number (match-string-no-properties 1)))
        (re-search-forward "Cycle Count: \\(.+\\)$" nil t)
        (setf cycle (string-to-number (match-string-no-properties 1)))
        (re-search-forward "Condition: \\(.+\\)$" nil t)
        (setf condition (match-string-no-properties 1))
        (re-search-forward "Connected: \\(.+\\)$" nil t)
        (setf connected (match-string-no-properties 1))
;;; NOTE: Now battery temperature
        (erase-buffer)
        (call-process "ioreg" nil t nil "-n" "AppleSmartBattery" "-r")
        (goto-char (point-min))
        (re-search-forward "Temperature..=." nil t)
        (re-search-forward "[[:digit:]]+" nil t)
        (setf battery-temp
              (/ (string-to-number (match-string-no-properties 0))
                 100.0)))
      (message
       (concat
        (format "fan: %d, temp: %s, battery temp: %.2f"
                fan-speed cpu-die-temperature battery-temp)
        "\n"
        (format "Full: %d, remaining: %d%s, fullp: %s, charging: %s, connected: %s, cycles: %d, condition: %s"
                full-capacity remain "%%" fullp charging connected cycle condition)))))
   ((equal arg (list 4)) (list-timers))
   (t
    (user-error "Unsupported ARG: %S" arg))))

(define-obsolete-function-alias 'durand-view-timers 'durand-view-timers-or-temps
  "2020-09-02" "View the list of timers.")

;;;###autoload
(defun durand-view-process (&optional arg)
  "View the list of processes"
  (interactive "P")
  (if arg
      (proced)
    (message "%s" (process-list))))

;;; My map for viewing

;;;###autoload
(defvar durand-view-map
  ;; Against every good principle I guess...
  (let ((map (list 'keymap "View")))
    (define-key map (vector ?v) 'view-mode)
    (define-key map (vector ?p) #'durand-view-process)
    (define-key map (vector ?t) #'durand-view-timers-or-temps)
    (define-key map (vector ?e) #'eshell)
    map)
  "The keymap that is related to my custom functions about viewing.")

;;; About view-mode

(require 'view)

(define-key view-mode-map (vector 108) #'recenter-top-bottom)

(define-key view-mode-map (vector ?j) #'View-scroll-line-forward)
(define-key view-mode-map (vector ?k) #'View-scroll-line-backward)
(define-key global-map (vector 3 118) durand-view-map)

;;; Open things

;;;###autoload
(defvar durand-open-targets
  (list (list "safari" "open" "-a" "Safari" -1)
        (list "terminal" "open" "-a" "Terminal" -1))
  "Targets to open by `durand-open-object'.

This is an association list whose elements have `car' equal to
the choice presented to the user, and have `cdr' a list that will
be passed to `make-process'.")

;;;###autoload
(defun durand-open-object (&optional arg)
  "Open something.

What can be opened is controlled by the variable
`durand-open-targets'.

If ARG is non-nil, and if the chosen target contains `-1' in the
command line options, then read a string to replace that -1."
  (interactive "P")
  (let* ((targets durand-open-targets)
         (choice (completing-read "Open: " targets nil t))
         (commands (cdr (assoc choice targets #'string=)))
         (contain-minus-one-p (memq -1 commands))
         (extra (cond
                 ((and arg contain-minus-one-p)
                  (read-string "Extra argument: "))))
         temp)
    (cond
     (contain-minus-one-p
      (setq commands (progn
                       (while (consp commands)
                         (cond
                          ((eq (car commands) -1)
                           (setq temp (cons extra temp)))
                          ((setq temp (cons (car commands) temp))))
                         (setq commands (cdr commands)))
                       (reverse temp)))))
    (setq conmmands (delq nil commands))
    (make-process :name "durand-open"
                  :buffer nil
                  :command commands)))

;;; C-c o => 3 111
(define-key global-map (vector 3 111) #'durand-open-object)

;;; Convert downloaded videos from youtube to the URL again and copy
;;; the result.

;;;###autoload
(defun durand-convert-youtube-video-to-url (video-name)
  "Convert VIDEO-NAME to its original url."
  (interactive (list (read-string "Video name: ")))
  (cond
   ((string-match "\\.[^.]+$" video-name)
    ;; with extension
    (kill-new
     (concat
      "https://www.youtube.com/watch?v="
      (substring-no-properties
       video-name
       (- (match-beginning 0) 11)
       (match-beginning 0)))))
   (t
    ;; no extension
    (kill-new
     (concat "https://www.youtube.com/watch?v="
             (substring-no-properties video-name -11))))))