Class: TTY::Reader::Line
- Inherits:
-
Object
- Object
- TTY::Reader::Line
- Defined in:
- lib/tty/reader/line.rb
Constant Summary collapse
- ANSI_MATCHER =
/(\[)?\033(\[)?[;?\d]*[\dA-Za-z](\])?/
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
The current cursor position witin the text.
-
#mode ⇒ Object
readonly
The line mode.
- #prompt ⇒ Object readonly
-
#text ⇒ Object
readonly
The editable text.
Class Method Summary collapse
-
.sanitize(text) ⇒ String
Strip ANSI characters from the text.
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.
-
#edit_mode ⇒ Boolean
Enable edit mode.
-
#editing? ⇒ Boolean
Check if line is in edit mode.
-
#end? ⇒ Boolean
Check if cursor reached end of the line.
-
#initialize(prompt, text = '') {|_self| ... } ⇒ Line
constructor
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.
-
#prompt_size ⇒ Object
Prompt size.
-
#remove ⇒ Object
Remove char from the line in front of the cursor.
-
#replace(text) ⇒ Object
Replace current line with new text.
-
#replace_mode ⇒ Boolean
Enable replace mode.
-
#replacing? ⇒ Boolean
Check if line is in replace mode.
-
#right(n = 1) ⇒ Object
Move line position to the right by n chars.
-
#size ⇒ Object
(also: #length)
Full line size with prompt.
-
#start? ⇒ Boolean
Check if cursor reached beginning of the line.
-
#text_size ⇒ Object
Text size.
-
#to_s ⇒ Object
(also: #inspect)
Full line with prompt as string.
Constructor Details
#initialize(prompt, text = '') {|_self| ... } ⇒ Line
Returns a new instance of Line.
35 36 37 38 39 40 41 |
# File 'lib/tty/reader/line.rb', line 35 def initialize(prompt, text = '') @prompt = prompt.dup @text = text.dup @cursor = [0, @text.length].max @mode = :edit yield self if block_given? end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
The current cursor position witin the text
27 28 29 |
# File 'lib/tty/reader/line.rb', line 27 def cursor @cursor end |
#mode ⇒ Object (readonly)
The line mode
31 32 33 |
# File 'lib/tty/reader/line.rb', line 31 def mode @mode end |
#prompt ⇒ Object (readonly)
33 34 35 |
# File 'lib/tty/reader/line.rb', line 33 def prompt @prompt end |
#text ⇒ Object (readonly)
The editable text
23 24 25 |
# File 'lib/tty/reader/line.rb', line 23 def text @text end |
Class Method Details
.sanitize(text) ⇒ String
Strip ANSI characters from the text
17 18 19 |
# File 'lib/tty/reader/line.rb', line 17 def self.sanitize(text) text.dup.gsub(ANSI_MATCHER, '') end |
Instance Method Details
#<<(char) ⇒ Object
Add char and move cursor
197 198 199 200 |
# File 'lib/tty/reader/line.rb', line 197 def <<(char) @text << char @cursor += 1 end |
#[](i) ⇒ Object
Read character
172 173 174 |
# File 'lib/tty/reader/line.rb', line 172 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.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tty/reader/line.rb', line 137 def []=(i, chars) edit_mode 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
205 206 207 |
# File 'lib/tty/reader/line.rb', line 205 def delete @text.slice!(@cursor, 1) end |
#edit_mode ⇒ Boolean
Enable edit mode
57 58 59 |
# File 'lib/tty/reader/line.rb', line 57 def edit_mode @mode = :edit end |
#editing? ⇒ Boolean
Check if line is in edit mode
48 49 50 |
# File 'lib/tty/reader/line.rb', line 48 def editing? @mode == :edit end |
#end? ⇒ Boolean
Check if cursor reached end of the line
93 94 95 |
# File 'lib/tty/reader/line.rb', line 93 def end? @cursor == @text.length end |
#insert(chars) ⇒ Object
Insert char(s) at cursor position
190 191 192 |
# File 'lib/tty/reader/line.rb', line 190 def insert(chars) self[@cursor] = chars end |
#left(n = 1) ⇒ Object
Move line position to the left by n chars
100 101 102 |
# File 'lib/tty/reader/line.rb', line 100 def left(n = 1) @cursor = [0, @cursor - n].max end |
#move_to_end ⇒ Object
Move cursor to end position
121 122 123 |
# File 'lib/tty/reader/line.rb', line 121 def move_to_end @cursor = @text.length # put cursor outside of text end |
#move_to_start ⇒ Object
Move cursor to beginning position
114 115 116 |
# File 'lib/tty/reader/line.rb', line 114 def move_to_start @cursor = 0 end |
#prompt_size ⇒ Object
Prompt size
228 229 230 |
# File 'lib/tty/reader/line.rb', line 228 def prompt_size self.class.sanitize(@prompt).size end |
#remove ⇒ Object
Remove char from the line in front of the cursor
212 213 214 215 |
# File 'lib/tty/reader/line.rb', line 212 def remove left @text.slice!(@cursor, 1) end |
#replace(text) ⇒ Object
Replace current line with new text
181 182 183 184 185 |
# File 'lib/tty/reader/line.rb', line 181 def replace(text) @text = text @cursor = @text.length # put cursor outside of text replace_mode end |
#replace_mode ⇒ Boolean
Enable replace mode
75 76 77 |
# File 'lib/tty/reader/line.rb', line 75 def replace_mode @mode = :replace end |
#replacing? ⇒ Boolean
Check if line is in replace mode
66 67 68 |
# File 'lib/tty/reader/line.rb', line 66 def replacing? @mode == :replace end |
#right(n = 1) ⇒ Object
Move line position to the right by n chars
107 108 109 |
# File 'lib/tty/reader/line.rb', line 107 def right(n = 1) @cursor = [@text.length, @cursor + n].min end |
#size ⇒ Object Also known as: length
Full line size with prompt
242 243 244 |
# File 'lib/tty/reader/line.rb', line 242 def size prompt_size + text_size end |
#start? ⇒ Boolean
Check if cursor reached beginning of the line
84 85 86 |
# File 'lib/tty/reader/line.rb', line 84 def start? @cursor.zero? end |
#text_size ⇒ Object
Text size
235 236 237 |
# File 'lib/tty/reader/line.rb', line 235 def text_size self.class.sanitize(@text).size end |
#to_s ⇒ Object Also known as: inspect
Full line with prompt as string
220 221 222 |
# File 'lib/tty/reader/line.rb', line 220 def to_s "#{@prompt}#{@text}" end |