From: Deniz Dogan <deniz.a.m.dogan@gmail.com>
To: Thomas Dean <tdean9db@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Writing a Lisp to show the capacity of the battery of the laptop
Date: Tue, 22 Feb 2011 16:33:56 +0100 [thread overview]
Message-ID: <AANLkTinz6TsEu_YMed5tRw6=RgEAcmTcM=BWHLTkjiiP@mail.gmail.com> (raw)
In-Reply-To: <87r5b1phrr.fsf@gmail.com>
2011/2/21 Thomas Dean <tdean9db@gmail.com>:
>
> Hi,
>
> I'm using Archlinux. I want to write a lisp to show the remaining
> capacity of the battery of the laptop. It is expected to work like
> this:
>
> 1. Read from /proc/acpi/battery/BAT0/state and obtain the remaining
> capacity of the battery.
> 2. Read from /proc/acpi/battery/BAT0/info and obtain the full capacity
> of the battery.
> 3. By calculating remaining / full, get the percentage and print it.
>
> I writing the following lisp but it seems that it doesn't work.
>
> ;--------------------------------LISP---------------------------------
>
> ; get remaining capacity
> (shell-command
> "cat /proc/acpi/battery/BAT0/state | grep \"remaining capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
> "td-battery-temp-buffer")
> (set-buffer "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
> (erase-buffer)
>
> ; get full capacity
> (shell-command
> "cat /proc/acpi/battery/BAT0/info | grep \"last full capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
> "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
>
> ; calculate percentage
> (message (concat "Remaining Capacity: " (number-to-string td-battery-remaining)
> "\nFull Capacity: " (number-to-string td-battery-full)))
>
> ; kill temporary buffer
> (set-buffer-modified-p nil)
> (kill-buffer)
>
> ;----------------------LISP END-------------------------------
>
> I'm new to lisp :-) So it maybe looks ugly.
>
> Could you please show me a good lisp that can finish this task?
>
Your code is unorganized and buffer-centric for no reason. Below is
some code I wrote, which I can't test at the moment, but it should
give you a few hints. By the way, try M-x battery in Emacs.
(defun bat-remaining ()
"Return the battery's remaining capacity."
(let ((result (shell-command "yada yada")))
(string-to-number result)))
(defun bat-full ()
"Return the battery's full capacity."
(let ((result (shell-command "blah blah")))
(string-to-number result)))
(defun bat-percent ()
"Return the remaining amount of battery power as an integer."
(let ((remaining (float (bat-remaining)))
(full (float (bat-full))))
(round (* (/ remaining full) 100))))
(defun bat-information ()
"Echo information about the battery."
(interactive)
(message "Remaining capacity: %s\nFull capacity: %s"
(bat-remaining)
(bat-full)))
--
Deniz Dogan
next prev parent reply other threads:[~2011-02-22 15:33 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-21 14:46 Writing a Lisp to show the capacity of the battery of the laptop Thomas Dean
2011-02-22 15:13 ` Le Wang
2011-02-22 15:33 ` Deniz Dogan [this message]
2011-02-22 16:51 ` Alberto Luaces
2011-02-22 17:25 ` Deniz Dogan
2011-02-22 17:35 ` Alberto Luaces
2011-02-22 17:41 ` Deniz Dogan
2011-02-22 17:25 ` Eli Zaretskii
[not found] <mailman.14.1298381790.4429.help-gnu-emacs@gnu.org>
2011-02-22 16:48 ` Pascal J. Bourguignon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='AANLkTinz6TsEu_YMed5tRw6=RgEAcmTcM=BWHLTkjiiP@mail.gmail.com' \
--to=deniz.a.m.dogan@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=tdean9db@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.