class Redwood::LogMode

a variant of text mode that allows the user to automatically follow text, and respawns when << is called if necessary.

Public Class Methods

new(autospawn_buffer_name=nil) click to toggle source

if buffer_name is supplied, this mode will spawn a buffer upon receiving the << message. otherwise, it will act like a regular buffer.

# File lib/sup/modes/log-mode.rb, line 15
def initialize autospawn_buffer_name=nil
  @follow = true
  @autospawn_buffer_name = autospawn_buffer_name
  @on_kill = []
  super()
end

Public Instance Methods

<<(s) click to toggle source
# File lib/sup/modes/log-mode.rb, line 33
def << s
  if buffer.nil? && @autospawn_buffer_name
    BufferManager.spawn @autospawn_buffer_name, self, :hidden => true, :system => true
  end

  s.split("\n").each { |l| super(l + "\n") } # insane. different << semantics.

  if @follow
    follow_top = lines - buffer.content_height + 1
    jump_to_line follow_top if topline < follow_top
  end
end
cleanup() click to toggle source
# File lib/sup/modes/log-mode.rb, line 50
def cleanup
  @on_kill.each { |cb| cb.call self }
  self.text = ""
  super
end
on_kill(&b;) click to toggle source

register callbacks for when the buffer is killed

# File lib/sup/modes/log-mode.rb, line 23
def on_kill &b; @on_kill << b end
status() click to toggle source
# File lib/sup/modes/log-mode.rb, line 46
def status
  super + " (follow: #@follow)"
end
toggle_follow() click to toggle source
# File lib/sup/modes/log-mode.rb, line 25
def toggle_follow
  @follow = !@follow
  if @follow
    jump_to_line(lines - buffer.content_height + 1) # leave an empty line at bottom
  end
  buffer.mark_dirty
end