how to display uptime on emacs status bar? I am looking for /usr/bin/uptime output to be displayed on status bar

996 Views Asked by At

I am looking for /usr/bin/uptime output to be displayed on emacs status bar? I am using GNU Emacs 23.1.1 on centos.

Thanks

3

There are 3 best solutions below

1
On

Lookup function emacs-uptime. And look this link to customize the mode line.

1
On

@louxius suggestions are worth following. As to specific implementation, here's a snippet from my .emacs:

...
;; custom modeline
(setq-default 
 mode-line-format
 (list " " 'mode-line-modified          ;; the "**" at the beginning
       "--" 'mode-line-buffer-identification    ;; buffer file name
       "--" 'mode-line-modes            ;; major and minor modes in effect
       'mode-line-position          ;; line, column, file %
       "--" '(:eval (battery-status))
       "--" '(:eval (temperature))
       "--" '(:eval (format-time-string "%I:%M" (current-time)))
       "-%-"))                  ;; dashes sufficient to fill rest of modeline.

(defun battery-status ()
  "Outputs the battery percentage from acpi."
  (replace-regexp-in-string 
   ".*?\\([0-9]+\\)%.*" " Battery: \\1%% " 
   (substring (shell-command-to-string "acpi") 0 -1)))

(defun temperature ()
  (replace-regexp-in-string
   ".*? \\([0-9\.]+\\) .*" "Temp: \\1°C "
   (substring (shell-command-to-string "acpi -t") 0 -1)))
...

I want different things displayed there, obviously, but this should be a decent starting point for you.

0
On

I don't have uptime on my system, so I can't test this for you. But it should give you an idea. Seems to work on my system with ps substituted for uptime.

Perhaps someone else will offer a simpler or cleaner solution. You might also look at call-process or start-process instead of shell-command-to-string --- start-process is async. You might also want to consider using an idle timer --- the code here can slow Emacs down considerably, since it calls uptime each time the mode line is updated.

(setq-default 
 mode-line-format
 (list " " 'mode-line-modified
       "--" 'mode-line-buffer-identification
       "--" 'mode-line-modes
       'mode-line-position
       "--" '(:eval (shell-command-to-string "uptime"))
       "-%-"))

Here is another approach, which doesn't seem to slow things down noticeably:

(defun bar ()
  (with-current-buffer (get-buffer-create "foo")
    (erase-buffer)
    (start-process "ps-proc" "foo" "uptime")))

(setq foo (run-with-idle-timer 30 'REPEAT 'bar))

(setq-default 
 mode-line-format
 (list " " 'mode-line-modified
       "--" 'mode-line-buffer-identification
       "--" 'mode-line-modes
       'mode-line-position
       "--" '(:eval (with-current-buffer (get-buffer-create "foo")
                      (buffer-substring (point-min) (point-max))))
       "-%-"))