Class: RawLine::Line
- Inherits:
-
Object
- Object
- RawLine::Line
- Includes:
- HighLine::SystemExtensions
- Defined in:
- lib/rawline/line.rb
Overview
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.
Instance Attribute Summary collapse
-
#history ⇒ Object
Returns the value of attribute history.
-
#history_size ⇒ Object
Returns the value of attribute history_size.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#position ⇒ Object
Returns the value of attribute position.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
-
#text ⇒ Object
Returns the value of attribute text.
-
#word_separator ⇒ Object
Returns the value of attribute word_separator.
Instance Method Summary collapse
-
#<<(char) ⇒ Object
Add a character (expressed as a character code) to the line text.
-
#[](index) ⇒ Object
Access the line text at
@index. -
#[]=(index, chars) ⇒ Object
Modify the character(s) in the line text at
@index. -
#bol ⇒ Object
Return the position corresponding to the beginning of the line.
-
#bol? ⇒ Boolean
Return true if the cursor is at the beginning of the line.
-
#eol ⇒ Object
Return the position corresponding to the end of the line.
-
#eol? ⇒ Boolean
Return true if the cursor is at the end of the line.
-
#initialize(history_size) {|_self| ... } ⇒ Line
constructor
Create an instance of RawLine::Line.
-
#left(offset = 1) ⇒ Object
Decrement the line position by
offset. -
#length ⇒ Object
Return the length of the line text.
-
#max_length ⇒ Object
Return the maximum line length.
-
#right(offset = 1) ⇒ Object
Increment the line position by
offset. -
#word ⇒ Object
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. -
#words ⇒ Object
Return an array containing the words present in the current line.
Constructor Details
#initialize(history_size) {|_self| ... } ⇒ Line
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.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rawline/line.rb', line 35 def initialize(history_size) @text = ANSIString.new("") @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 |
Instance Attribute Details
#history ⇒ Object
Returns the value of attribute history.
21 22 23 |
# File 'lib/rawline/line.rb', line 21 def history @history end |
#history_size ⇒ Object
Returns the value of attribute history_size.
21 22 23 |
# File 'lib/rawline/line.rb', line 21 def history_size @history_size end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
22 23 24 |
# File 'lib/rawline/line.rb', line 22 def offset @offset end |
#position ⇒ Object
Returns the value of attribute position.
21 22 23 |
# File 'lib/rawline/line.rb', line 21 def position @position end |
#prompt ⇒ Object
Returns the value of attribute prompt.
21 22 23 |
# File 'lib/rawline/line.rb', line 21 def prompt @prompt end |
#text ⇒ Object
Returns the value of attribute text.
21 22 23 |
# File 'lib/rawline/line.rb', line 21 def text @text end |
#word_separator ⇒ Object
Returns the value of attribute word_separator.
21 22 23 |
# File 'lib/rawline/line.rb', line 21 def word_separator @word_separator end |
Instance Method Details
#<<(char) ⇒ Object
Add a character (expressed as a character code) to the line text.
147 148 149 |
# File 'lib/rawline/line.rb', line 147 def <<(char) @text << char.chr end |
#[](index) ⇒ Object
Access the line text at @index
154 155 156 |
# File 'lib/rawline/line.rb', line 154 def [](index) @text[index] end |
#[]=(index, chars) ⇒ Object
Modify the character(s) in the line text at @index
161 162 163 |
# File 'lib/rawline/line.rb', line 161 def []=(index, chars) @text[index] = chars end |
#bol ⇒ Object
Return the position corresponding to the beginning of the line.
105 106 107 |
# File 'lib/rawline/line.rb', line 105 def bol 0 end |
#bol? ⇒ Boolean
Return true if the cursor is at the beginning of the line.
112 113 114 |
# File 'lib/rawline/line.rb', line 112 def bol? @position<=bol end |
#eol ⇒ Object
Return the position corresponding to the end of the line.
119 120 121 |
# File 'lib/rawline/line.rb', line 119 def eol @text.length-1 end |
#eol? ⇒ Boolean
Return true if the cursor is at the end of the line.
126 127 128 |
# File 'lib/rawline/line.rb', line 126 def eol? @position>=eol end |
#left(offset = 1) ⇒ Object
Decrement the line position by offset
133 134 135 |
# File 'lib/rawline/line.rb', line 133 def left(offset=1) @position = (@position-offset <= 0) ? 0 : @position-offset end |
#length ⇒ Object
Return the length of the line text.
168 169 170 |
# File 'lib/rawline/line.rb', line 168 def length @text.length end |
#max_length ⇒ Object
Return the maximum line length. By default, it corresponds to the terminal’s width minus the length of the line prompt.
51 52 53 |
# File 'lib/rawline/line.rb', line 51 def max_length terminal_size[0]-@offset end |
#right(offset = 1) ⇒ Object
Increment the line position by offset
140 141 142 |
# File 'lib/rawline/line.rb', line 140 def right(offset=1) @position += offset end |
#word ⇒ Object
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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rawline/line.rb', line 65 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 |
#words ⇒ Object
Return an array containing the words present in the current line
98 99 100 |
# File 'lib/rawline/line.rb', line 98 def words @text.split @word_separator end |