class TTY::Reader::Line
Constants
- ANSI_MATCHER
Attributes
The current cursor position witin the text @api public
The line mode @api public
The editable text @api public
Public Class Methods
# File lib/tty/reader/line.rb, line 34 def initialize(prompt, text = '') @prompt = prompt.dup @text = text.dup @cursor = [0, @text.length].max @mode = :edit yield self if block_given? end
Strip ANSI characters from the text
@param [String] text
@return [String]
@api public
# File lib/tty/reader/line.rb, line 16 def self.sanitize(text) text.dup.gsub(ANSI_MATCHER, '') end
Public Instance Methods
Add char and move cursor
@api public
# File lib/tty/reader/line.rb, line 196 def <<(char) @text << char @cursor += 1 end
Read character
@api public
# File lib/tty/reader/line.rb, line 171 def [](i) @text[i] end
Insert characters inside a line. When the lines exceeds maximum length, an extra space is added to accomodate index.
@param [Integer] i
the index to insert at
@example
text = 'aaa' line[5]= 'b' => 'aaa b'
@api public
# File lib/tty/reader/line.rb, line 136 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
Remove char from the line at current position
@api public
# File lib/tty/reader/line.rb, line 204 def delete @text.slice!(@cursor, 1) end
Enable edit mode
@return [Boolean]
@public
# File lib/tty/reader/line.rb, line 56 def edit_mode @mode = :edit end
Check if line is in edit mode
@return [Boolean]
@public
# File lib/tty/reader/line.rb, line 47 def editing? @mode == :edit end
Check if cursor reached end of the line
@return [Boolean]
@api public
# File lib/tty/reader/line.rb, line 92 def end? @cursor == @text.length end
Insert char(s) at cursor position
@api public
# File lib/tty/reader/line.rb, line 189 def insert(chars) self[@cursor] = chars end
Move line position to the left by n chars
@api public
# File lib/tty/reader/line.rb, line 99 def left(n = 1) @cursor = [0, @cursor - n].max end
Move cursor to end position
@api public
# File lib/tty/reader/line.rb, line 120 def move_to_end @cursor = @text.length # put cursor outside of text end
Move cursor to beginning position
@api public
# File lib/tty/reader/line.rb, line 113 def move_to_start @cursor = 0 end
Prompt size
@api public
# File lib/tty/reader/line.rb, line 227 def prompt_size self.class.sanitize(@prompt).size end
Remove char from the line in front of the cursor
@api public
# File lib/tty/reader/line.rb, line 211 def remove left @text.slice!(@cursor, 1) end
Replace current line with new text
@param [String] text
@api public
# File lib/tty/reader/line.rb, line 180 def replace(text) @text = text @cursor = @text.length # put cursor outside of text replace_mode end
Enable replace mode
@return [Boolean]
@public
# File lib/tty/reader/line.rb, line 74 def replace_mode @mode = :replace end
Check if line is in replace mode
@return [Boolean]
@public
# File lib/tty/reader/line.rb, line 65 def replacing? @mode == :replace end
Move line position to the right by n chars
@api public
# File lib/tty/reader/line.rb, line 106 def right(n = 1) @cursor = [@text.length, @cursor + n].min end
Full line size with prompt
@api public
# File lib/tty/reader/line.rb, line 241 def size prompt_size + text_size end
Check if cursor reached beginning of the line
@return [Boolean]
@api public
# File lib/tty/reader/line.rb, line 83 def start? @cursor.zero? end
Text size
@api public
# File lib/tty/reader/line.rb, line 234 def text_size self.class.sanitize(@text).size end
Full line with prompt as string
@api public
# File lib/tty/reader/line.rb, line 219 def to_s "#{@prompt}#{@text}" end