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
-
#<<(string) ⇒ 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 = 30) {|_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. - #text_up_to_position ⇒ Object
-
#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 = 30) {|_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=30) @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
#<<(string) ⇒ Object
Add a character (expressed as a character code) to the line text.
151 152 153 |
# File 'lib/rawline/line.rb', line 151 def <<(string) @text << string end |
#[](index) ⇒ Object
Access the line text at @index
158 159 160 |
# File 'lib/rawline/line.rb', line 158 def [](index) @text[index] end |
#[]=(index, chars) ⇒ Object
Modify the character(s) in the line text at @index
165 166 167 |
# File 'lib/rawline/line.rb', line 165 def []=(index, chars) @text[index] = chars end |
#bol ⇒ Object
Return the position corresponding to the beginning of the line.
109 110 111 |
# File 'lib/rawline/line.rb', line 109 def bol 0 end |
#bol? ⇒ Boolean
Return true if the cursor is at the beginning of the line.
116 117 118 |
# File 'lib/rawline/line.rb', line 116 def bol? @position<=bol end |
#eol ⇒ Object
Return the position corresponding to the end of the line.
123 124 125 |
# File 'lib/rawline/line.rb', line 123 def eol @text.length-1 end |
#eol? ⇒ Boolean
Return true if the cursor is at the end of the line.
130 131 132 |
# File 'lib/rawline/line.rb', line 130 def eol? @position>=eol end |
#left(offset = 1) ⇒ Object
Decrement the line position by offset
137 138 139 |
# File 'lib/rawline/line.rb', line 137 def left(offset=1) @position = (@position-offset <= 0) ? 0 : @position-offset end |
#length ⇒ Object
Return the length of the line text.
172 173 174 |
# File 'lib/rawline/line.rb', line 172 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
144 145 146 |
# File 'lib/rawline/line.rb', line 144 def right(offset=1) @position += offset end |
#text_up_to_position ⇒ Object
59 60 61 |
# File 'lib/rawline/line.rb', line 59 def text_up_to_position text[0...position] 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.
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 94 95 96 97 |
# File 'lib/rawline/line.rb', line 69 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
102 103 104 |
# File 'lib/rawline/line.rb', line 102 def words @text.split @word_separator end |