]> git.cworth.org Git - sup/blob - lib/sup/modes/log-mode.rb
Merge branch 'logging'
[sup] / lib / sup / modes / log-mode.rb
1 require 'stringio'
2 module Redwood
3
4 ## a variant of text mode that allows the user to automatically follow text,
5 ## and respawns when << is called if necessary.
6
7 class LogMode < TextMode
8   register_keymap do |k|
9     k.add :toggle_follow, "Toggle follow mode", 'f'
10   end
11
12   def initialize buffer_name
13     @follow = true
14     @buffer_name = buffer_name
15     @on_kill = []
16     super()
17   end
18
19   ## register callbacks for when the buffer is killed
20   def on_kill &b; @on_kill << b end
21
22   def toggle_follow
23     @follow = !@follow
24     if @follow
25       jump_to_line(lines - buffer.content_height + 1) # leave an empty line at bottom
26     end
27     buffer.mark_dirty
28   end
29
30   def << s
31     unless buffer
32       BufferManager.spawn @buffer_name, self, :hidden => true, :system => true
33     end
34
35     s.split("\n").each { |l| super(l + "\n") } # insane. different << semantics.
36
37     if @follow
38       follow_top = lines - buffer.content_height + 1
39       jump_to_line follow_top if topline < follow_top
40     end
41   end
42
43   def status
44     super + " (follow: #@follow)"
45   end
46
47   def cleanup
48     @on_kill.each { |cb| cb.call self }
49     self.text = ""
50     super
51   end
52 end
53
54 end