Class: Ruco::Editor

Inherits:
Object show all
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

Instance Method Summary collapse

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
37
38
39
40
# File 'lib/ruco/editor.rb', line 14

def initialize(file, options)
  @file = file
  @options = options
  handle = (File.exist?(file) ? File.open(file, 'rb') : nil)

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

  content = handle&.read || ''
  @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

  # git and kubectl wait for the file to be closed, so give them that event
  at_exit { handle&.close }
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/ruco/editor.rb', line 3

def file
  @file
end

#historyObject (readonly)

Returns the value of attribute history.



5
6
7
# File 'lib/ruco/editor.rb', line 5

def history
  @history
end

Instance Method Details

#contentObject



77
78
79
# File 'lib/ruco/editor.rb', line 77

def content
  text_area.content.freeze # no modifications allowed
end

#find(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruco/editor.rb', line 42

def find(text)
  move(:relative, 0,0) # reset selection
  start = text_area.content.index(text, text_area.index_for_position+1)
  return unless start

  # select the found word
  finish = start + text.size
  move(:to_index, finish)
  selecting{ move(:to_index, start) }

  true
end

#modified?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ruco/editor.rb', line 55

def modified?
  @saved_content != text_area.content
end

#saveObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruco/editor.rb', line 59

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.message
end

#store_sessionObject



73
74
75
# File 'lib/ruco/editor.rb', line 73

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