class BBS::Editor::Layout
The screen rows of a text wrapped at word boundaries: each row is [line, start, stop], the characters start...stop of line line. A row breaks after the last space that fits, or at the width when a word is longer than a row; it may end in one space beyond the width, which takes no room.
Attributes
The rows, an Array of [line, start, stop].
Public Class Methods
Source
# File lib/bbs/editor/layout.rb, line 15 def initialize(lines, width) @width = width @rows = lines.each_with_index.flat_map do |text, i| starts = starts(text) starts.each_with_index.map { |s, j| [i, s, starts[j + 1] || text.length] } end end
Lays lines (an Array of String) out in rows width wide.
Public Instance Methods
Source
# File lib/bbs/editor/layout.rb, line 29 def row_end(index) line, start, stop = @rows[index] following = @rows[index + 1] following.nil? || following[0] != line ? stop : [stop - 1, start].max end
The last cursor position on row index: its end on the last row of a line, before its last character otherwise, as that position is the next rowβs start.
Source
# File lib/bbs/editor/layout.rb, line 24 def row_of(line, pos) = @rows.rindex { |l, start, _| l == line && start <= pos } || 0
The index of the row the cursor at line, pos is on.