class RawLine::Line
The Line
class is used to represent the current line being processed and edited by RawLine::Editor
. It keeps track of the characters typed, the cursor position, the current word and maintains an internal history to allow undos and redos.
Attributes
Public Class Methods
Create an instance of RawLine::Line
. This method takes an optional block used to override the following instance attributes:
-
@text
- the line text. -
@history_size
- the size of the line history buffer. -
@position
- the current cursor position within the line. -
@prompt
- a prompt to prepend to the line text.
# File lib/rawline/line.rb, line 35 def initialize(history_size) @text = "" @history_size = history_size @position = 0 @prompt = "" @word_separator = ' ' yield self if block_given? @history = RawLine::HistoryBuffer.new(@history_size) @history << "" # Add empty line for complete undo... @offset = @prompt.length end
Public Instance Methods
Add a character (expressed as a character code) to the line text.
# File lib/rawline/line.rb, line 143 def <<(char) @text << char.chr end
Access the line text at @index
# File lib/rawline/line.rb, line 150 def [](index) @text[index] end
Modify the character(s) in the line text at @index
# File lib/rawline/line.rb, line 157 def []=(index, chars) @text[index] = chars end
Return the position corresponding to the beginning of the line.
# File lib/rawline/line.rb, line 101 def bol 0 end
Return true if the cursor is at the beginning of the line.
# File lib/rawline/line.rb, line 108 def bol? @position<=bol end
Return the position corresponding to the end of the line.
# File lib/rawline/line.rb, line 115 def eol @text.length-1 end
Return true if the cursor is at the end of the line.
# File lib/rawline/line.rb, line 122 def eol? @position>=eol end
Decrement the line position by offset
# File lib/rawline/line.rb, line 129 def left(offset=1) @position = (@position-offset <= 0) ? 0 : @position-offset end
Return the length of the line text.
# File lib/rawline/line.rb, line 164 def length @text.length end
Return the maximum line length. By default, it corresponds to the terminal’s width minus the length of the line prompt.
# File lib/rawline/line.rb, line 51 def max_length terminal_size[0]-@offset end
Increment the line position by offset
# File lib/rawline/line.rb, line 136 def right(offset=1) @position = (@position+offset >= max_length) ? max_length : @position+offset end
Return information about the current word, as a Hash composed by the following elements:
-
:start
: The position in the line corresponding to the word start -
:end
: The position in the line corresponding to the word end -
:text
: The word text.
# File lib/rawline/line.rb, line 61 def word return {:start => bol, :end => eol+1, :text => @text} if @word_separator.to_s == '' last = @text.index(@word_separator, @position) first = @text.rindex(@word_separator, @position) # Trim word separators and handle EOL and BOL if first then first +=1 else first = bol end if last then last -=1 else last = eol+1 unless last end # Swap if overlapping last, first = first, last if last < first text = @text[first..last].to_s # Repeat the search if within word separator if text.match @word_separator then last = first first = @text.rindex(@word_separator, first) if first then first+=1 else first = bol end text = @text[first..last] end {:start => first, :end => last, :text => text} end
Return an array containing the words present in the current line
# File lib/rawline/line.rb, line 94 def words @text.split @word_separator end