Class: Redwood::TextMode

Inherits:
ScrollMode show all
Defined in:
lib/sup/modes/text_mode.rb

Direct Known Subclasses

HelpMode, LogMode

Instance Attribute Summary collapse

Attributes inherited from ScrollMode

#botline, #leftcol, #status, #topline

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

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!, #cleanup, #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, #status, #unsaved?

Constructor Details

#initialize(text = "", filename = nil) ⇒ TextMode

Returns a new instance of TextMode.



10
11
12
13
14
15
16
# File 'lib/sup/modes/text_mode.rb', line 10

def initialize text="", filename=nil
  @text = text
  @filename = filename
  update_lines
  buffer.mark_dirty if buffer
  super()
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/sup/modes/text_mode.rb', line 4

def text
  @text
end

Instance Method Details

#<<(line) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/sup/modes/text_mode.rb', line 52

def << line
  @lines = [0] if @text.empty?
  @text << line.fix_encoding!
  @lines << @text.length
  if buffer
    ensure_mode_validity
    buffer.mark_dirty
  end
end

#[](i) ⇒ Object



66
67
68
69
70
# File 'lib/sup/modes/text_mode.rb', line 66

def [] i
  return nil unless i < @lines.length
  @text[@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)].normalize_whitespace
#    (@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)).inspect
end

#linesObject



62
63
64
# File 'lib/sup/modes/text_mode.rb', line 62

def lines
  @lines.length - 1
end

#pipeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sup/modes/text_mode.rb', line 23

def pipe
  command = BufferManager.ask(:shell, "pipe command: ")
  return if command.nil? || command.empty?

  output, success = pipe_to_process(command) do |stream|
    @text.each { |l| stream.puts l }
  end

  unless success
    BufferManager.flash "Invalid command: '#{command}' is not an executable"
    return
  end

  if output
    BufferManager.spawn "Output of '#{command}'", TextMode.new(output.ascii)
  else
    BufferManager.flash "'#{command}' done!"
  end
end

#save_to_diskObject



18
19
20
21
# File 'lib/sup/modes/text_mode.rb', line 18

def save_to_disk
  fn = BufferManager.ask_for_filename :filename, "Save to file: ", @filename
  save_to_file(fn) { |f| f.puts text } if fn
end