9

display-battery-mode displays battery information, but I only want to use it when my computer is unplugged and running off battery power.

  1. Is there a way to set the display mode according to the power supply mode?

  2. Can I also get the temperature reported in the modeline?

Dan
  • 32,584
  • 6
  • 98
  • 168
Nick
  • 4,423
  • 4
  • 24
  • 41

2 Answers2

6

I'm not sure whether you can do that with the built-in display-battery-mode. It's not very customizable in this regard, and you may have to resort to heavy advises to change the built-in mode as you like.

I wrote fancy-battery.el some time ago to provide a more customizable indicator for the battery status. Notably, I wanted to indicate the state of the battery with colours, but the package is flexible enough to allow for your use case as well, by changing fancy-battery-mode-line accordingly:

(setq fancy-battery-mode-line
      '(:eval (unless (equal (cdr (assq ?b fancy-battery-last-status)) "+")
                (fancy-battery-default-mode-line))))

Don't ask, the status comes from battery.el, which has a horrible API. Just trust me that ?b is the battery state, and "+" indicates charging, for most backends at least.

This setting will show the battery status, but only if the battery is discharging. To use another format, write your own function to replace fancy-battery-default-mode-line. Feel free to take mine as inspritation.

Regarding the temperature, you may have luck with a different backend. Take a look at the existing backends in battery.el, search for one that includes battery temperature and try to fulfil it's requirements. However, battery.el typically succeeds at picking the best backend for your system, so quite likely your hardware simply does not report the battery temperature, or is not properly supported by your OS.

2

You can achieve this by advicing battery-update, the function which actually updates the modeline to display battery status. The advice checks if the battery is currently charging in which case it calls battery-update with battery-mode-line-format bound to nil (which in effect hides the battery information), otherwise battery-update is called with unchanged value of battery-mode-line-format

(defun my-skip-battery-display-if-charging (original-func)
  (let ((battery-mode-line-format (unless (string= (downcase (cdr (assoc ?B (funcall battery-status-function)))) "charging")
                                    battery-mode-line-format)))
          (funcall original-func)))

(advice-add 'battery-update :around #'my-skip-battery-display-if-charging)

The advice is a bit clunky since we call battery-status-function once while the buffer-update function already calls that function once so we are making one extra function call but this is what I could come up with.

Iqbal Ansari
  • 7,468
  • 1
  • 28
  • 31