Class: Redwood::LogMode

Inherits:
TextMode show all
Defined in:
lib/sup/modes/log_mode.rb

Overview

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

Direct Known Subclasses

ConsoleMode, PollMode

Instance Attribute Summary

Attributes inherited from TextMode

#text

Attributes inherited from ScrollMode

#botline, #leftcol, #topline

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

Methods inherited from TextMode

#[], #lines, #pipe, #save_to_disk

Methods inherited from ScrollMode

#at_bottom?, #at_top?, #cancel_search!, #col_jump, #col_left, #col_right, #continue_search_in_buffer, #draw, #ensure_mode_validity, #half_page_down, #half_page_up, #in_search?, #jump_to_col, #jump_to_end, #jump_to_left, #jump_to_line, #jump_to_start, #line_down, #line_up, #page_down, #page_up, #resize, #rightcol, #search_goto_line, #search_goto_pos, #search_in_buffer, #search_start_line

Methods inherited from Mode

#blur, #cancel_search!, #draw, #focus, #handle_input, #help_text, #in_search?, keymap, keymaps, #killable?, load_all_modes, make_name, #name, #pipe_to_process, register_keymap, #resize, #resolve_input, #save_to_file, #unsaved?

Constructor Details

#initialize(autospawn_buffer_name = nil) ⇒ LogMode

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



15
16
17
18
19
20
# 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

Instance Method Details

#<<(s) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# 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

#cleanupObject



50
51
52
53
54
# 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) ⇒ Object

register callbacks for when the buffer is killed



23
# File 'lib/sup/modes/log_mode.rb', line 23

def on_kill &b; @on_kill << b end

#statusObject



46
47
48
# File 'lib/sup/modes/log_mode.rb', line 46

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

#toggle_followObject



25
26
27
28
29
30
31
# 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