mem.lisp 915 B

123456789101112131415161718192021222324
  1. (in-package :stumpwm)
  2. (defun get-proc-fd-field (s field)
  3. (if s
  4. (do ((line (read-line s nil nil) (read-line s nil nil)))
  5. ((null line) nil)
  6. (let ((split (cl-ppcre:split "\\s*:\\s*" line)))
  7. (when (string= (car split) field) (return (cadr split)))))
  8. ""))
  9. (defun mem-usage ()
  10. "Returns amount of available memory."
  11. (with-open-file (file #P"/proc/meminfo" :if-does-not-exist nil)
  12. (read-from-string (get-proc-fd-field file "MemAvailable"))))
  13. (defun fmt-mem-available (mem &optional color)
  14. "Returns a string representing the current available memory."
  15. (let* ((available (truncate (/ mem 1000))))
  16. (if color
  17. (cond ((< available (* 1024 8)) (format nil "^[^B^3*MEM: ~4D MB^]" available))
  18. ((< available (* 1024 4)) (format nil "^[^B^1*MEM: ~4D MB^]" available))
  19. (t (format nil "MEM: ~4D MB" available)))
  20. (format nil "MEM: ~4D MB" available))))