summaryrefslogtreecommitdiffhomepage
path: root/lib/eos/jao-dirmon.el
blob: a2535ad8c4259f509e541e601d504e3d15e1e17e (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
;;; jao-dirmon.el --- little utility to monitor disk usage  -*- lexical-binding: t; -*-

;; Copyright (C) 2022  jao

;; Author: jao <mail@jao.io>
;; Keywords: tools

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; What directories suddenly increased their size?

;;; Code:

(require 'multisession)
(require 'jao-shell)

(define-multisession-variable jao-dirmon-last '())

(defun jao-dirmon-dirs ()
  (jao-shell-cmd-lines "find ~ -mindepth 2 -maxdepth 3 -type d"))

(defun jao-dirmon-sizes ()
  (mapcar (lambda (f)
            (let ((x (split-string (jao-shell-string "du -b -s" f))))
              (cons (cadr x) (/ (string-to-number (car x)) 1024 1024))))
          (jao-dirmon-dirs)))

(defvar jao-dirmon-threshold 100)
(defvar jao-dirmon-last-delta nil)

(defun jao-dirmon--show-deltas (old current deltas)
  (with-temp-buffer
    (insert "High deltas since " (car old) "\n------------------\n\n")
    (dolist (d deltas)
      (insert d "\n"))
    (beginning-of-buffer)
    (pop-to-buffer (current-buffer) nil t)
    (when (y-or-n-p "Save current state?")
      (setf (multisession-value jao-dirmon-last)
            (cons (current-time-string) current)))))

;;;###autoload
(defun jao-dirmon-report ()
  (interactive)
  (let ((old (multisession-value jao-dirmon-last))
        (current (jao-dirmon-sizes))
        (high ()))
    (dolist (c current)
      (let ((d (- (cdr c) (alist-get (car c) old 0 nil #'string=))))
        (when (> d jao-dirmon-threshold)
          (push c high))))
    (setq jao-dirmon-last-delta high)
    (jao-dirmon--show-deltas old current jao-dirmon-last-delta)
    jao-dirmon-last-delta))

(provide 'jao-dirmon)
;;; jao-dirmon.el ends here