class TTY::Reader::Line

Constants

ANSI_MATCHER

Attributes

cursor[R]

The current cursor position witin the text @api public

mode[R]

The line mode @api public

prompt[R]

The prompt displayed before input @api public

text[R]

The editable text @api public

Public Class Methods

new(text = "", prompt: "") { |self| ... } click to toggle source

Create a Line instance

@api private

# File lib/tty/reader/line.rb, line 39
def initialize(text = "", prompt: "")
  @prompt = prompt.dup
  @text   = text.dup
  @cursor = [0, @text.length].max
  @mode   = :edit

  yield self if block_given?
end
sanitize(text) click to toggle source

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

<<(char) click to toggle source

Add char and move cursor

@api public

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

Read character

@api public

# File lib/tty/reader/line.rb, line 178
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

@param [String] chars

the characters to insert

@example

text = "aaa"
line[5]= "b"
=> "aaa  b"

@api public

# File lib/tty/reader/line.rb, line 145
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) click to toggle source

Remove char from the line at current position

@api public

# File lib/tty/reader/line.rb, line 211
def delete(n = 1)
  @text.slice!(@cursor, n)
end
edit_mode() click to toggle source

Enable edit mode

@return [Boolean]

@public

# File lib/tty/reader/line.rb, line 62
def edit_mode
  @mode = :edit
end
editing?() click to toggle source

Check if line is in edit mode

@return [Boolean]

@public

# File lib/tty/reader/line.rb, line 53
def editing?
  @mode == :edit
end
end?() click to toggle source

Check if cursor reached end of the line

@return [Boolean]

@api public

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

Insert char(s) at cursor position

@api public

# File lib/tty/reader/line.rb, line 196
def insert(chars)
  self[@cursor] = chars
end
inspect()
Alias for: to_s
left(n = 1) click to toggle source

Move line position to the left by n chars

@api public

# File lib/tty/reader/line.rb, line 105
def left(n = 1)
  @cursor = [0, @cursor - n].max
end
length()
Alias for: size
move_to_end() click to toggle source

Move cursor to end position

@api public

# File lib/tty/reader/line.rb, line 126
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/reader/line.rb, line 119
def move_to_start
  @cursor = 0
end
prompt_size() click to toggle source

Prompt size

@api public

# File lib/tty/reader/line.rb, line 237
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) click to toggle source

Remove char from the line in front of the cursor

@param [Integer] n

the number of chars to remove

@api public

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

Replace current line with new text

@param [String] text

@api public

# File lib/tty/reader/line.rb, line 187
def replace(text)
  @text = text
  @cursor = @text.length # put cursor outside of text
  replace_mode
end
replace_mode() click to toggle source

Enable replace mode

@return [Boolean]

@public

# File lib/tty/reader/line.rb, line 80
def replace_mode
  @mode = :replace
end
replacing?() click to toggle source

Check if line is in replace mode

@return [Boolean]

@public

# File lib/tty/reader/line.rb, line 71
def replacing?
  @mode == :replace
end
right(n = 1) click to toggle source

Move line position to the right by n chars

@api public

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

Full line size with prompt

@api public

# File lib/tty/reader/line.rb, line 254
def size
  prompt_size + text_size
end
Also aliased as: length
start?() click to toggle source

Check if cursor reached beginning of the line

@return [Boolean]

@api public

# File lib/tty/reader/line.rb, line 89
def start?
  @cursor.zero?
end
text_size() click to toggle source

Text size

@api public

# File lib/tty/reader/line.rb, line 247
def text_size
  self.class.sanitize(@text).size
end
to_s() click to toggle source

Full line with prompt as string

@api public

# File lib/tty/reader/line.rb, line 229
def to_s
  "#{@prompt}#{@text}"
end
Also aliased as: inspect