Class: Ruco::TextArea
Direct Known Subclasses
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
- #content ⇒ Object
- #cursor ⇒ Object
- #cursor_index ⇒ Object
- #delete(count) ⇒ Object
- #delete_line ⇒ Object
-
#initialize(content, options) ⇒ TextArea
constructor
A new instance of TextArea.
- #insert(text) ⇒ Object
- #move(where, *args) ⇒ Object
- #position_for_index(index) ⇒ Object
- #resize(lines, columns) ⇒ Object
- #view ⇒ Object
Constructor Details
#initialize(content, options) ⇒ TextArea
Returns a new instance of TextArea.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ruco/text_area.rb', line 5 def initialize(content, ) @lines = tabs_to_spaces(content).naive_split("\n") @options = @line = 0 @column = 0 @cursor_line = 0 @cursor_column = 0 @scrolled_lines = 0 @scrolled_columns = 0 @options[:line_scrolling_offset] ||= @options[:lines] / 2 @options[:column_scrolling_offset] ||= @options[:columns] / 2 end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
3 4 5 |
# File 'lib/ruco/text_area.rb', line 3 def lines @lines end |
Instance Method Details
#content ⇒ Object
94 95 96 |
# File 'lib/ruco/text_area.rb', line 94 def content (lines * "\n").freeze end |
#cursor ⇒ Object
79 80 81 |
# File 'lib/ruco/text_area.rb', line 79 def cursor Cursor.new @cursor_line, @cursor_column end |
#cursor_index ⇒ Object
83 84 85 86 87 |
# File 'lib/ruco/text_area.rb', line 83 def cursor_index index = lines[0...@line].join("\n").size + @column index += 1 if @line > 0 # account for missing newline index end |
#delete(count) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruco/text_area.rb', line 60 def delete(count) if count > 0 if current_line[@column..-1].size >= count current_line.slice!(@column, count) else with_lines_as_string do |content| content.slice!(cursor_index, count) end end else backspace(count.abs) end end |
#delete_line ⇒ Object
74 75 76 77 |
# File 'lib/ruco/text_area.rb', line 74 def delete_line lines.slice!(@line, 1) adjust_view end |
#insert(text) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruco/text_area.rb', line 49 def insert(text) text = tabs_to_spaces(text) if text == "\n" and @column >= after_last_word current_whitespace = current_line.match(/^\s*/)[0] next_whitespace = lines[@line+1].to_s.match(/^\s*/)[0] text = text + [current_whitespace, next_whitespace].max end insert_into_content text move_according_to_insert text end |
#move(where, *args) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruco/text_area.rb', line 24 def move(where, *args) case where when :relative then @line += args.first @column += args.last when :to then @line, @column = args when :to_bol then move_to_bol(*args) when :to_eol then move_to_eol(*args) when :to_line then @line = args.first when :to_column then @column = args.first when :page_down then shift = @options[:lines] - 1 @line += shift @scrolled_lines += shift when :page_up then shift = @options[:lines] - 1 @line -= shift @scrolled_lines -= shift else raise "Unknown move type #{where} with #{args.inspect}" end adjust_view end |
#position_for_index(index) ⇒ Object
89 90 91 92 |
# File 'lib/ruco/text_area.rb', line 89 def position_for_index(index) jump = content.slice(0, index).to_s.naive_split("\n") [jump.size - 1, jump.last.size] end |
#resize(lines, columns) ⇒ Object
98 99 100 101 |
# File 'lib/ruco/text_area.rb', line 98 def resize(lines, columns) @options[:lines] = lines @options[:columns] = columns end |
#view ⇒ Object
18 19 20 21 22 |
# File 'lib/ruco/text_area.rb', line 18 def view Array.new(@options[:lines]).map_with_index do |_,i| (lines[i + @scrolled_lines] || "").slice(@scrolled_columns, @options[:columns]) end * "\n" + "\n" end |