Class: TTY::Reader::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/reader/line.rb

Constant Summary collapse

ANSI_MATCHER =
/(\[)?\033(\[)?[;?\d]*[\dA-Za-z](\])?/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt, text = '') {|_self| ... } ⇒ Line

Returns a new instance of Line.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
# File 'lib/tty/reader/line.rb', line 35

def initialize(prompt, text = '')
  @prompt = prompt.dup
  @text   = text.dup
  @cursor = [0, @text.length].max
  @mode   = :edit
  yield self if block_given?
end

Instance Attribute Details

#cursorObject (readonly)

The current cursor position witin the text



27
28
29
# File 'lib/tty/reader/line.rb', line 27

def cursor
  @cursor
end

#modeObject (readonly)

The line mode



31
32
33
# File 'lib/tty/reader/line.rb', line 31

def mode
  @mode
end

#promptObject (readonly)



33
34
35
# File 'lib/tty/reader/line.rb', line 33

def prompt
  @prompt
end

#textObject (readonly)

The editable text



23
24
25
# File 'lib/tty/reader/line.rb', line 23

def text
  @text
end

Class Method Details

.sanitize(text) ⇒ String

Strip ANSI characters from the text

Parameters:

  • text (String)

Returns:

  • (String)


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

def self.sanitize(text)
  text.dup.gsub(ANSI_MATCHER, '')
end

Instance Method Details

#<<(char) ⇒ Object

Add char and move cursor



197
198
199
200
# File 'lib/tty/reader/line.rb', line 197

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

#[](i) ⇒ Object

Read character



172
173
174
# File 'lib/tty/reader/line.rb', line 172

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tty/reader/line.rb', line 137

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

#deleteObject

Remove char from the line at current position



205
206
207
# File 'lib/tty/reader/line.rb', line 205

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

#edit_modeBoolean

Enable edit mode

Returns:

  • (Boolean)


57
58
59
# File 'lib/tty/reader/line.rb', line 57

def edit_mode
  @mode = :edit
end

#editing?Boolean

Check if line is in edit mode

Returns:

  • (Boolean)


48
49
50
# File 'lib/tty/reader/line.rb', line 48

def editing?
  @mode == :edit
end

#end?Boolean

Check if cursor reached end of the line

Returns:

  • (Boolean)


93
94
95
# File 'lib/tty/reader/line.rb', line 93

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

#insert(chars) ⇒ Object

Insert char(s) at cursor position



190
191
192
# File 'lib/tty/reader/line.rb', line 190

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

#left(n = 1) ⇒ Object

Move line position to the left by n chars



100
101
102
# File 'lib/tty/reader/line.rb', line 100

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

#move_to_endObject

Move cursor to end position



121
122
123
# File 'lib/tty/reader/line.rb', line 121

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

#move_to_startObject

Move cursor to beginning position



114
115
116
# File 'lib/tty/reader/line.rb', line 114

def move_to_start
  @cursor = 0
end

#prompt_sizeObject

Prompt size



228
229
230
# File 'lib/tty/reader/line.rb', line 228

def prompt_size
  self.class.sanitize(@prompt).size
end

#removeObject

Remove char from the line in front of the cursor



212
213
214
215
# File 'lib/tty/reader/line.rb', line 212

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

#replace(text) ⇒ Object

Replace current line with new text

Parameters:

  • text (String)


181
182
183
184
185
# File 'lib/tty/reader/line.rb', line 181

def replace(text)
  @text = text
  @cursor = @text.length # put cursor outside of text
  replace_mode
end

#replace_modeBoolean

Enable replace mode

Returns:

  • (Boolean)


75
76
77
# File 'lib/tty/reader/line.rb', line 75

def replace_mode
  @mode = :replace
end

#replacing?Boolean

Check if line is in replace mode

Returns:

  • (Boolean)


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

def replacing?
  @mode == :replace
end

#right(n = 1) ⇒ Object

Move line position to the right by n chars



107
108
109
# File 'lib/tty/reader/line.rb', line 107

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

#sizeObject Also known as: length

Full line size with prompt



242
243
244
# File 'lib/tty/reader/line.rb', line 242

def size
  prompt_size + text_size
end

#start?Boolean

Check if cursor reached beginning of the line

Returns:

  • (Boolean)


84
85
86
# File 'lib/tty/reader/line.rb', line 84

def start?
  @cursor.zero?
end

#text_sizeObject

Text size



235
236
237
# File 'lib/tty/reader/line.rb', line 235

def text_size
  self.class.sanitize(@text).size
end

#to_sObject Also known as: inspect

Full line with prompt as string



220
221
222
# File 'lib/tty/reader/line.rb', line 220

def to_s
  "#{@prompt}#{@text}"
end