Sha256: 7dca74baa0b6b2792ca8a66c2c88f83032157e3c76ee0e1d16db53a5867361a0

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

module Redwood

class TextMode < ScrollMode
  attr_reader :text
  register_keymap do |k|
    k.add :save_to_disk, "Save to disk", 's'
    k.add :pipe, "Pipe to process", '|'
  end

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

  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

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

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

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

  def text= t
    @text = t
    update_lines
    if buffer
      ensure_mode_validity
      buffer.mark_dirty
    end
  end

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

  def lines
    @lines.length - 1
  end

  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

private

  def update_lines
    pos = @text.find_all_positions("\n")
    pos.push @text.length unless pos.last == @text.length - 1
    @lines = [0] + pos.map { |x| x + 1 }
  end
end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sup-0.20.0 lib/sup/modes/text_mode.rb
sup-0.19.0 lib/sup/modes/text_mode.rb
sup-0.18.0 lib/sup/modes/text_mode.rb
sup-0.17.0 lib/sup/modes/text_mode.rb
sup-0.16.0 lib/sup/modes/text_mode.rb
sup-0.15.4 lib/sup/modes/text_mode.rb
sup-0.15.3 lib/sup/modes/text_mode.rb
sup-0.15.2 lib/sup/modes/text_mode.rb
sup-0.15.1 lib/sup/modes/text_mode.rb