Class: TTY::Prompt::Reader::Line Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tty/prompt/reader/line.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = "") {|_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.

Returns a new instance of Line.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
# 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

Instance Attribute Details

#cursorObject

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.



16
17
18
# File 'lib/tty/prompt/reader/line.rb', line 16

def cursor
  @cursor
end

#textObject

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.



14
15
16
# File 'lib/tty/prompt/reader/line.rb', line 14

def text
  @text
end

Instance Method Details

#<<(char) ⇒ Object

Add char and move cursor



140
141
142
143
# File 'lib/tty/prompt/reader/line.rb', line 140

def <<(char)
  @text << char
  @cursor += 1
end

#[](i) ⇒ Object

Read character



116
117
118
# File 'lib/tty/prompt/reader/line.rb', line 116

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.

Examples:

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

Parameters:

  • i (Integer)

    the index to insert at



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

#deleteObject

Remove char from the line at current position



148
149
150
# File 'lib/tty/prompt/reader/line.rb', line 148

def delete
  @text.slice!(@cursor, 1)
end

#end?Boolean

Check if cursor reached end of the line

Returns:

  • (Boolean)


38
39
40
# File 'lib/tty/prompt/reader/line.rb', line 38

def end?
  @cursor == @text.length
end

#insert(chars) ⇒ Object

Insert char(s) at cursor position



133
134
135
# File 'lib/tty/prompt/reader/line.rb', line 133

def insert(chars)
  self[@cursor] = chars
end

#left(n = 1) ⇒ Object

Move line position to the left by n chars



45
46
47
# File 'lib/tty/prompt/reader/line.rb', line 45

def left(n = 1)
  @cursor = [0, @cursor - n].max
end

#move_to_endObject

Move cursor to end position



66
67
68
# File 'lib/tty/prompt/reader/line.rb', line 66

def move_to_end
  @cursor = @text.length # put cursor outside of text
end

#move_to_startObject

Move cursor to beginning position



59
60
61
# File 'lib/tty/prompt/reader/line.rb', line 59

def move_to_start
  @cursor = 0
end

#removeObject

Remove char from the line in front of the cursor



155
156
157
158
# File 'lib/tty/prompt/reader/line.rb', line 155

def remove
  left
  @text.slice!(@cursor, 1)
end

#replace(text) ⇒ Object

Replace current line with new text

Parameters:

  • text (String)


125
126
127
128
# 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) ⇒ Object

Move line position to the right by n chars



52
53
54
# File 'lib/tty/prompt/reader/line.rb', line 52

def right(n = 1)
  @cursor = [@text.length, @cursor + n].min
end

#start?Boolean

Check if cursor reached beginning of the line

Returns:

  • (Boolean)


29
30
31
# File 'lib/tty/prompt/reader/line.rb', line 29

def start?
  @cursor == 0
end