class TTY::Prompt::Reader::Line

Attributes

cursor[RW]
text[RW]

Public Class Methods

new(text = "") { |self| ... } click to toggle source
# 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

Public Instance Methods

<<(char) click to toggle source

Add char and move cursor

@api public

# File lib/tty/prompt/reader/line.rb, line 140
def <<(char)
  @text << char
  @cursor += 1
end
[](i) click to toggle source

Read character

@api public

# File lib/tty/prompt/reader/line.rb, line 116
def [](i)
  @text[i]
end
[]=(i, chars) click to toggle source

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/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() click to toggle source

Remove char from the line at current position

@api public

# File lib/tty/prompt/reader/line.rb, line 148
def delete
  @text.slice!(@cursor, 1)
end
end?() click to toggle source

Check if cursor reached end of the line

@return [Boolean]

@api public

# File lib/tty/prompt/reader/line.rb, line 38
def end?
  @cursor == @text.length
end
insert(chars) click to toggle source

Insert char(s) at cursor position

@api public

# File lib/tty/prompt/reader/line.rb, line 133
def insert(chars)
  self[@cursor] = chars
end
left(n = 1) click to toggle source

Move line position to the left by n chars

@api public

# File lib/tty/prompt/reader/line.rb, line 45
def left(n = 1)
  @cursor = [0, @cursor - n].max
end
move_to_end() click to toggle source

Move cursor to end position

@api public

# 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() click to toggle source

Move cursor to beginning position

@api public

# File lib/tty/prompt/reader/line.rb, line 59
def move_to_start
  @cursor = 0
end
remove() click to toggle source

Remove char from the line in front of the cursor

@api public

# File lib/tty/prompt/reader/line.rb, line 155
def remove
  left
  @text.slice!(@cursor, 1)
end
replace(text) click to toggle source

Replace current line with new text

@param [String] text

@api public

# 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) click to toggle source

Move line position to the right by n chars

@api public

# File lib/tty/prompt/reader/line.rb, line 52
def right(n = 1)
  @cursor = [@text.length, @cursor + n].min
end
start?() click to toggle source

Check if cursor reached beginning of the line

@return [Boolean]

@api public

# File lib/tty/prompt/reader/line.rb, line 29
def start?
  @cursor == 0
end