class BBS::TextBuffer
The text and the cursor of an input, without a screen: BBS::Field and BBS::Editor keep theirs in one and draw it each in their own way, so a key does the same in both.
The text is a list of lines; the cursor is a line index and a character position in that line.
buffer = BBS::TextBuffer.new('Hello', max: 80) buffer.edit('!') # => buffer buffer.text # => "Hello!"
Constants
- KEYS
-
The editing keys besides the characters, and the method each calls.
- MAX_LENGTH
-
The most characters a buffer holds when it is given no
max: 64 KiB. - PRINTABLE_CHAR
-
One printable character, the only kind
editinserts (BBS::Text::PRINTABLE_CHAR). - SPACED_CONTROLS
-
The control characters of a new text that become a space: tab and CR.
Attributes
The lines of the text, without line ends (Array of String).
The cursor’s position in its line, 0 to the line’s length.
Public Class Methods
# File lib/bbs/text_buffer.rb, line 40 def initialize(text = '', max: nil, single_line: false) @max = max || MAX_LENGTH @single = single_line text = text.to_s.gsub(Ansi::CRLF, "\n").tr(SPACED_CONTROLS, ' ') text = text.tr("\n", ' ') if @single @lines = text.split("\n", -1).map(&:dup) @lines = [+''] if @lines.empty? @line = @lines.size - 1 @pos = @lines.last.length end
Creates a buffer holding text with the cursor at its end.
- text
-
the text to start from; lines end with LF, tabs become spaces
- max
-
the most characters the text may hold (line ends count as one);
nilmeansMAX_LENGTH - single_line
-
one line: line ends in
textbecome spaces and RETURN does not split the line
Public Instance Methods
Source
# File lib/bbs/text_buffer.rb, line 77 def edit(key) return nil unless editing?(key) key.is_a?(String) ? insert(key) : send(KEYS.fetch(key)) self end
Applies the editing key key (as BBS::Telnet#readkey returns it):
- a one-character String
-
inserted at the cursor, unless the text is at
maxor it is not printable :backspace-
deletes before the cursor, joining the line to the one above at its start
:delete-
deletes under the cursor, joining the next line at the end of this one
:left,:right-
move one character, across line ends
:enter-
splits the line at the cursor (not in a single-line buffer)
Returns self when the key was one of these, nil for any other key, which the caller handles.
Source
# File lib/bbs/text_buffer.rb, line 55 def length = @lines.sum(&:length) + @lines.size - 1
The number of characters in the text, line ends counted as one each.
Source
# File lib/bbs/text_buffer.rb, line 58 def place(line, pos) @line = line.clamp(0, @lines.size - 1) @pos = pos.clamp(0, @lines[@line].length) end
Puts the cursor on line at pos, both clamped to the text.
Source
# File lib/bbs/text_buffer.rb, line 52 def text = @lines.join("\n")
The text, the lines joined by LF.