source: project/wiki/wmiirc snippets @ 15248

Last change on this file since 15248 was 15248, checked in by sjamaan, 10 years ago

Get rid of silly uptime command, and use builtin seconds->string

File size: 2.3 KB
Line 
1== wmiirc snippets
2
3Post your favorite wmiirc scriptlets here.
4
5=== Tag cycling
6
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.
11
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)))))
26</enscript>
27
28=== Battery charge in status bar on NetBSD
29
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.
37
38On my iBook G4 laptop, the battery is called {{smartbat0}}.
39
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)))
49</enscript>
50
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:
54
55<enscript highlight=scheme>
56(let loop ()
57   (let ((charge (get-charge)))
58     (wmii:write-tab
59       "rbar" "status"
60       (conc charge "% | " (seconds->string (current-seconds))))
61     (when (<= charge 5)
62       (process-run "xmessage -center 'Low battery power!'")))
63     (thread-sleep! 1)
64     (loop))
65</enscript>
Note: See TracBrowser for help on using the repository browser.