[comp.emacs] FREE-SPACE.EL -- Show disk space free in mode line.

Ram-Ashwin@cs.yale.edu (Ashwin Ram) (01/11/89)

I have a hack to continuously display the free disk space (using /bin/df or
/com/lvolfs or whatever).  Typing M-x free-space-checker gives you something
like "21% free" or "79% used" in your mode line (along with the day-date and
Mail notification).

To use:
        (autoload "free-space-checker" "free-space" nil t)
        M-x free-space-checker  - interactive
        (free-space-checker)    - in your ~/.emacs file.

There are two files, free-space.el (with the GNU Emacs lisp code) and
free-space-checker (a shell script to output the info to be put into the mode
line).

Comments and improvements are welcome.

-- Ashwin.

ARPA:    Ram-Ashwin@cs.yale.edu
UUCP:    {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin
BITNET:  Ram@yalecs


--- --- --- --- cut here, save in free-space.el --- --- --- ---
;; FREE-SPACE.EL -- Show disk space free in mode line.
;; 
;; This file is not part of the GNU Emacs distribution (yet).
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.
;;
;; Everyone is granted permission to copy, modify and redistribute
;; this file, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.
;;
;; Comments, corrections, and improvements should be sent to:
;;
;;     Ashwin Ram
;;
;;     ARPA:   Ram-Ashwin@cs.yale.edu
;;     UUCP:   {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin
;;     BITNET: Ram@yalecs
;;
;;
;; MODIFICATION HISTORY:
;;
;; 01/08/89 Ashwin Ram <Ram-Ashwin@cs.yale.edu>
;;          Initial release.

(defvar free-space-checker-program
   "free-space-checker"
   "\
*Program to check if there are new bboard messages to be read.  Should produce
something like \"65%\" or \"65% free\" or \"35% used\" on standard output.")

(defvar free-space-checker-process nil)
(defvar free-space-checker-string nil)

(defun free-space-checker ()
   "\
Start a background process to display \"nn% free\" or \"nn% used\" in the mode line."
   (interactive)
   (if (and free-space-checker-process
            (eq (process-status free-space-checker-process) 'run))
       (message "You already have an free-space-checker process running.")
       (save-excursion
          (if free-space-checker-process
              (delete-process free-space-checker-process))
	  (or global-mode-string (setq global-mode-string '("")))
	  (or (memq 'free-space-checker-string global-mode-string)
	      (setq global-mode-string
		    (append global-mode-string '(free-space-checker-string))))
          (setq free-space-checker-process (start-process "free-space-checker" nil free-space-checker-program))
          (process-kill-without-query free-space-checker-process)
	  (set-process-sentinel free-space-checker-process 'free-space-checker-sentinel)
	  (set-process-filter free-space-checker-process 'free-space-checker-filter))))

(defun free-space-checker-sentinel (proc reason)
   (or (eq (process-status proc) 'run)
       (setq free-space-checker-string ""))
   (force-mode-line-update))

(defun free-space-checker-filter (proc string)
   (setq free-space-checker-string (concat " " string))
   (force-mode-line-update))

(defun force-mode-line-update ()
   "This is swiped from time.el and really should be part of GNU Emacs."
  ;; Force redisplay of all buffers' mode lines to be considered.
  (save-excursion (set-buffer (other-buffer)))
  (set-buffer-modified-p (buffer-modified-p))
  ;; Do redisplay right now, if no input pending.
  (sit-for 0))
--- --- --- --- cut here, end of free-space.el --- --- --- ---

Here are a couple of sample free-space-checker files:

For the Apollos:
--- --- --- --- cut here, save in free-space-checker --- --- --- ---
#!/com/sh
/com/ppri -lo 1 -hi 1
while ((true)) do
    lvolfs | fpat -p [0-9] | chpat " *{[0-9]*} *{[0-9]*}? *{[0-9]*} *[0-9]*?*@n" "@3% free"
    sleep 300
enddo
--- --- --- --- cut here, end of free-space-checker --- --- --- ---

For Unix:
--- --- --- --- cut here, save in free-space-checker --- --- --- ---
#!/bin/csh
while (1)
    df $HOME | grep "[0-9]" | sed -e "s/ *[^ ]* *[^ ]* *[^ ]* *[^ ]* *\([0-9]*%\) .*$/\1 used/"
    sleep 300
end
--- --- --- --- cut here, end of free-space-checker --- --- --- ---