Sha256: df99bb512f7cfe42ba31188b1cc8a097b347fed8a4dad075f5ed0542f29f9726

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Ruco
  class Editor
    attr_reader :file
    attr_reader :text_area
    private :text_area
    delegate :view, :style_map, :cursor,
      :insert, :indent, :unindent, :delete, :delete_line,
      :redo, :undo,
      :selecting, :selection, :text_in_selection, :reset,
      :move, :resize,
      :to => :text_area

    def initialize(file, options)
      @file = file

      # check for size (10000 lines * 100 chars should be enough for everybody !?)
      if File.exist?(@file) and File.size(@file) > (1024 * 1024)
        raise "#{@file} is larger than 1MB, did you really want to open that with Ruco?"
      end

      content = (File.exist?(@file) ? File.read(@file) : '')
      content.tabs_to_spaces! if options[:convert_tabs]
      
      if options[:convert_return]
        content.gsub!(/\r\n?/,"\n")
      else
        raise "Ruco does not support \\r characters, start with --convert-return to remove them" if content.include?("\r")
      end

      @saved_content = content
      @text_area = EditorArea.new(content, options)
      restore_session
    end

    def find(text)
      move(:relative, 0,0) # reset selection
      return unless start = text_area.content.index(text, text_area.index_for_position+1)
      finish = start + text.size
      move(:to_index, finish)
      selecting{ move(:to_index, start) }
      true
    end

    def modified?
      @saved_content != text_area.content
    end

    def save
      content = text_area.content
      File.open(@file,'w'){|f| f.write(content) }
      @saved_content = content
      true
    rescue Object => e
      e.message
    end

    def store_session
      session_store.set(@file, text_area.state.slice(:position, :screen_position))
    end

    private

    def restore_session
      if state = session_store.get(@file)
        text_area.state = state
      end
    end

    def session_store
      FileStore.new(File.expand_path('~/.ruco/sessions'), :keep => 20)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruco-0.0.44 lib/ruco/editor.rb
ruco-0.0.43 lib/ruco/editor.rb