Class: Ruco::TextArea
Direct Known Subclasses
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
Instance Method Summary collapse
- #content ⇒ Object
- #cursor ⇒ Object
- #delete(count) ⇒ Object
- #index_for_position(position = self.position) ⇒ Object
-
#initialize(content, options) ⇒ TextArea
constructor
A new instance of TextArea.
- #insert(text) ⇒ Object
- #move(where, *args) ⇒ Object
- #reset ⇒ Object
- #resize(lines, columns) ⇒ Object
- #selecting(&block) ⇒ Object
- #style_map ⇒ 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 |
# File 'lib/ruco/text_area.rb', line 5 def initialize(content, ) @lines = content.naive_split("\n") @options = .dup @column = 0 @line = 0 @window = Window.new(@options.delete(:lines), @options.delete(:columns), @options[:window]||{}) adjust_window end |
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
3 4 5 |
# File 'lib/ruco/text_area.rb', line 3 def column @column end |
#line ⇒ Object
Returns the value of attribute line.
3 4 5 |
# File 'lib/ruco/text_area.rb', line 3 def line @line end |
#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
#content ⇒ Object
117 118 119 |
# File 'lib/ruco/text_area.rb', line 117 def content (lines * "\n").freeze end |
#cursor ⇒ Object
19 20 21 22 |
# File 'lib/ruco/text_area.rb', line 19 def cursor adjust_window @window.cursor end |
#delete(count) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ruco/text_area.rb', line 92 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!(index_for_position, count) end end else backspace(count.abs) end end |
#index_for_position(position = self.position) ⇒ Object
111 112 113 114 115 |
# File 'lib/ruco/text_area.rb', line 111 def index_for_position(position=self.position) index = lines[0...position.line].join("\n").size + position.column index += 1 if position.line > 0 # account for missing newline index end |
#insert(text) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ruco/text_area.rb', line 79 def insert(text) delete_content_in_selection if @selection text.tabs_to_spaces! if text == "\n" and @column >= current_line.leading_whitespace.size current_whitespace = current_line.leading_whitespace next_whitespace = lines[line+1].to_s.leading_whitespace text = text + [current_whitespace, next_whitespace].max end insert_into_content text move_according_to_insert text end |
#move(where, *args) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ruco/text_area.rb', line 29 def move(where, *args) case where when :relative then self.line += args.first self.column += args.last when :to then self.line, self.column = args when :to_bol then move_to_bol(*args) when :to_eol then move_to_eol(*args) when :to_line then self.line = args.first when :to_column then self.column = args.first when :to_index then move(:to, *position_for_index(*args)) when :page_down then self.line += @window.lines @window.set_top(@window.top + @window.lines, @lines.size) when :page_up then self.line -= @window.lines @window.set_top(@window.top - @window.lines, @lines.size) else raise "Unknown move type #{where} with #{args.inspect}" end @selection = nil unless @selecting end |
#reset ⇒ Object
75 76 77 |
# File 'lib/ruco/text_area.rb', line 75 def reset @selection = nil end |
#resize(lines, columns) ⇒ Object
121 122 123 124 |
# File 'lib/ruco/text_area.rb', line 121 def resize(lines, columns) @window.lines = lines @window.columns = columns end |
#selecting(&block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ruco/text_area.rb', line 53 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 |
#style_map ⇒ Object
24 25 26 27 |
# File 'lib/ruco/text_area.rb', line 24 def style_map adjust_window @window.style_map(@selection) end |
#text_in_selection ⇒ Object
68 69 70 71 72 73 |
# File 'lib/ruco/text_area.rb', line 68 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
14 15 16 17 |
# File 'lib/ruco/text_area.rb', line 14 def view adjust_window @window.crop(lines) * "\n" + "\n" end |