~ubuntu-branches/ubuntu/vivid/sup-mail/vivid

« back to all changes in this revision

Viewing changes to lib/sup/modes/log-mode.rb

  • Committer: Bazaar Package Importer
  • Author(s): Decklin Foster
  • Date: 2009-10-12 16:47:08 UTC
  • mfrom: (1.1.4 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091012164708-9fk9h4i1y6v5ofgm
New upstream version

Show diffs side-by-side

added added

removed removed

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