Sha256: a24d3cbacca0eaefb5686d42c9060d4df2b0c47947524cf77829100ff8ba12cf

Contents?: true

Size: 1.33 KB

Versions: 32

Compression:

Stored size: 1.33 KB

Contents

require 'stringio'
module Redwood

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

class LogMode < TextMode
  register_keymap do |k|
    k.add :toggle_follow, "Toggle follow mode", 'f'
  end

  ## if buffer_name is supplied, this mode will spawn a buffer
  ## upon receiving the << message. otherwise, it will act like
  ## a regular buffer.
  def initialize autospawn_buffer_name=nil
    @follow = true
    @autospawn_buffer_name = autospawn_buffer_name
    @on_kill = []
    super()
  end

  ## register callbacks for when the buffer is killed
  def on_kill &b; @on_kill << b end

  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

  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

  def status
    super + " (follow: #@follow)"
  end

  def cleanup
    @on_kill.each { |cb| cb.call self }
    self.text = ""
    super
  end
end

end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
sup-1.2 lib/sup/modes/log_mode.rb
sup-1.1 lib/sup/modes/log_mode.rb
sup-1.0 lib/sup/modes/log_mode.rb
sup-0.23 lib/sup/modes/log_mode.rb
sup-0.22.1 lib/sup/modes/log_mode.rb
sup-0.22.0 lib/sup/modes/log_mode.rb
sup-0.21.0 lib/sup/modes/log_mode.rb
sup-0.20.0 lib/sup/modes/log_mode.rb
sup-0.19.0 lib/sup/modes/log_mode.rb
sup-0.18.0 lib/sup/modes/log_mode.rb
sup-0.17.0 lib/sup/modes/log_mode.rb
sup-0.16.0 lib/sup/modes/log_mode.rb
sup-0.15.4 lib/sup/modes/log_mode.rb
sup-0.15.3 lib/sup/modes/log_mode.rb
sup-0.15.2 lib/sup/modes/log_mode.rb
sup-0.15.1 lib/sup/modes/log_mode.rb
sup-0.15.0 lib/sup/modes/log_mode.rb
sup-0.14.1.1 lib/sup/modes/log_mode.rb
sup-0.13.2.1 lib/sup/modes/log_mode.rb
sup-0.14.1 lib/sup/modes/log_mode.rb