Class: Ruco::TextArea
Direct Known Subclasses
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
Instance Method Summary collapse
- #color_mask ⇒ Object
- #content ⇒ Object
- #cursor ⇒ Object
- #cursor_index(line = @line, column = @column) ⇒ Object
- #delete(count) ⇒ Object
- #delete_line ⇒ Object
- #index_for_position(line, column) ⇒ 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
- #selecting(&block) ⇒ Object
- #text_in_selection ⇒ 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 |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
3 4 5 |
# File 'lib/ruco/text_area.rb', line 3 def selection @selection end |
Instance Method Details
#color_mask ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruco/text_area.rb', line 24 def color_mask mask = Array.new(@options[:lines]) return mask unless @selection mask.map_with_index do |_,line| visible = visible_area(line) next unless @selection.overlap?(visible) first = [@selection.first, visible.first].max last = [@selection.last, visible.last].min [ [first[1]-@scrolled_columns,Curses::A_REVERSE], [last[1]-@scrolled_columns, Curses::A_NORMAL] ] end end |
#content ⇒ Object
146 147 148 |
# File 'lib/ruco/text_area.rb', line 146 def content (lines * "\n").freeze end |
#cursor ⇒ Object
127 128 129 |
# File 'lib/ruco/text_area.rb', line 127 def cursor Cursor.new @cursor_line, @cursor_column end |
#cursor_index(line = @line, column = @column) ⇒ Object
131 132 133 134 135 |
# File 'lib/ruco/text_area.rb', line 131 def cursor_index(line=@line, column=@column) index = lines[0...line].join("\n").size + column index += 1 if line > 0 # account for missing newline index end |
#delete(count) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ruco/text_area.rb', line 103 def delete(count) if @selection delete_content_in_selection return end 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
122 123 124 125 |
# File 'lib/ruco/text_area.rb', line 122 def delete_line lines.slice!(@line, 1) adjust_view end |
#index_for_position(line, column) ⇒ Object
137 138 139 |
# File 'lib/ruco/text_area.rb', line 137 def index_for_position(line, column) cursor_index(line, column) end |
#insert(text) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruco/text_area.rb', line 90 def insert(text) delete_content_in_selection if @selection 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ruco/text_area.rb', line 42 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 @selection = nil unless @selecting adjust_view end |
#position_for_index(index) ⇒ Object
141 142 143 144 |
# File 'lib/ruco/text_area.rb', line 141 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
150 151 152 153 |
# File 'lib/ruco/text_area.rb', line 150 def resize(lines, columns) @options[:lines] = lines @options[:columns] = columns end |
#selecting(&block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ruco/text_area.rb', line 68 def selecting(&block) start = if @selection (position == @selection.first ? @selection.last : @selection.first) else position end @selecting = true instance_exec(&block) @selecting = false sorted = [start, position].sort @selection = sorted[0]..sorted[1] end |
#text_in_selection ⇒ Object
83 84 85 86 87 88 |
# File 'lib/ruco/text_area.rb', line 83 def text_in_selection return '' unless @selection start = index_for_position(*@selection.first) finish = index_for_position(*@selection.last) content.slice(start, finish-start) 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 |