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
The prompt displayed before input.
-
#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(n = 1) ⇒ 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(text = "", prompt: "") {|_self| ... } ⇒ Line
constructor
private
Create a Line instance.
-
#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(n = 1) ⇒ 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(text = "", prompt: "") {|_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.
Create a Line instance
40 41 42 43 44 45 46 47 |
# File 'lib/tty/reader/line.rb', line 40 def initialize(text = "", prompt: "") @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)
The prompt displayed before input
35 36 37 |
# File 'lib/tty/reader/line.rb', line 35 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
204 205 206 207 |
# File 'lib/tty/reader/line.rb', line 204 def <<(char) @text << char @cursor += 1 end |
#[](i) ⇒ Object
Read character
179 180 181 |
# File 'lib/tty/reader/line.rb', line 179 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.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/tty/reader/line.rb', line 146 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 # insert outside of line input 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(n = 1) ⇒ Object
Remove char from the line at current position
212 213 214 |
# File 'lib/tty/reader/line.rb', line 212 def delete(n = 1) @text.slice!(@cursor, n) end |
#edit_mode ⇒ Boolean
Enable edit mode
63 64 65 |
# File 'lib/tty/reader/line.rb', line 63 def edit_mode @mode = :edit end |
#editing? ⇒ Boolean
Check if line is in edit mode
54 55 56 |
# File 'lib/tty/reader/line.rb', line 54 def editing? @mode == :edit end |
#end? ⇒ Boolean
Check if cursor reached end of the line
99 100 101 |
# File 'lib/tty/reader/line.rb', line 99 def end? @cursor == @text.length end |
#insert(chars) ⇒ Object
Insert char(s) at cursor position
197 198 199 |
# File 'lib/tty/reader/line.rb', line 197 def insert(chars) self[@cursor] = chars end |
#left(n = 1) ⇒ Object
Move line position to the left by n chars
106 107 108 |
# File 'lib/tty/reader/line.rb', line 106 def left(n = 1) @cursor = [0, @cursor - n].max end |
#move_to_end ⇒ Object
Move cursor to end position
127 128 129 |
# File 'lib/tty/reader/line.rb', line 127 def move_to_end @cursor = @text.length # put cursor outside of text end |
#move_to_start ⇒ Object
Move cursor to beginning position
120 121 122 |
# File 'lib/tty/reader/line.rb', line 120 def move_to_start @cursor = 0 end |
#prompt_size ⇒ Object
Prompt size
238 239 240 241 242 243 |
# File 'lib/tty/reader/line.rb', line 238 def prompt_size p = self.class.sanitize(@prompt).split(/\r?\n/) # return the length of each line + screen width for every line past the first # which accounts for multi-line prompts p.join.length + ((p.length - 1) * TTY::Screen.width ) end |
#remove(n = 1) ⇒ Object
Remove char from the line in front of the cursor
222 223 224 225 |
# File 'lib/tty/reader/line.rb', line 222 def remove(n = 1) left(n) @text.slice!(@cursor, n) end |
#replace(text) ⇒ Object
Replace current line with new text
188 189 190 191 192 |
# File 'lib/tty/reader/line.rb', line 188 def replace(text) @text = text @cursor = @text.length # put cursor outside of text replace_mode end |
#replace_mode ⇒ Boolean
Enable replace mode
81 82 83 |
# File 'lib/tty/reader/line.rb', line 81 def replace_mode @mode = :replace end |
#replacing? ⇒ Boolean
Check if line is in replace mode
72 73 74 |
# File 'lib/tty/reader/line.rb', line 72 def replacing? @mode == :replace end |
#right(n = 1) ⇒ Object
Move line position to the right by n chars
113 114 115 |
# File 'lib/tty/reader/line.rb', line 113 def right(n = 1) @cursor = [@text.length, @cursor + n].min end |
#size ⇒ Object Also known as: length
Full line size with prompt
255 256 257 |
# File 'lib/tty/reader/line.rb', line 255 def size prompt_size + text_size end |
#start? ⇒ Boolean
Check if cursor reached beginning of the line
90 91 92 |
# File 'lib/tty/reader/line.rb', line 90 def start? @cursor.zero? end |
#text_size ⇒ Object
Text size
248 249 250 |
# File 'lib/tty/reader/line.rb', line 248 def text_size self.class.sanitize(@text).size end |
#to_s ⇒ Object Also known as: inspect
Full line with prompt as string
230 231 232 |
# File 'lib/tty/reader/line.rb', line 230 def to_s "#{@prompt}#{@text}" end |