On Wed, May 28, 2008 at 1:19 PM, Ian Ross <
address@hidden> wrote:
- Remove all run-shell-command calls in contrib/battery.lisp and replace
with native file processing.
---
contrib/battery.lisp | 44 ++++++++++++++++++++++++++++++--------------
1 files changed, 30 insertions(+), 14 deletions(-)
diff --git a/contrib/battery.lisp b/contrib/battery.lisp
index 9be6ae2..1f2ac02 100644
--- a/contrib/battery.lisp
+++ b/contrib/battery.lisp
@@ -42,26 +42,42 @@
(defvar *bat-remain-time* nil)
(defvar *bat-prev-time* 0)
+(defun read-battery-file (battery fname)
+ (let ((fields (make-hash-table :test #'equal)))
+ (with-open-file (s (concatenate 'string "/proc/acpi/battery/" battery "/" fname))
+ (do ((line (read-line s nil nil) (read-line s nil nil)))
+ ((null line) fields)
+ (let ((split (cl-ppcre:split ":\\s*" line)))
+ (setf (gethash (string-trim '(#\Space) (car split)) fields)
+ (string-trim '(#\Space) (cadr split))))))))
+
(defun current-battery-charge ()
"Calculate remaining battery charge. Don't make calculation more than once in 15 seconds."
(let ((now (/ (get-internal-real-time) internal-time-units-per-second)))
(when (or (= 0 *bat-prev-time*) (>= (- now *bat-prev-time*) 15))
(setf *bat-prev-time* now)
- (if (string= "no" (string-trim '(#\Newline) (run-shell-command "grep present /proc/acpi/battery/BAT0/state | grep -o -e '\\w*$'" t)))
- (setq *bat-state* nil)
- (let ((remain (parse-integer (run-shell-command "grep remaining\\ capacity /proc/acpi/battery/BAT0/state | sed -n 's/[a-zA-Z\\ \\:]*//gp'" t)))
- (rate (/ (parse-integer (run-shell-command "grep present\\ rate /proc/acpi/battery/BAT0/state | sed -n 's/[a-zA-Z\\ \\:]*//gp'" t)) 60))
- (full (parse-integer (run-shell-command "grep last\\ full\\ capacity /proc/acpi/battery/BAT0/info | sed -n 's/[a-zA-Z\\:\\ ]//gp'" t))))
-
- (setq *bat-remain* (round (/ (* 100 remain) full))
- *bat-state* (string-trim '(#\Newline) (run-shell-command "grep charging\\ state /proc/acpi/battery/BAT0/state | grep -o -e '\\w*$'" t))
- *bat-remain-time* nil)
+ (let ((battery-state (read-battery-file "BAT0" "state"))
+ (battery-info (read-battery-file "BAT0" "info")))
+ (if (string= "no" (gethash "present" battery-state))
+ (setq *bat-state* nil)
+ (let ((charge-state (gethash "charging state" battery-state))
+ (remain (parse-integer (gethash "remaining capacity" battery-state)
+ :junk-allowed t))
+ (rate (/ (or (parse-integer (gethash "present rate" battery-state)
+ :junk-allowed t) 0) 60))
+ (full (parse-integer (gethash "design capacity" battery-info)
+ :junk-allowed t)))
+ (setq *bat-remain* (round (/ (* 100 remain) full))
+ *bat-state* charge-state
+ *bat-remain-time* nil)
- (when (> rate 0)
- (let* ((online (round (/ (if (string= "charging" *bat-state*) (- full remain) remain) rate))))
- (setq *bat-remain-time* (multiple-value-bind (h m)
- (truncate online 60)
- (list h m))))))))))
+ (when (> rate 0)
+ (let* ((online (round (/ (if (string= "charging" *bat-state*)
+ (- full remain) remain)
+ rate))))
+ (setq *bat-remain-time* (multiple-value-bind (h m)
+ (truncate online 60)
+ (list h m)))))))))))
(defun fmt-bat-charge (ml)
"Returns a string representing the remaining battery charge (for laptop users.)"
--
1.5.2.5
_______________________________________________
Stumpwm-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/stumpwm-devel