chiark / gitweb /
ypp-chatlog-alerter: disable debugging; do clear properly (new notification)
[ypp-sc-tools.main.git] / ypp-chatlog-alerter
1 #!/usr/bin/wish
2
3 proc manyset {list args} {
4     foreach val $list var $args {
5         upvar 1 $var my
6         set my $val
7     }
8 }
9
10 set progname ypp-chatlog-alerter
11 set height 5
12
13 proc menuent {w l a x} {
14     set c [list .mbar.$w add command -label $l -command $x]
15     if {[string length $a]} { lappend c -accel Command-$a }
16     eval $c
17 }
18 proc menus {} {
19     global height
20     
21     menu .mbar -tearoff 0
22     foreach w {file edit} l {File Edit} {
23         menu .mbar.$w -tearoff 0
24         .mbar add cascade -menu .mbar.$w -label $l
25     }
26     foreach l {Open Quit} a {O Q} x {newfile exit} {
27         menuent file $l $a $x
28     }
29     foreach l {Cut Copy Paste Clear} a {X C V {}} {
30         menuent edit $l $a [list event generate {[focus]} <<$l>>]]
31     }
32     . configure -menu .mbar
33 }
34
35 proc nonportability {} {
36     global progname defaultfile
37     
38     switch -exact [tk windowingsystem] {
39         aqua {
40             set defaultfile ~/Library/Preferences/$progname.prefs
41         }
42         x11 {
43             set defaultfile ~/.$progname.rc
44         }
45         default {
46             error ?
47         }
48     }
49 }
50
51 set lw_ls {times pirates messages}
52 set lw_ws {.time .pirate .msg}
53
54 proc widgets {} {
55     global height lw_ws prtimes
56     listbox .time   -width  5 -background black \
57         -listvariable prtimes -foreground white
58     listbox .pirate -width 14 -background black
59     listbox .msg    -width 80
60     for_lw {
61         $w configure -height $height -borderwidth 0 -activestyle none \
62             -highlightthickness 0
63     }
64     eval pack $lw_ws -side left
65     label .overlay -relief raised -foreground white
66 }
67
68 proc shownotice {colour message} {
69     .overlay configure -text $message -background $colour
70     place .overlay -relx 0.5 -rely 0.5 -anchor center
71 }
72 proc hidenotice {} {
73     place forget .overlay
74 }
75
76 proc newfile {} {
77     global currentfile defaultfile logfile
78     
79     set newfile [tk_getOpenFile -multiple 0 -initialfile $currentfile \
80                      -title "Select YPP log to track"]
81     if {![string length $newfile]} return
82
83     catch { close $logfile }
84     catch { unset logfile }
85
86     set currentfile $newfile
87     set newdefaults [open $defaultfile.new w]
88     puts $newdefaults "[list set currentfile $currentfile]"
89     close $newdefaults
90     file rename -force $defaultfile.new $defaultfile
91
92     clearlists
93     pollfile
94 }
95
96 proc for_lw {args} {
97     global lw_ls lw_ws
98     set body [lindex $args end]
99     set args [lreplace $args end end]
100     uplevel 1 [list \
101                    foreach l $lw_ls \
102                            w $lw_ws] \
103                            $args \
104                          [list $body]
105 }
106
107 set e_life 120
108 set tint_switch 90
109 set tint_switched [expr { exp( -($tint_switch+0.0) / $e_life ) }]
110
111 proc retint {} {
112     global times e_life retint_after otherevent prtimes
113     catch { after cancel $retint_after }
114     set i 0
115     set now [clock seconds]
116     set latest $otherevent
117     set newprtimes {}
118     foreach time $times {
119         set age [expr {$now-$time}]
120         if {!$time} {
121             lappend newprtimes {}
122         } elseif {$age < 60} {
123             lappend newprtimes [format "%3ds" $age]
124         } else {
125             lappend newprtimes [format "%3dm" [expr {$age/60}]]
126         }
127         set latest [expr { $time > $latest ? $time : $latest }]
128         set tint [expr { exp( (-($age >= 0 ? $age : 0) + 0.0) / $e_life ) }]
129 #puts "AGE $age LA $latest TI $tint"
130         tintentries .msg $i $tint
131         incr i
132     }
133     set prtimes $newprtimes
134     set next [expr { ($now - $latest < 10 ? 10 :
135                       $now - $latest > 3000 ? 3000 :
136                       $now - $latest
137                       ) * 10 }]
138 #puts "nexting $latest $now $next"
139     set retint_after [after $next retint]
140 }
141
142 proc tintentries {ws y tint} {
143     global tint_switched
144     #puts "$tint $tint_switched"
145     set yellow [format "%02x" [expr {round( 255 *
146         ( $tint >= $tint_switched ? $tint : 0 )
147                                             )}]]
148     set black [format "%02x" [expr {round( 255 *
149         ( $tint >= $tint_switched ? 0 : ($tint / $tint_switched)*0.75 + 0.25 )
150                                            )}]]
151     set fg [format "#${black}${black}${black}"]
152     set bg [format "#${yellow}${yellow}00"]
153     foreach w $ws { $w itemconfigure $y -foreground $fg -background $bg }
154 }
155
156 proc clearlists {} {
157     global height otherevent
158     global times prtimes pirates messages
159     set currentfile {}
160     
161     for_lw { $w delete 0 end; set $l {} }
162     set ntimes {}
163     set prtimes {}
164     for {set i 0} {$i<$height} {incr i} {
165         for_lw { lappend $l {}; $w insert end {} }
166         lappend ntimes 0
167         lappend prtimes {}
168     }
169     set times $ntimes
170     set otherevent [clock seconds]
171     retint
172 }
173
174 proc showtints {} {
175     global e_life
176     set divs 20
177     listbox .tints -width 60 -height [expr {$divs+1}]
178     for {set y 0} {$y <= $divs} {incr y} {
179         set tint [expr {($y+0.0)/$divs}]
180         .tints insert end \
181             "[format "#%2d   %6f   %4ds" $y $tint [expr {round(
182                 $tint > 0 ? -log($tint) * $e_life : "9999"
183             )}]]  The quick brown fox jumped over the lazy dog"
184         tintentries .tints $y $tint
185     }
186     pack .tints -side bottom
187 }
188
189 proc file-read-lines {lvar body} {
190     upvar 1 $lvar l
191     global logfile poll_after lastactivity bufdata
192     
193 #puts f-r-l
194     if {![info exists logfile]} {
195         return
196     }
197     while 1 {
198         if {[catch { read $logfile } got]} {
199             file-error $got
200             return
201         }
202 #puts "f-r-l [string length $got]"
203         if {![string length $got] && [eof $logfile]} {
204             set ago [expr { [clock seconds] - $lastactivity }]
205             set interval [expr {( $ago < 10 ? 10 :
206                                   $ago > 3000 ? 3000 :
207                                   $ago ) * 10}]
208 #puts "requeue filepoll $interval"
209             set poll_after [after $interval pollfile]
210             return
211         }
212         set lastactivity [clock seconds]
213
214         while {[regexp {^(.*?)[\r\n]+(.*)$} $got dummy lhs rhs]} {
215             set l "$bufdata$lhs"
216             set bufdata {}
217             set got $rhs
218 #puts ">>$l<<"
219             uplevel 1 $body
220         }
221         append bufdata $got
222     }
223 }
224
225 proc file-error {emsg} {
226     global logfile
227     
228     shownotice red "Error reading logfile $currentfile:\n$emsg"
229     catch { close $logfile }
230     catch { unset logfile }
231 }       
232
233 proc pollfile {} {
234     global poll_after logfile currentfile
235     global errorCode errorInfo bufdata lastactivity
236
237     catch { after cancel $poll_after }
238     if {![string length $currentfile]} {
239         shownotice red "No log file selected.  Use File / Open."
240         return
241     }
242     if {![info exists logfile]} {
243         set bufdata {}
244         set lastactivity [clock seconds] 
245         if {[catch { set logfile [open $currentfile r] } emsg]} {
246             shownotice red "Error opening logfile $currentfile:\n$emsg"
247             return
248         }
249         shownotice \#000080 "Reading $currentfile"
250         if {[catch {
251             seek $logfile -1024 end
252         } emsg]} {
253             if {![string match {POSIX EINVAL *} $errorCode]} {
254                 file-error $emsg
255             }
256         }
257         file-read-lines l { }
258     }
259     file-read-lines l {
260         hidenotice
261         if {[regexp {^\[\d+:\d+:\d+\] (.*)} $l dummy rhs]} {
262 #puts PROCLINE
263             process-line $rhs
264         }
265     }
266 }
267
268 proc process-line {l} {
269     if {[regexp {^(\w+) tells ye, \"(.*)\"$} $l dummy pirate msg]} {
270 #puts "MESSAGE $l"
271         message $pirate $msg
272     }
273 }
274
275 proc message {pirate msg} {
276     global times pirates messages height
277     global lw_ls lw_ws
278     
279     set ix [lsearch -exact $pirates $pirate]
280     set now [clock seconds]
281     if {$ix < 0} {
282         set cix 0
283         set oldest $now
284         foreach time $times {
285             if {$time < $oldest} {
286                 set oldest $time
287                 set ix $cix
288             }
289             incr cix
290         }
291         for_lw {
292             set $l [lreplace [set $l] $ix $ix]
293             lappend $l {}
294             $w delete $ix
295             $w insert end {}
296         }
297         set ix [expr {$height-1}]
298     }
299     for_lw new [list $now $pirate $msg] {
300         set $l [lreplace [set $l] $ix $ix $new]
301         $w delete $ix
302         $w insert $ix $new
303     }
304 #puts "TIMES $times"
305     .pirate itemconfigure $ix -foreground white
306     retint
307 }
308
309 proc parseargs {} {
310     global argv
311     foreach arg $argv {
312         if {![string compare $arg --test-tints]} {
313             showtints
314         } else {
315             error "unknown option $arg"
316         }
317     }
318 }
319
320 menus
321 nonportability
322 parseargs
323 widgets
324 clearlists
325
326 if {[file exists $defaultfile]} {
327     source $defaultfile
328 }
329
330 pollfile