Class: Ruco::TextArea
Direct Known Subclasses
Constant Summary collapse
- SURROUNDING_CHARS =
{ '<' => '>', '(' => ')', '[' => ']', '{' => '}', '"' => '"', "'" => "'", "`" => "`", '/' => '/' }
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
- #position ⇒ 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.
16 17 18 19 20 21 22 23 |
# File 'lib/ruco/text_area.rb', line 16 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.
14 15 16 |
# File 'lib/ruco/text_area.rb', line 14 def column @column end |
#line ⇒ Object
Returns the value of attribute line.
14 15 16 |
# File 'lib/ruco/text_area.rb', line 14 def line @line end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
14 15 16 |
# File 'lib/ruco/text_area.rb', line 14 def lines @lines end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
14 15 16 |
# File 'lib/ruco/text_area.rb', line 14 def selection @selection end |
Instance Method Details
#content ⇒ Object
133 134 135 |
# File 'lib/ruco/text_area.rb', line 133 def content (lines * "\n").freeze end |
#cursor ⇒ Object
30 31 32 33 |
# File 'lib/ruco/text_area.rb', line 30 def cursor adjust_window @window.cursor end |
#delete(count) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruco/text_area.rb', line 110 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
129 130 131 |
# File 'lib/ruco/text_area.rb', line 129 def index_for_position(position=self.position) lines[0...position.line].sum{|l| l.size + 1} + position.column end |
#insert(text) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ruco/text_area.rb', line 91 def insert(text) if @selection if SURROUNDING_CHARS[text] return surround_selection_with(text) else delete_content_in_selection end end 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruco/text_area.rb', line 40 def move(where, *args) case where when :relative move_relative(*args) when :to 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 self.line += @window.lines @window.set_top(@window.top + @window.lines, @lines.size) when :page_up self.line -= @window.lines @window.set_top(@window.top - @window.lines, @lines.size) when :jump move_jump(args.first) else raise "Unknown move type #{where} with #{args.inspect}" end @selection = nil unless @selecting end |
#position ⇒ Object
142 143 144 |
# File 'lib/ruco/text_area.rb', line 142 def position Position.new(line, column) end |
#reset ⇒ Object
87 88 89 |
# File 'lib/ruco/text_area.rb', line 87 def reset @selection = nil end |
#resize(lines, columns) ⇒ Object
137 138 139 140 |
# File 'lib/ruco/text_area.rb', line 137 def resize(lines, columns) @window.lines = lines @window.columns = columns end |
#selecting(&block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruco/text_area.rb', line 65 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
35 36 37 38 |
# File 'lib/ruco/text_area.rb', line 35 def style_map adjust_window @window.style_map(@selection) end |
#text_in_selection ⇒ Object
80 81 82 83 84 85 |
# File 'lib/ruco/text_area.rb', line 80 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
25 26 27 28 |
# File 'lib/ruco/text_area.rb', line 25 def view adjust_window @window.crop(lines) * "\n" end |