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

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

Overview

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.

API:

  • private

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:

API:

  • private



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.

API:

  • private



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.

API:

  • private



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

API:

  • public



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

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

#[](i) ⇒ Object

Read character

API:

  • public



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:

  • the index to insert at

API:

  • public



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

API:

  • public



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:

API:

  • public



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

API:

  • public



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

API:

  • public



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

API:

  • public



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

API:

  • public



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

API:

  • public



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:

API:

  • public



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

API:

  • public



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:

API:

  • public



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

def start?
  @cursor == 0
end