Class: TTY::Reader::Line

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

Overview

API:

  • public

Constant Summary collapse

ANSI_MATCHER =

API:

  • public

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a Line instance

Yields:

  • (_self)

Yield Parameters:

API:

  • private



40
41
42
43
44
45
46
47
# File 'lib/tty/reader/line.rb', line 40

def initialize(text = "", prompt: "")
  @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

API:

  • public



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

def cursor
  @cursor
end

#modeObject (readonly)

The line mode

API:

  • public



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

def mode
  @mode
end

#promptObject (readonly)

The prompt displayed before input

API:

  • public



35
36
37
# File 'lib/tty/reader/line.rb', line 35

def prompt
  @prompt
end

#textObject (readonly)

The editable text

API:

  • public



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:

Returns:

API:

  • public



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

API:

  • public



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

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

#[](i) ⇒ Object

Read character

API:

  • public



179
180
181
# File 'lib/tty/reader/line.rb', line 179

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

  • the characters to insert

API:

  • public



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/tty/reader/line.rb', line 146

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) ⇒ Object

Remove char from the line at current position

API:

  • public



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

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

#edit_modeBoolean

Enable edit mode

Returns:

API:

  • public



63
64
65
# File 'lib/tty/reader/line.rb', line 63

def edit_mode
  @mode = :edit
end

#editing?Boolean

Check if line is in edit mode

Returns:

API:

  • public



54
55
56
# File 'lib/tty/reader/line.rb', line 54

def editing?
  @mode == :edit
end

#end?Boolean

Check if cursor reached end of the line

Returns:

API:

  • public



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

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

#insert(chars) ⇒ Object

Insert char(s) at cursor position

API:

  • public



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

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

#left(n = 1) ⇒ Object

Move line position to the left by n chars

API:

  • public



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

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

#move_to_endObject

Move cursor to end position

API:

  • public



127
128
129
# File 'lib/tty/reader/line.rb', line 127

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

#move_to_startObject

Move cursor to beginning position

API:

  • public



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

def move_to_start
  @cursor = 0
end

#prompt_sizeObject

Prompt size

API:

  • public



238
239
240
241
242
243
# File 'lib/tty/reader/line.rb', line 238

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) ⇒ Object

Remove char from the line in front of the cursor

Parameters:

  • (defaults to: 1)

    the number of chars to remove

API:

  • public



222
223
224
225
# File 'lib/tty/reader/line.rb', line 222

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

#replace(text) ⇒ Object

Replace current line with new text

Parameters:

API:

  • public



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

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

#replace_modeBoolean

Enable replace mode

Returns:

API:

  • public



81
82
83
# File 'lib/tty/reader/line.rb', line 81

def replace_mode
  @mode = :replace
end

#replacing?Boolean

Check if line is in replace mode

Returns:

API:

  • public



72
73
74
# File 'lib/tty/reader/line.rb', line 72

def replacing?
  @mode == :replace
end

#right(n = 1) ⇒ Object

Move line position to the right by n chars

API:

  • public



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

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

#sizeObject Also known as: length

Full line size with prompt

API:

  • public



255
256
257
# File 'lib/tty/reader/line.rb', line 255

def size
  prompt_size + text_size
end

#start?Boolean

Check if cursor reached beginning of the line

Returns:

API:

  • public



90
91
92
# File 'lib/tty/reader/line.rb', line 90

def start?
  @cursor.zero?
end

#text_sizeObject

Text size

API:

  • public



248
249
250
# File 'lib/tty/reader/line.rb', line 248

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

#to_sObject Also known as: inspect

Full line with prompt as string

API:

  • public



230
231
232
# File 'lib/tty/reader/line.rb', line 230

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