source: project/wiki/wmiirc snippets @ 15247

Last change on this file since 15247 was 15247, checked in by sjamaan, 12 years ago

Document how to display the battery charge under NetBSD

File size: 2.4 KB
1== wmiirc snippets
3Post your favorite wmiirc scriptlets here.
5=== Tag cycling
7Normally you have numbered tags and pressing Mod+n gives you tag n, but
8some people prefer to have more meaningful tag names.  This means more
9work to type them in, so here's a way to navigate to the left or to
10the right of the current tab through the tabs as they appear in the bar.
12<enscript highlight=scheme>
13   ((key ,modkey "comma")
14    . ,(lambda _
15         (let loop ((list (wmii:tags))
16                    (tag (wmii:tag)))
17           (cond
18            ((or (null? list) (null? (cdr list))) #f)
19            ((string=? (cadr list) tag) (wmii:goto-tag (car list)))
20            (else (loop (cdr list) tag))))))
21   ((key ,modkey "period")
22    . ,(lambda _
23         (and-let* ((tag (member (wmii:tag) (wmii:tags)))
24                    ((not (null? (cdr tag)))))
25           (wmii:goto-tag (cadr tag)))))
28=== Battery charge in status bar on NetBSD
30On my notebook, I want to have a quick view on my battery charge at
31all times.  Starting with NetBSD 5.0, you can use the program
32"envstat" to obtain battery info (before you'd use "apm").
33Unfortunately, there's no easy way to parse its output in a script.
34You could use the XML output, but then you'd have to wade through the
35entire output because the {{-s}} switch does not restrict the proplib
36output to the given sensors.
38On my iBook G4 laptop, the battery is called {{smartbat0}}.
40<enscript highlight=scheme>
41(define (get-charge)
42  ;; Note that ordering of -s args doesn't influence ordering of output!
43  (let* ((lines (cddr (with-input-from-pipe "envstat -s 'smartbat0:Battery design cap,smartbat0:Battery charge'" read-lines)))
44         (max (string->number
45               (cadr (string-match ".*([0-9]+\\.[0-9]+).*" (car lines)))))
46         (charge (string->number
47                  (cadr (string-match ".*([0-9]+\\.[0-9]+).*" (cadr lines))))))
48    (/ (round (/ (* charge 1000) max)) 10)))
51This procedure gives the output at a granularity of one tenth percent.
52I check the output in my status procedure that is run once every
53second and display a warning if the charge drops below a threshold:
55<enscript highlight=scheme>
56(let loop ()
57   (let ((charge (get-charge)))
58     (wmii:write-tab
59       "rbar" "status"
60       (conc charge "% | "
61             (with-input-from-pipe "echo -n $(uptime | sed 's/.*://; s/,//g') '|' $(date)" read-string)))
62     (when (<= charge 5)
63       (process-run "xmessage -center 'Low battery power!'")))
64     (thread-sleep! 1)
65     (loop))
Note: See TracBrowser for help on using the repository browser.