class TextEditor::Window
Public Instance Methods
cache()
click to toggle source
# File lib/text_editor/window.rb 8 def cache 9 @cache ||= {} 10 end
file()
click to toggle source
# File lib/text_editor/window.rb 12 def file 13 config.files.first 14 end
render()
click to toggle source
# File lib/text_editor/window.rb 16 def render 17 buffer.each_line.with_index do |line, index| 18 if cache[index] != line 19 cache[index] = line 20 move(index, 0) 21 write(line) 22 end 23 end 24 25 deleted_lines = cache.keys - (0...buffer.size).to_a 26 27 deleted_lines.each do |index| 28 cache.delete(index) 29 move(index, 0) 30 delete_line 31 end 32 33 line = cursor.line 34 content = buffer.line(line) 35 search = content[0..cursor.column] 36 37 tabs = search.scan("\t").size 38 width = config.tab.width - 1 39 column = cursor.column + (tabs * width) 40 41 move(line, column) 42 window.refresh 43 end
state()
click to toggle source
# File lib/text_editor/window.rb 45 def state 46 @state || update(Buffer.new(file), Cursor.new) 47 end
update(buffer, cursor)
click to toggle source
# File lib/text_editor/window.rb 49 def update(buffer, cursor) 50 @state = State.new(buffer, cursor) 51 end
Private Instance Methods
delete_line()
click to toggle source
# File lib/text_editor/window.rb 55 def delete_line 56 window.deleteln 57 end
move(*coords)
click to toggle source
# File lib/text_editor/window.rb 59 def move(*coords) 60 window.setpos(*coords) 61 end
window()
click to toggle source
# File lib/text_editor/window.rb 63 def window 64 @window ||= terminal.window(terminal.lines, terminal.columns, 0, 0) 65 end
write(string)
click to toggle source
# File lib/text_editor/window.rb 67 def write(string) 68 tabbed = string.gsub("\t", config.tab.to_s) 69 padded = tabbed.ljust(terminal.columns) 70 window.addstr(padded) 71 end