Class: Ruco::Editor
- Defined in:
- lib/ruco/editor.rb,
lib/ruco/editor/colors.rb,
lib/ruco/editor/history.rb,
lib/ruco/editor/line_numbers.rb
Defined Under Namespace
Modules: Colors, History, LineNumbers
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
Instance Method Summary collapse
- #find(text) ⇒ Object
-
#initialize(file, options) ⇒ Editor
constructor
A new instance of Editor.
- #modified? ⇒ Boolean
- #save ⇒ Object
- #store_session ⇒ Object
Constructor Details
#initialize(file, options) ⇒ Editor
Returns a new instance of Editor.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruco/editor.rb', line 14 def initialize(file, ) @file = file @options = # 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.binary_read(@file) : '') @options[:language] ||= LanguageSniffer.detect(@file, :content => content).language content.tabs_to_spaces! if @options[:convert_tabs] # cleanup newline formats @newline = content.match("\r\n|\r|\n") @newline = (@newline ? @newline[0] : "\n") content.gsub!(/\r\n?/,"\n") @saved_content = content @text_area = EditorArea.new(content, @options) @history = @text_area.history restore_session end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
3 4 5 |
# File 'lib/ruco/editor.rb', line 3 def file @file end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
5 6 7 |
# File 'lib/ruco/editor.rb', line 5 def history @history end |
Instance Method Details
#find(text) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/ruco/editor.rb', line 38 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 |
#modified? ⇒ Boolean
47 48 49 |
# File 'lib/ruco/editor.rb', line 47 def modified? @saved_content != text_area.content end |
#save ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruco/editor.rb', line 51 def save lines = text_area.send(:lines) lines.each(&:rstrip!) if @options[:remove_trailing_whitespace_on_save] lines << '' if @options[:blank_line_before_eof_on_save] and lines.last.to_s !~ /^\s*$/ content = lines * @newline File.open(@file,'wb'){|f| f.write(content) } @saved_content = content.gsub(/\r?\n/, "\n") true rescue Object => e e. end |
#store_session ⇒ Object
65 66 67 |
# File 'lib/ruco/editor.rb', line 65 def store_session session_store.set(@file, text_area.state.slice(:position, :screen_position)) end |