Class: Ruco::TextArea

Inherits:
Object show all
Defined in:
lib/ruco/text_area.rb

Direct Known Subclasses

EditorArea, TextField

Constant Summary collapse

SURROUNDING_CHARS =
{
  '<' => '>',
  '(' => ')',
  '[' => ']',
  '{' => '}',
  '"' => '"',
  "'" => "'",
  "`" => "`",
  '/' => '/'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, options) ⇒ TextArea

Returns a new instance of TextArea.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruco/text_area.rb', line 16

def initialize(content, options)
  if content.respond_to?(:encoding) and content.encoding.to_s.include?('ASCII')
    content = content.force_encoding('UTF-8')
  end

  @lines = content.naive_split("\n")
  @options = options.dup
  @column = 0
  @line = 0
  @window = Window.new(@options.delete(:lines), @options.delete(:columns), @options[:window]||{})
  adjust_window
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



14
15
16
# File 'lib/ruco/text_area.rb', line 14

def column
  @column
end

#lineObject

Returns the value of attribute line.



14
15
16
# File 'lib/ruco/text_area.rb', line 14

def line
  @line
end

#linesObject (readonly)

Returns the value of attribute lines.



14
15
16
# File 'lib/ruco/text_area.rb', line 14

def lines
  @lines
end

#selectionObject (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

#contentObject



138
139
140
# File 'lib/ruco/text_area.rb', line 138

def content
  (lines * "\n").freeze
end

#cursorObject



34
35
36
37
# File 'lib/ruco/text_area.rb', line 34

def cursor
  adjust_window
  @window.cursor
end

#delete(count) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruco/text_area.rb', line 115

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



134
135
136
# File 'lib/ruco/text_area.rb', line 134

def index_for_position(position=self.position)
  lines[0...position.line].sum{|l| l.size + 1} + position.column
end

#insert(text) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruco/text_area.rb', line 95

def insert(text)
  text = text.force_encoding('UTF-8')
  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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruco/text_area.rb', line 44

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

#positionObject



147
148
149
# File 'lib/ruco/text_area.rb', line 147

def position
  Position.new(line, column)
end

#resetObject



91
92
93
# File 'lib/ruco/text_area.rb', line 91

def reset
  @selection = nil
end

#resize(lines, columns) ⇒ Object



142
143
144
145
# File 'lib/ruco/text_area.rb', line 142

def resize(lines, columns)
  @window.lines = lines
  @window.columns = columns
end

#selecting(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruco/text_area.rb', line 69

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_mapObject



39
40
41
42
# File 'lib/ruco/text_area.rb', line 39

def style_map
  adjust_window
  @window.style_map(@selection)
end

#text_in_selectionObject



84
85
86
87
88
89
# File 'lib/ruco/text_area.rb', line 84

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

#viewObject



29
30
31
32
# File 'lib/ruco/text_area.rb', line 29

def view
  adjust_window
  @window.crop(lines) * "\n"
end