Class: TTY::Prompt::Reader::Line Private
- Inherits:
-
Object
- Object
- TTY::Prompt::Reader::Line
- Extended by:
- Forwardable
- Defined in:
- lib/tty/prompt/reader/line.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
- #cursor ⇒ Object private
- #text ⇒ Object private
Instance Method Summary collapse
-
#<<(char) ⇒ Object
Add char and move cursor.
-
#[](i) ⇒ Object
Read character.
-
#[]=(i, chars) ⇒ Object
Insert characters inside a line.
-
#delete ⇒ Object
Remove char from the line at current position.
-
#end? ⇒ Boolean
Check if cursor reached end of the line.
-
#initialize(text = "") {|_self| ... } ⇒ Line
constructor
private
A new instance of Line.
-
#insert(chars) ⇒ Object
Insert char(s) at cursor position.
-
#left(n = 1) ⇒ Object
Move line position to the left by n chars.
-
#move_to_end ⇒ Object
Move cursor to end position.
-
#move_to_start ⇒ Object
Move cursor to beginning position.
-
#remove ⇒ Object
Remove char from the line in front of the cursor.
-
#replace(text) ⇒ Object
Replace current line with new text.
-
#right(n = 1) ⇒ Object
Move line position to the right by n chars.
-
#start? ⇒ Boolean
Check if cursor reached beginning of the line.
Constructor Details
#initialize(text = "") {|_self| ... } ⇒ Line
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Line.
18 19 20 21 22 |
# File 'lib/tty/prompt/reader/line.rb', line 18 def initialize(text = "") @text = text @cursor = [0, @text.length].max yield self if block_given? end |
Instance Attribute Details
#cursor ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/tty/prompt/reader/line.rb', line 16 def cursor @cursor end |
#text ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/tty/prompt/reader/line.rb', line 14 def text @text end |
Instance Method Details
#<<(char) ⇒ Object
Add char and move cursor
140 141 142 143 |
# File 'lib/tty/prompt/reader/line.rb', line 140 def <<(char) @text << char @cursor += 1 end |
#[](i) ⇒ Object
Read character
116 117 118 |
# File 'lib/tty/prompt/reader/line.rb', line 116 def [](i) @text[i] end |
#[]=(i, chars) ⇒ Object
Insert characters inside a line. When the lines exceeds maximum length, an extra space is added to accomodate index.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tty/prompt/reader/line.rb', line 82 def []=(i, chars) if i.is_a?(Range) @text[i] = chars @cursor += chars.length return end if i <= 0 before_text = '' after_text = @text.dup elsif i == @text.length - 1 before_text = @text.dup after_text = '' elsif i > @text.length - 1 before_text = @text.dup after_text = ?\s * (i - @text.length) @cursor += after_text.length else before_text = @text[0..i-1].dup after_text = @text[i..-1].dup end if i > @text.length - 1 @text = before_text << after_text << chars else @text = before_text << chars << after_text end @cursor = i + chars.length end |
#delete ⇒ Object
Remove char from the line at current position
148 149 150 |
# File 'lib/tty/prompt/reader/line.rb', line 148 def delete @text.slice!(@cursor, 1) end |
#end? ⇒ Boolean
Check if cursor reached end of the line
38 39 40 |
# File 'lib/tty/prompt/reader/line.rb', line 38 def end? @cursor == @text.length end |
#insert(chars) ⇒ Object
Insert char(s) at cursor position
133 134 135 |
# File 'lib/tty/prompt/reader/line.rb', line 133 def insert(chars) self[@cursor] = chars end |
#left(n = 1) ⇒ Object
Move line position to the left by n chars
45 46 47 |
# File 'lib/tty/prompt/reader/line.rb', line 45 def left(n = 1) @cursor = [0, @cursor - n].max end |
#move_to_end ⇒ Object
Move cursor to end position
66 67 68 |
# File 'lib/tty/prompt/reader/line.rb', line 66 def move_to_end @cursor = @text.length # put cursor outside of text end |
#move_to_start ⇒ Object
Move cursor to beginning position
59 60 61 |
# File 'lib/tty/prompt/reader/line.rb', line 59 def move_to_start @cursor = 0 end |
#remove ⇒ Object
Remove char from the line in front of the cursor
155 156 157 158 |
# File 'lib/tty/prompt/reader/line.rb', line 155 def remove left @text.slice!(@cursor, 1) end |
#replace(text) ⇒ Object
Replace current line with new text
125 126 127 128 |
# File 'lib/tty/prompt/reader/line.rb', line 125 def replace(text) @text = text @cursor = @text.length # put cursor outside of text end |
#right(n = 1) ⇒ Object
Move line position to the right by n chars
52 53 54 |
# File 'lib/tty/prompt/reader/line.rb', line 52 def right(n = 1) @cursor = [@text.length, @cursor + n].min end |
#start? ⇒ Boolean
Check if cursor reached beginning of the line
29 30 31 |
# File 'lib/tty/prompt/reader/line.rb', line 29 def start? @cursor == 0 end |